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

1.152   ! nicm        1: /* $OpenBSD: screen-write.c,v 1.151 2019/04/18 11:07:28 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.152   ! nicm      402:
        !           403:        screen_write_set_cursor(ctx, cx, cy);
        !           404: }
        !           405:
        !           406: /* Draw a menu on screen. */
        !           407: void
        !           408: screen_write_menu(struct screen_write_ctx *ctx, struct menu *menu, int choice)
        !           409: {
        !           410:        struct screen           *s = ctx->s;
        !           411:        struct grid_cell         gc;
        !           412:        u_int                    cx, cy, i, j;
        !           413:
        !           414:        cx = s->cx;
        !           415:        cy = s->cy;
        !           416:
        !           417:        memcpy(&gc, &grid_default_cell, sizeof gc);
        !           418:
        !           419:        screen_write_box(ctx, menu->width + 4, menu->count + 2);
        !           420:        screen_write_cursormove(ctx, cx + 2, cy, 0);
        !           421:        format_draw(ctx, &gc, menu->width, menu->title, NULL);
        !           422:
        !           423:        for (i = 0; i < menu->count; i++) {
        !           424:                if (menu->items[i].name == NULL) {
        !           425:                        screen_write_cursormove(ctx, cx, cy + 1 + i, 0);
        !           426:                        screen_write_hline(ctx, menu->width + 4, 1, 1);
        !           427:                } else {
        !           428:                        if (choice >= 0 && i == (u_int)choice)
        !           429:                                gc.attr |= GRID_ATTR_REVERSE;
        !           430:                        screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
        !           431:                        for (j = 0; j < menu->width; j++)
        !           432:                                screen_write_putc(ctx, &gc, ' ');
        !           433:                        screen_write_cursormove(ctx, cx + 2, cy + 1 + i, 0);
        !           434:                        format_draw(ctx, &gc, menu->width, menu->items[i].name,
        !           435:                            NULL);
        !           436:                        if (choice >= 0 && i == (u_int)choice)
        !           437:                                gc.attr &= ~GRID_ATTR_REVERSE;
        !           438:                }
        !           439:        }
1.125     nicm      440:
1.144     nicm      441:        screen_write_set_cursor(ctx, cx, cy);
1.125     nicm      442: }
                    443:
                    444: /* Draw a box on screen. */
                    445: void
                    446: screen_write_box(struct screen_write_ctx *ctx, u_int nx, u_int ny)
                    447: {
                    448:        struct screen           *s = ctx->s;
                    449:        struct grid_cell         gc;
                    450:        u_int                    cx, cy, i;
                    451:
                    452:        cx = s->cx;
                    453:        cy = s->cy;
                    454:
                    455:        memcpy(&gc, &grid_default_cell, sizeof gc);
                    456:        gc.attr |= GRID_ATTR_CHARSET;
                    457:
                    458:        screen_write_putc(ctx, &gc, 'l');
                    459:        for (i = 1; i < nx - 1; i++)
                    460:                screen_write_putc(ctx, &gc, 'q');
                    461:        screen_write_putc(ctx, &gc, 'k');
                    462:
1.144     nicm      463:        screen_write_set_cursor(ctx, cx, cy + ny - 1);
1.125     nicm      464:        screen_write_putc(ctx, &gc, 'm');
                    465:        for (i = 1; i < nx - 1; i++)
                    466:                screen_write_putc(ctx, &gc, 'q');
                    467:        screen_write_putc(ctx, &gc, 'j');
                    468:
                    469:        for (i = 1; i < ny - 1; i++) {
1.144     nicm      470:                screen_write_set_cursor(ctx, cx, cy + i);
1.125     nicm      471:                screen_write_putc(ctx, &gc, 'x');
                    472:        }
                    473:        for (i = 1; i < ny - 1; i++) {
1.144     nicm      474:                screen_write_set_cursor(ctx, cx + nx - 1, cy + i);
1.125     nicm      475:                screen_write_putc(ctx, &gc, 'x');
                    476:        }
                    477:
1.144     nicm      478:        screen_write_set_cursor(ctx, cx, cy);
1.125     nicm      479: }
                    480:
1.132     nicm      481: /*
                    482:  * Write a preview version of a window. Assumes target area is big enough and
                    483:  * already cleared.
                    484:  */
1.125     nicm      485: void
                    486: screen_write_preview(struct screen_write_ctx *ctx, struct screen *src, u_int nx,
                    487:     u_int ny)
                    488: {
                    489:        struct screen           *s = ctx->s;
                    490:        struct grid_cell         gc;
                    491:        u_int                    cx, cy, px, py;
                    492:
                    493:        cx = s->cx;
                    494:        cy = s->cy;
                    495:
                    496:        /*
                    497:         * If the cursor is on, pick the area around the cursor, otherwise use
                    498:         * the top left.
                    499:         */
                    500:        if (src->mode & MODE_CURSOR) {
                    501:                px = src->cx;
                    502:                if (px < nx / 3)
                    503:                        px = 0;
                    504:                else
                    505:                        px = px - nx / 3;
                    506:                if (px + nx > screen_size_x(src)) {
                    507:                        if (nx > screen_size_x(src))
                    508:                                px = 0;
                    509:                        else
                    510:                                px = screen_size_x(src) - nx;
                    511:                }
                    512:                py = src->cy;
                    513:                if (py < ny / 3)
                    514:                        py = 0;
                    515:                else
                    516:                        py = py - ny / 3;
                    517:                if (py + ny > screen_size_y(src)) {
                    518:                        if (ny > screen_size_y(src))
                    519:                                py = 0;
                    520:                        else
                    521:                                py = screen_size_y(src) - ny;
                    522:                }
                    523:        } else {
                    524:                px = 0;
                    525:                py = 0;
                    526:        }
                    527:
1.132     nicm      528:        screen_write_fast_copy(ctx, src, px, src->grid->hsize + py, nx, ny);
1.125     nicm      529:
                    530:        if (src->mode & MODE_CURSOR) {
                    531:                grid_view_get_cell(src->grid, src->cx, src->cy, &gc);
                    532:                gc.attr |= GRID_ATTR_REVERSE;
1.144     nicm      533:                screen_write_set_cursor(ctx, cx + (src->cx - px),
1.125     nicm      534:                    cy + (src->cy - py));
                    535:                screen_write_cell(ctx, &gc);
1.1       nicm      536:        }
                    537: }
                    538:
1.17      nicm      539: /* Set up context for TTY command. */
1.87      nicm      540: static void
                    541: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1       nicm      542: {
1.87      nicm      543:        struct screen   *s = ctx->s;
1.123     nicm      544:
                    545:        memset(ttyctx, 0, sizeof *ttyctx);
1.1       nicm      546:
1.17      nicm      547:        ttyctx->wp = ctx->wp;
1.1       nicm      548:
1.17      nicm      549:        ttyctx->ocx = s->cx;
                    550:        ttyctx->ocy = s->cy;
                    551:
                    552:        ttyctx->orlower = s->rlower;
                    553:        ttyctx->orupper = s->rupper;
1.87      nicm      554: }
1.31      nicm      555:
1.61      nicm      556: /* Set a mode. */
                    557: void
                    558: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
                    559: {
                    560:        struct screen   *s = ctx->s;
                    561:
                    562:        s->mode |= mode;
                    563: }
                    564:
                    565: /* Clear a mode. */
                    566: void
                    567: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
                    568: {
                    569:        struct screen   *s = ctx->s;
                    570:
                    571:        s->mode &= ~mode;
                    572: }
                    573:
1.1       nicm      574: /* Cursor up by ny. */
                    575: void
                    576: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
                    577: {
                    578:        struct screen   *s = ctx->s;
1.139     nicm      579:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      580:
                    581:        if (ny == 0)
                    582:                ny = 1;
                    583:
1.139     nicm      584:        if (cy < s->rupper) {
1.12      nicm      585:                /* Above region. */
1.139     nicm      586:                if (ny > cy)
                    587:                        ny = cy;
1.12      nicm      588:        } else {
                    589:                /* Below region. */
1.139     nicm      590:                if (ny > cy - s->rupper)
                    591:                        ny = cy - s->rupper;
1.12      nicm      592:        }
1.139     nicm      593:        if (cx == screen_size_x(s))
                    594:                cx--;
                    595:
                    596:        cy -= ny;
1.1       nicm      597:
1.139     nicm      598:        screen_write_set_cursor(ctx, cx, cy);
1.1       nicm      599: }
                    600:
                    601: /* Cursor down by ny. */
                    602: void
                    603: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
                    604: {
                    605:        struct screen   *s = ctx->s;
1.139     nicm      606:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      607:
                    608:        if (ny == 0)
                    609:                ny = 1;
                    610:
1.139     nicm      611:        if (cy > s->rlower) {
1.12      nicm      612:                /* Below region. */
1.139     nicm      613:                if (ny > screen_size_y(s) - 1 - cy)
                    614:                        ny = screen_size_y(s) - 1 - cy;
1.12      nicm      615:        } else {
                    616:                /* Above region. */
1.139     nicm      617:                if (ny > s->rlower - cy)
                    618:                        ny = s->rlower - cy;
1.12      nicm      619:        }
1.139     nicm      620:        if (cx == screen_size_x(s))
                    621:            cx--;
                    622:        else if (ny == 0)
1.1       nicm      623:                return;
                    624:
1.139     nicm      625:        cy += ny;
                    626:
                    627:        screen_write_set_cursor(ctx, cx, cy);
1.1       nicm      628: }
                    629:
1.101     nicm      630: /* Cursor right by nx. */
1.1       nicm      631: void
                    632: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
                    633: {
                    634:        struct screen   *s = ctx->s;
1.139     nicm      635:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      636:
                    637:        if (nx == 0)
                    638:                nx = 1;
                    639:
1.139     nicm      640:        if (nx > screen_size_x(s) - 1 - cx)
                    641:                nx = screen_size_x(s) - 1 - cx;
1.1       nicm      642:        if (nx == 0)
                    643:                return;
                    644:
1.139     nicm      645:        cx += nx;
                    646:
                    647:        screen_write_set_cursor(ctx, cx, cy);
1.1       nicm      648: }
                    649:
                    650: /* Cursor left by nx. */
                    651: void
                    652: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
                    653: {
                    654:        struct screen   *s = ctx->s;
1.139     nicm      655:        u_int            cx = s->cx, cy = s->cy;
1.1       nicm      656:
                    657:        if (nx == 0)
                    658:                nx = 1;
                    659:
1.139     nicm      660:        if (nx > cx)
                    661:                nx = cx;
1.1       nicm      662:        if (nx == 0)
                    663:                return;
                    664:
1.139     nicm      665:        cx -= nx;
                    666:
                    667:        screen_write_set_cursor(ctx, cx, cy);
1.5       nicm      668: }
                    669:
1.29      nicm      670: /* Backspace; cursor left unless at start of wrapped line when can move up. */
                    671: void
                    672: screen_write_backspace(struct screen_write_ctx *ctx)
                    673: {
                    674:        struct screen           *s = ctx->s;
                    675:        struct grid_line        *gl;
1.139     nicm      676:        u_int                    cx = s->cx, cy = s->cy;
1.29      nicm      677:
1.139     nicm      678:        if (cx == 0) {
                    679:                if (cy == 0)
1.29      nicm      680:                        return;
1.139     nicm      681:                gl = grid_get_line(s->grid, s->grid->hsize + cy - 1);
1.29      nicm      682:                if (gl->flags & GRID_LINE_WRAPPED) {
1.139     nicm      683:                        cy--;
                    684:                        cx = screen_size_x(s) - 1;
1.29      nicm      685:                }
                    686:        } else
1.139     nicm      687:                cx--;
                    688:
                    689:        screen_write_set_cursor(ctx, cx, cy);
1.29      nicm      690: }
                    691:
1.5       nicm      692: /* VT100 alignment test. */
                    693: void
                    694: screen_write_alignmenttest(struct screen_write_ctx *ctx)
                    695: {
                    696:        struct screen           *s = ctx->s;
1.17      nicm      697:        struct tty_ctx           ttyctx;
1.5       nicm      698:        struct grid_cell         gc;
                    699:        u_int                    xx, yy;
                    700:
                    701:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.77      nicm      702:        utf8_set(&gc.data, 'E');
1.7       ray       703:
1.5       nicm      704:        for (yy = 0; yy < screen_size_y(s); yy++) {
                    705:                for (xx = 0; xx < screen_size_x(s); xx++)
                    706:                        grid_view_set_cell(s->grid, xx, yy, &gc);
                    707:        }
1.7       ray       708:
1.139     nicm      709:        screen_write_set_cursor(ctx, 0, 0);
1.5       nicm      710:
                    711:        s->rupper = 0;
                    712:        s->rlower = screen_size_y(s) - 1;
1.143     nicm      713:
                    714:        screen_write_initctx(ctx, &ttyctx);
1.5       nicm      715:
1.109     nicm      716:        screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1.17      nicm      717:        tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1       nicm      718: }
                    719:
                    720: /* Insert nx characters. */
                    721: void
1.99      nicm      722: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1       nicm      723: {
                    724:        struct screen   *s = ctx->s;
1.17      nicm      725:        struct tty_ctx   ttyctx;
1.1       nicm      726:
                    727:        if (nx == 0)
                    728:                nx = 1;
                    729:
1.9       nicm      730:        if (nx > screen_size_x(s) - s->cx)
                    731:                nx = screen_size_x(s) - s->cx;
1.1       nicm      732:        if (nx == 0)
                    733:                return;
                    734:
1.107     nicm      735:        if (s->cx > screen_size_x(s) - 1)
                    736:                return;
                    737:
1.87      nicm      738:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      739:        ttyctx.bg = bg;
1.1       nicm      740:
1.107     nicm      741:        grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      742:
1.111     nicm      743:        screen_write_collect_flush(ctx, 0);
1.17      nicm      744:        ttyctx.num = nx;
                    745:        tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1       nicm      746: }
                    747:
                    748: /* Delete nx characters. */
                    749: void
1.99      nicm      750: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1       nicm      751: {
                    752:        struct screen   *s = ctx->s;
1.17      nicm      753:        struct tty_ctx   ttyctx;
1.1       nicm      754:
                    755:        if (nx == 0)
                    756:                nx = 1;
                    757:
1.9       nicm      758:        if (nx > screen_size_x(s) - s->cx)
                    759:                nx = screen_size_x(s) - s->cx;
1.1       nicm      760:        if (nx == 0)
                    761:                return;
                    762:
1.107     nicm      763:        if (s->cx > screen_size_x(s) - 1)
                    764:                return;
                    765:
1.87      nicm      766:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      767:        ttyctx.bg = bg;
1.1       nicm      768:
1.107     nicm      769:        grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      770:
1.111     nicm      771:        screen_write_collect_flush(ctx, 0);
1.17      nicm      772:        ttyctx.num = nx;
                    773:        tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59      nicm      774: }
                    775:
                    776: /* Clear nx characters. */
                    777: void
1.121     nicm      778: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.59      nicm      779: {
                    780:        struct screen   *s = ctx->s;
                    781:        struct tty_ctx   ttyctx;
                    782:
                    783:        if (nx == 0)
                    784:                nx = 1;
                    785:
                    786:        if (nx > screen_size_x(s) - s->cx)
                    787:                nx = screen_size_x(s) - s->cx;
                    788:        if (nx == 0)
                    789:                return;
                    790:
1.107     nicm      791:        if (s->cx > screen_size_x(s) - 1)
                    792:                return;
                    793:
1.87      nicm      794:        screen_write_initctx(ctx, &ttyctx);
1.121     nicm      795:        ttyctx.bg = bg;
1.59      nicm      796:
1.124     nicm      797:        grid_view_clear(s->grid, s->cx, s->cy, nx, 1, bg);
1.59      nicm      798:
1.111     nicm      799:        screen_write_collect_flush(ctx, 0);
1.59      nicm      800:        ttyctx.num = nx;
                    801:        tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1       nicm      802: }
                    803:
                    804: /* Insert ny lines. */
                    805: void
1.99      nicm      806: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      807: {
                    808:        struct screen   *s = ctx->s;
1.107     nicm      809:        struct grid     *gd = s->grid;
1.17      nicm      810:        struct tty_ctx   ttyctx;
1.1       nicm      811:
                    812:        if (ny == 0)
                    813:                ny = 1;
                    814:
1.11      nicm      815:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    816:                if (ny > screen_size_y(s) - s->cy)
                    817:                        ny = screen_size_y(s) - s->cy;
                    818:                if (ny == 0)
                    819:                        return;
                    820:
1.87      nicm      821:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      822:                ttyctx.bg = bg;
1.11      nicm      823:
1.107     nicm      824:                grid_view_insert_lines(gd, s->cy, ny, bg);
1.11      nicm      825:
1.111     nicm      826:                screen_write_collect_flush(ctx, 0);
1.17      nicm      827:                ttyctx.num = ny;
                    828:                tty_write(tty_cmd_insertline, &ttyctx);
1.11      nicm      829:                return;
                    830:        }
                    831:
                    832:        if (ny > s->rlower + 1 - s->cy)
                    833:                ny = s->rlower + 1 - s->cy;
1.1       nicm      834:        if (ny == 0)
                    835:                return;
1.41      nicm      836:
1.87      nicm      837:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      838:        ttyctx.bg = bg;
1.1       nicm      839:
                    840:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      841:                grid_view_insert_lines(gd, s->cy, ny, bg);
                    842:        else
                    843:                grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      844:
1.111     nicm      845:        screen_write_collect_flush(ctx, 0);
1.17      nicm      846:        ttyctx.num = ny;
                    847:        tty_write(tty_cmd_insertline, &ttyctx);
1.1       nicm      848: }
                    849:
                    850: /* Delete ny lines. */
                    851: void
1.99      nicm      852: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      853: {
                    854:        struct screen   *s = ctx->s;
1.107     nicm      855:        struct grid     *gd = s->grid;
1.17      nicm      856:        struct tty_ctx   ttyctx;
1.1       nicm      857:
                    858:        if (ny == 0)
                    859:                ny = 1;
                    860:
1.11      nicm      861:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    862:                if (ny > screen_size_y(s) - s->cy)
                    863:                        ny = screen_size_y(s) - s->cy;
                    864:                if (ny == 0)
                    865:                        return;
                    866:
1.87      nicm      867:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      868:                ttyctx.bg = bg;
1.11      nicm      869:
1.107     nicm      870:                grid_view_delete_lines(gd, s->cy, ny, bg);
1.11      nicm      871:
1.111     nicm      872:                screen_write_collect_flush(ctx, 0);
1.17      nicm      873:                ttyctx.num = ny;
                    874:                tty_write(tty_cmd_deleteline, &ttyctx);
1.11      nicm      875:                return;
                    876:        }
1.41      nicm      877:
1.11      nicm      878:        if (ny > s->rlower + 1 - s->cy)
                    879:                ny = s->rlower + 1 - s->cy;
1.1       nicm      880:        if (ny == 0)
                    881:                return;
                    882:
1.87      nicm      883:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      884:        ttyctx.bg = bg;
1.1       nicm      885:
                    886:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      887:                grid_view_delete_lines(gd, s->cy, ny, bg);
                    888:        else
                    889:                grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      890:
1.111     nicm      891:        screen_write_collect_flush(ctx, 0);
1.17      nicm      892:        ttyctx.num = ny;
                    893:        tty_write(tty_cmd_deleteline, &ttyctx);
1.1       nicm      894: }
                    895:
                    896: /* Clear line at cursor. */
                    897: void
1.99      nicm      898: screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      899: {
1.92      nicm      900:        struct screen           *s = ctx->s;
                    901:        struct grid_line        *gl;
                    902:        struct tty_ctx           ttyctx;
                    903:        u_int                    sx = screen_size_x(s);
1.1       nicm      904:
1.137     nicm      905:        gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140     nicm      906:        if (gl->cellsize == 0 && COLOUR_DEFAULT(bg))
1.92      nicm      907:                return;
1.1       nicm      908:
1.108     nicm      909:        screen_write_initctx(ctx, &ttyctx);
                    910:        ttyctx.bg = bg;
                    911:
1.99      nicm      912:        grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
                    913:
1.109     nicm      914:        screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      915:        screen_write_collect_flush(ctx, 0);
1.17      nicm      916:        tty_write(tty_cmd_clearline, &ttyctx);
1.1       nicm      917: }
                    918:
                    919: /* Clear to end of line from cursor. */
                    920: void
1.99      nicm      921: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      922: {
1.92      nicm      923:        struct screen           *s = ctx->s;
                    924:        struct grid_line        *gl;
                    925:        struct tty_ctx           ttyctx;
                    926:        u_int                    sx = screen_size_x(s);
1.1       nicm      927:
1.137     nicm      928:        gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.140     nicm      929:        if (s->cx > sx - 1 || (s->cx >= gl->cellsize && COLOUR_DEFAULT(bg)))
1.92      nicm      930:                return;
1.108     nicm      931:
                    932:        screen_write_initctx(ctx, &ttyctx);
                    933:        ttyctx.bg = bg;
1.1       nicm      934:
1.99      nicm      935:        grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
                    936:
1.109     nicm      937:        if (s->cx == 0)
                    938:                screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      939:        screen_write_collect_flush(ctx, 0);
1.41      nicm      940:        tty_write(tty_cmd_clearendofline, &ttyctx);
1.1       nicm      941: }
                    942:
                    943: /* Clear to start of line from cursor. */
                    944: void
1.99      nicm      945: screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      946: {
                    947:        struct screen   *s = ctx->s;
1.17      nicm      948:        struct tty_ctx   ttyctx;
1.92      nicm      949:        u_int            sx = screen_size_x(s);
1.1       nicm      950:
1.87      nicm      951:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm      952:        ttyctx.bg = bg;
1.1       nicm      953:
1.109     nicm      954:        if (s->cx > sx - 1)
1.99      nicm      955:                grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109     nicm      956:        else
1.99      nicm      957:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1       nicm      958:
1.109     nicm      959:        if (s->cx > sx - 1)
                    960:                screen_write_collect_clear(ctx, s->cy, 1);
1.114     nicm      961:        screen_write_collect_flush(ctx, 0);
1.17      nicm      962:        tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1       nicm      963: }
                    964:
1.101     nicm      965: /* Move cursor to px,py. */
1.1       nicm      966: void
1.147     nicm      967: screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py,
                    968:     int origin)
1.1       nicm      969: {
                    970:        struct screen   *s = ctx->s;
                    971:
1.147     nicm      972:        if (origin && py != -1 && (s->mode & MODE_ORIGIN)) {
1.146     nicm      973:                if ((u_int)py > s->rlower - s->rupper)
1.144     nicm      974:                        py = s->rlower;
                    975:                else
                    976:                        py += s->rupper;
                    977:        }
1.145     nicm      978:
1.146     nicm      979:        if (px != -1 && (u_int)px > screen_size_x(s) - 1)
1.145     nicm      980:                px = screen_size_x(s) - 1;
1.146     nicm      981:        if (py != -1 && (u_int)py > screen_size_y(s) - 1)
1.145     nicm      982:                py = screen_size_y(s) - 1;
1.1       nicm      983:
1.139     nicm      984:        screen_write_set_cursor(ctx, px, py);
1.1       nicm      985: }
                    986:
1.101     nicm      987: /* Reverse index (up with scroll). */
1.1       nicm      988: void
1.122     nicm      989: screen_write_reverseindex(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.1       nicm      993:
1.87      nicm      994:        screen_write_initctx(ctx, &ttyctx);
1.122     nicm      995:        ttyctx.bg = bg;
1.1       nicm      996:
1.109     nicm      997:        if (s->cy == s->rupper)
1.122     nicm      998:                grid_view_scroll_region_down(s->grid, s->rupper, s->rlower, bg);
1.109     nicm      999:        else if (s->cy > 0)
1.139     nicm     1000:                screen_write_set_cursor(ctx, -1, s->cy - 1);
1.1       nicm     1001:
1.111     nicm     1002:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1003:        tty_write(tty_cmd_reverseindex, &ttyctx);
1.1       nicm     1004: }
                   1005:
                   1006: /* Set scroll region. */
                   1007: void
1.83      nicm     1008: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
                   1009:     u_int rlower)
1.1       nicm     1010: {
                   1011:        struct screen   *s = ctx->s;
                   1012:
                   1013:        if (rupper > screen_size_y(s) - 1)
                   1014:                rupper = screen_size_y(s) - 1;
                   1015:        if (rlower > screen_size_y(s) - 1)
                   1016:                rlower = screen_size_y(s) - 1;
1.13      nicm     1017:        if (rupper >= rlower)   /* cannot be one line */
1.1       nicm     1018:                return;
                   1019:
1.111     nicm     1020:        screen_write_collect_flush(ctx, 0);
1.110     nicm     1021:
1.1       nicm     1022:        /* Cursor moves to top-left. */
1.139     nicm     1023:        screen_write_set_cursor(ctx, 0, 0);
1.1       nicm     1024:
                   1025:        s->rupper = rupper;
                   1026:        s->rlower = rlower;
                   1027: }
                   1028:
1.34      nicm     1029: /* Line feed. */
1.1       nicm     1030: void
1.122     nicm     1031: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped, u_int bg)
1.1       nicm     1032: {
1.20      nicm     1033:        struct screen           *s = ctx->s;
1.109     nicm     1034:        struct grid             *gd = s->grid;
1.20      nicm     1035:        struct grid_line        *gl;
1.1       nicm     1036:
1.137     nicm     1037:        gl = grid_get_line(gd, gd->hsize + s->cy);
1.20      nicm     1038:        if (wrapped)
                   1039:                gl->flags |= GRID_LINE_WRAPPED;
1.73      nicm     1040:        else
                   1041:                gl->flags &= ~GRID_LINE_WRAPPED;
1.20      nicm     1042:
1.114     nicm     1043:        log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
                   1044:            s->rupper, s->rlower);
                   1045:
1.122     nicm     1046:        if (bg != ctx->bg) {
                   1047:                screen_write_collect_flush(ctx, 1);
                   1048:                ctx->bg = bg;
                   1049:        }
                   1050:
1.92      nicm     1051:        if (s->cy == s->rlower) {
1.122     nicm     1052:                grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.109     nicm     1053:                screen_write_collect_scroll(ctx);
1.110     nicm     1054:                ctx->scrolled++;
1.109     nicm     1055:        } else if (s->cy < screen_size_y(s) - 1)
1.139     nicm     1056:                screen_write_set_cursor(ctx, -1, s->cy + 1);
1.1       nicm     1057: }
                   1058:
1.110     nicm     1059: /* Scroll up. */
                   1060: void
1.122     nicm     1061: screen_write_scrollup(struct screen_write_ctx *ctx, u_int lines, u_int bg)
1.110     nicm     1062: {
                   1063:        struct screen   *s = ctx->s;
                   1064:        struct grid     *gd = s->grid;
                   1065:        u_int            i;
                   1066:
                   1067:        if (lines == 0)
                   1068:                lines = 1;
                   1069:        else if (lines > s->rlower - s->rupper + 1)
                   1070:                lines = s->rlower - s->rupper + 1;
                   1071:
1.122     nicm     1072:        if (bg != ctx->bg) {
                   1073:                screen_write_collect_flush(ctx, 1);
                   1074:                ctx->bg = bg;
                   1075:        }
                   1076:
1.110     nicm     1077:        for (i = 0; i < lines; i++) {
1.122     nicm     1078:                grid_view_scroll_region_up(gd, s->rupper, s->rlower, bg);
1.110     nicm     1079:                screen_write_collect_scroll(ctx);
                   1080:        }
                   1081:        ctx->scrolled += lines;
                   1082: }
                   1083:
1.1       nicm     1084: /* Carriage return (cursor to start of line). */
                   1085: void
                   1086: screen_write_carriagereturn(struct screen_write_ctx *ctx)
                   1087: {
1.139     nicm     1088:        screen_write_set_cursor(ctx, 0, -1);
1.1       nicm     1089: }
                   1090:
                   1091: /* Clear to end of screen from cursor. */
                   1092: void
1.99      nicm     1093: screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1094: {
                   1095:        struct screen   *s = ctx->s;
1.107     nicm     1096:        struct grid     *gd = s->grid;
1.17      nicm     1097:        struct tty_ctx   ttyctx;
1.92      nicm     1098:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1099:
1.87      nicm     1100:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm     1101:        ttyctx.bg = bg;
1.1       nicm     1102:
1.46      nicm     1103:        /* Scroll into history if it is enabled and clearing entire screen. */
1.109     nicm     1104:        if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1.107     nicm     1105:                grid_view_clear_history(gd, bg);
1.109     nicm     1106:        else {
                   1107:                if (s->cx <= sx - 1)
1.107     nicm     1108:                        grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
                   1109:                grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1.46      nicm     1110:        }
1.1       nicm     1111:
1.109     nicm     1112:        screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
1.111     nicm     1113:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1114:        tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1       nicm     1115: }
                   1116:
                   1117: /* Clear to start of screen. */
                   1118: void
1.105     nicm     1119: screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1120: {
                   1121:        struct screen   *s = ctx->s;
1.17      nicm     1122:        struct tty_ctx   ttyctx;
1.92      nicm     1123:        u_int            sx = screen_size_x(s);
1.1       nicm     1124:
1.87      nicm     1125:        screen_write_initctx(ctx, &ttyctx);
1.105     nicm     1126:        ttyctx.bg = bg;
1.1       nicm     1127:
1.109     nicm     1128:        if (s->cy > 0)
1.105     nicm     1129:                grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1.109     nicm     1130:        if (s->cx > sx - 1)
1.120     nicm     1131:                grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109     nicm     1132:        else
1.120     nicm     1133:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1       nicm     1134:
1.109     nicm     1135:        screen_write_collect_clear(ctx, 0, s->cy);
1.111     nicm     1136:        screen_write_collect_flush(ctx, 0);
1.17      nicm     1137:        tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1       nicm     1138: }
                   1139:
                   1140: /* Clear entire screen. */
                   1141: void
1.99      nicm     1142: screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm     1143: {
                   1144:        struct screen   *s = ctx->s;
1.17      nicm     1145:        struct tty_ctx   ttyctx;
1.92      nicm     1146:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm     1147:
1.87      nicm     1148:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm     1149:        ttyctx.bg = bg;
1.1       nicm     1150:
1.46      nicm     1151:        /* Scroll into history if it is enabled. */
                   1152:        if (s->grid->flags & GRID_HISTORY)
1.99      nicm     1153:                grid_view_clear_history(s->grid, bg);
1.83      nicm     1154:        else
1.99      nicm     1155:                grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1.1       nicm     1156:
1.109     nicm     1157:        screen_write_collect_clear(ctx, 0, sy);
1.17      nicm     1158:        tty_write(tty_cmd_clearscreen, &ttyctx);
1.51      nicm     1159: }
                   1160:
                   1161: /* Clear entire history. */
                   1162: void
                   1163: screen_write_clearhistory(struct screen_write_ctx *ctx)
                   1164: {
                   1165:        struct screen   *s = ctx->s;
                   1166:        struct grid     *gd = s->grid;
                   1167:
1.99      nicm     1168:        grid_move_lines(gd, 0, gd->hsize, gd->sy, 8);
1.93      nicm     1169:        gd->hscrolled = gd->hsize = 0;
1.1       nicm     1170: }
                   1171:
1.109     nicm     1172: /* Clear a collected line. */
                   1173: static void
                   1174: screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
                   1175: {
                   1176:        struct screen_write_collect_item        *ci, *tmp;
                   1177:        u_int                                    i;
                   1178:        size_t                                   size;
                   1179:
1.151     nicm     1180:        for (i = y; i < y + n; i++) {
1.109     nicm     1181:                if (TAILQ_EMPTY(&ctx->list[i].items))
                   1182:                        continue;
                   1183:                size = 0;
                   1184:                TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
                   1185:                        size += ci->used;
                   1186:                        TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
                   1187:                        free(ci);
                   1188:                }
                   1189:                ctx->skipped += size;
1.114     nicm     1190:                log_debug("%s: dropped %zu bytes (line %u)", __func__, size, i);
1.109     nicm     1191:        }
                   1192: }
                   1193:
                   1194: /* Scroll collected lines up. */
                   1195: static void
                   1196: screen_write_collect_scroll(struct screen_write_ctx *ctx)
                   1197: {
                   1198:        struct screen                           *s = ctx->s;
                   1199:        struct screen_write_collect_line        *cl;
                   1200:        u_int                                    y;
                   1201:
1.114     nicm     1202:        log_debug("%s: at %u,%u (region %u-%u)", __func__, s->cx, s->cy,
                   1203:            s->rupper, s->rlower);
                   1204:
1.109     nicm     1205:        screen_write_collect_clear(ctx, s->rupper, 1);
                   1206:        for (y = s->rupper; y < s->rlower; y++) {
                   1207:                cl = &ctx->list[y + 1];
                   1208:                TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
                   1209:                TAILQ_INIT(&cl->items);
                   1210:        }
                   1211: }
                   1212:
                   1213: /* Flush collected lines. */
                   1214: static void
1.111     nicm     1215: screen_write_collect_flush(struct screen_write_ctx *ctx, int scroll_only)
1.109     nicm     1216: {
                   1217:        struct screen                           *s = ctx->s;
                   1218:        struct screen_write_collect_item        *ci, *tmp;
1.116     nicm     1219:        u_int                                    y, cx, cy, items = 0;
1.109     nicm     1220:        struct tty_ctx                           ttyctx;
1.116     nicm     1221:        size_t                                   written = 0;
1.110     nicm     1222:
                   1223:        if (ctx->scrolled != 0) {
                   1224:                log_debug("%s: scrolled %u (region %u-%u)", __func__,
                   1225:                    ctx->scrolled, s->rupper, s->rlower);
                   1226:                if (ctx->scrolled > s->rlower - s->rupper + 1)
                   1227:                        ctx->scrolled = s->rlower - s->rupper + 1;
                   1228:
                   1229:                screen_write_initctx(ctx, &ttyctx);
                   1230:                ttyctx.num = ctx->scrolled;
1.122     nicm     1231:                ttyctx.bg = ctx->bg;
1.110     nicm     1232:                tty_write(tty_cmd_scrollup, &ttyctx);
                   1233:        }
                   1234:        ctx->scrolled = 0;
1.122     nicm     1235:        ctx->bg = 8;
                   1236:
1.111     nicm     1237:        if (scroll_only)
                   1238:                return;
1.109     nicm     1239:
                   1240:        cx = s->cx; cy = s->cy;
                   1241:        for (y = 0; y < screen_size_y(s); y++) {
                   1242:                TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
1.144     nicm     1243:                        screen_write_set_cursor(ctx, ci->x, y);
1.109     nicm     1244:                        screen_write_initctx(ctx, &ttyctx);
                   1245:                        ttyctx.cell = &ci->gc;
1.118     nicm     1246:                        ttyctx.wrapped = ci->wrapped;
1.109     nicm     1247:                        ttyctx.ptr = ci->data;
                   1248:                        ttyctx.num = ci->used;
                   1249:                        tty_write(tty_cmd_cells, &ttyctx);
1.116     nicm     1250:
                   1251:                        items++;
                   1252:                        written += ci->used;
1.109     nicm     1253:
                   1254:                        TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
                   1255:                        free(ci);
                   1256:                }
                   1257:        }
                   1258:        s->cx = cx; s->cy = cy;
1.116     nicm     1259:
                   1260:        log_debug("%s: flushed %u items (%zu bytes)", __func__, items, written);
                   1261:        ctx->written += written;
1.109     nicm     1262: }
                   1263:
                   1264: /* Finish and store collected cells. */
                   1265: void
                   1266: screen_write_collect_end(struct screen_write_ctx *ctx)
                   1267: {
                   1268:        struct screen                           *s = ctx->s;
                   1269:        struct screen_write_collect_item        *ci = ctx->item;
                   1270:        struct grid_cell                         gc;
1.131     nicm     1271:        u_int                                    xx;
1.109     nicm     1272:
                   1273:        if (ci->used == 0)
                   1274:                return;
                   1275:        ci->data[ci->used] = '\0';
                   1276:
                   1277:        ci->x = s->cx;
                   1278:        TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
                   1279:        ctx->item = xcalloc(1, sizeof *ctx->item);
                   1280:
                   1281:        log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
                   1282:            s->cy);
                   1283:
1.131     nicm     1284:        if (s->cx != 0) {
                   1285:                for (xx = s->cx; xx > 0; xx--) {
                   1286:                        grid_view_get_cell(s->grid, xx, s->cy, &gc);
                   1287:                        if (~gc.flags & GRID_FLAG_PADDING)
                   1288:                                break;
1.136     nicm     1289:                        grid_view_set_cell(s->grid, xx, s->cy,
                   1290:                            &grid_default_cell);
1.131     nicm     1291:                }
1.139     nicm     1292:                if (gc.data.width > 1) {
1.136     nicm     1293:                        grid_view_set_cell(s->grid, xx, s->cy,
                   1294:                            &grid_default_cell);
1.139     nicm     1295:                }
1.131     nicm     1296:        }
                   1297:
1.109     nicm     1298:        memcpy(&gc, &ci->gc, sizeof gc);
                   1299:        grid_view_set_cells(s->grid, s->cx, s->cy, &gc, ci->data, ci->used);
1.139     nicm     1300:        screen_write_set_cursor(ctx, s->cx + ci->used, -1);
1.131     nicm     1301:
                   1302:        for (xx = s->cx; xx < screen_size_x(s); xx++) {
                   1303:                grid_view_get_cell(s->grid, xx, s->cy, &gc);
                   1304:                if (~gc.flags & GRID_FLAG_PADDING)
                   1305:                        break;
                   1306:                grid_view_set_cell(s->grid, xx, s->cy, &grid_default_cell);
                   1307:        }
1.109     nicm     1308: }
                   1309:
                   1310: /* Write cell data, collecting if necessary. */
                   1311: void
                   1312: screen_write_collect_add(struct screen_write_ctx *ctx,
                   1313:     const struct grid_cell *gc)
                   1314: {
                   1315:        struct screen                           *s = ctx->s;
                   1316:        struct screen_write_collect_item        *ci;
                   1317:        u_int                                    sx = screen_size_x(s);
                   1318:        int                                      collect;
                   1319:
                   1320:        /*
                   1321:         * Don't need to check that the attributes and whatnot are still the
1.116     nicm     1322:         * same - input_parse will end the collection when anything that isn't
                   1323:         * a plain character is encountered. Also nothing should make it here
                   1324:         * that isn't a single ASCII character.
1.109     nicm     1325:         */
                   1326:
                   1327:        collect = 1;
1.136     nicm     1328:        if (gc->data.width != 1 || gc->data.size != 1 || *gc->data.data >= 0x7f)
1.109     nicm     1329:                collect = 0;
                   1330:        else if (gc->attr & GRID_ATTR_CHARSET)
                   1331:                collect = 0;
                   1332:        else if (~s->mode & MODE_WRAP)
                   1333:                collect = 0;
                   1334:        else if (s->mode & MODE_INSERT)
                   1335:                collect = 0;
1.138     nicm     1336:        else if (s->sel != NULL)
1.109     nicm     1337:                collect = 0;
                   1338:        if (!collect) {
                   1339:                screen_write_collect_end(ctx);
1.116     nicm     1340:                screen_write_collect_flush(ctx, 0);
1.109     nicm     1341:                screen_write_cell(ctx, gc);
                   1342:                return;
                   1343:        }
                   1344:        ctx->cells++;
                   1345:
                   1346:        if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
                   1347:                screen_write_collect_end(ctx);
1.118     nicm     1348:        ci = ctx->item; /* may have changed */
                   1349:
1.109     nicm     1350:        if (s->cx > sx - 1) {
1.114     nicm     1351:                log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.118     nicm     1352:                ci->wrapped = 1;
1.122     nicm     1353:                screen_write_linefeed(ctx, 1, 8);
1.139     nicm     1354:                screen_write_set_cursor(ctx, 0, -1);
1.109     nicm     1355:        }
                   1356:
                   1357:        if (ci->used == 0)
                   1358:                memcpy(&ci->gc, gc, sizeof ci->gc);
                   1359:        ci->data[ci->used++] = gc->data.data[0];
                   1360:        if (ci->used == (sizeof ci->data) - 1)
                   1361:                screen_write_collect_end(ctx);
                   1362: }
                   1363:
1.1       nicm     1364: /* Write cell data. */
                   1365: void
1.60      nicm     1366: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1       nicm     1367: {
                   1368:        struct screen           *s = ctx->s;
                   1369:        struct grid             *gd = s->grid;
1.109     nicm     1370:        struct grid_line        *gl;
                   1371:        struct grid_cell_entry  *gce;
                   1372:        struct grid_cell         tmp_gc, now_gc;
1.15      nicm     1373:        struct tty_ctx           ttyctx;
1.92      nicm     1374:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.115     nicm     1375:        u_int                    width = gc->data.width, xx, last, cx, cy;
1.109     nicm     1376:        int                      selected, skip = 1;
1.1       nicm     1377:
1.109     nicm     1378:        /* Ignore padding cells. */
1.1       nicm     1379:        if (gc->flags & GRID_FLAG_PADDING)
                   1380:                return;
1.109     nicm     1381:        ctx->cells++;
1.32      nicm     1382:
1.109     nicm     1383:        /* If the width is zero, combine onto the previous character. */
1.1       nicm     1384:        if (width == 0) {
1.112     nicm     1385:                screen_write_collect_flush(ctx, 0);
1.104     nicm     1386:                if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
1.115     nicm     1387:                        cx = s->cx; cy = s->cy;
1.144     nicm     1388:                        screen_write_set_cursor(ctx, xx, s->cy);
1.87      nicm     1389:                        screen_write_initctx(ctx, &ttyctx);
1.104     nicm     1390:                        ttyctx.cell = gc;
                   1391:                        tty_write(tty_cmd_cell, &ttyctx);
1.115     nicm     1392:                        s->cx = cx; s->cy = cy;
1.1       nicm     1393:                }
                   1394:                return;
                   1395:        }
1.112     nicm     1396:
                   1397:        /* Flush any existing scrolling. */
                   1398:        screen_write_collect_flush(ctx, 1);
1.1       nicm     1399:
1.109     nicm     1400:        /* If this character doesn't fit, ignore it. */
                   1401:        if ((~s->mode & MODE_WRAP) &&
                   1402:            width > 1 &&
                   1403:            (width > sx || (s->cx != sx && s->cx > sx - width)))
                   1404:                return;
                   1405:
1.6       nicm     1406:        /* If in insert mode, make space for the cells. */
1.98      nicm     1407:        if (s->mode & MODE_INSERT) {
1.109     nicm     1408:                grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
                   1409:                skip = 0;
                   1410:        }
1.6       nicm     1411:
1.20      nicm     1412:        /* Check this will fit on the current line and wrap if not. */
1.92      nicm     1413:        if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.128     nicm     1414:                log_debug("%s: wrapped at %u,%u", __func__, s->cx, s->cy);
1.122     nicm     1415:                screen_write_linefeed(ctx, 1, 8);
1.139     nicm     1416:                screen_write_set_cursor(ctx, 0, -1);
1.128     nicm     1417:                screen_write_collect_flush(ctx, 1);
1.1       nicm     1418:        }
                   1419:
1.64      nicm     1420:        /* Sanity check cursor position. */
1.92      nicm     1421:        if (s->cx > sx - width || s->cy > sy - 1)
1.1       nicm     1422:                return;
1.106     nicm     1423:        screen_write_initctx(ctx, &ttyctx);
                   1424:
1.1       nicm     1425:        /* Handle overwriting of UTF-8 characters. */
1.137     nicm     1426:        gl = grid_get_line(s->grid, s->grid->hsize + s->cy);
1.92      nicm     1427:        if (gl->flags & GRID_LINE_EXTENDED) {
                   1428:                grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
                   1429:                if (screen_write_overwrite(ctx, &now_gc, width))
                   1430:                        skip = 0;
                   1431:        }
1.1       nicm     1432:
                   1433:        /*
                   1434:         * If the new character is UTF-8 wide, fill in padding cells. Have
                   1435:         * already ensured there is enough room.
                   1436:         */
1.89      nicm     1437:        for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.131     nicm     1438:                log_debug("%s: new padding at %u,%u", __func__, xx, s->cy);
1.88      nicm     1439:                grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89      nicm     1440:                skip = 0;
                   1441:        }
                   1442:
                   1443:        /* If no change, do not draw. */
1.92      nicm     1444:        if (skip) {
                   1445:                if (s->cx >= gl->cellsize)
                   1446:                        skip = grid_cells_equal(gc, &grid_default_cell);
                   1447:                else {
                   1448:                        gce = &gl->celldata[s->cx];
                   1449:                        if (gce->flags & GRID_FLAG_EXTENDED)
                   1450:                                skip = 0;
1.95      nicm     1451:                        else if (gc->flags != gce->flags)
1.92      nicm     1452:                                skip = 0;
                   1453:                        else if (gc->attr != gce->data.attr)
                   1454:                                skip = 0;
                   1455:                        else if (gc->fg != gce->data.fg)
                   1456:                                skip = 0;
                   1457:                        else if (gc->bg != gce->data.bg)
                   1458:                                skip = 0;
1.95      nicm     1459:                        else if (gc->data.width != 1)
                   1460:                                skip = 0;
1.109     nicm     1461:                        else if (gc->data.size != 1)
                   1462:                                skip = 0;
1.95      nicm     1463:                        else if (gce->data.data != gc->data.data[0])
1.92      nicm     1464:                                skip = 0;
                   1465:                }
                   1466:        }
1.1       nicm     1467:
1.127     nicm     1468:        /* Update the selected flag and set the cell. */
1.90      nicm     1469:        selected = screen_check_selection(s, s->cx, s->cy);
1.109     nicm     1470:        if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1471:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1472:                tmp_gc.flags |= GRID_FLAG_SELECTED;
                   1473:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109     nicm     1474:        } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1475:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1476:                tmp_gc.flags &= ~GRID_FLAG_SELECTED;
                   1477:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1478:        } else if (!skip)
1.89      nicm     1479:                grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109     nicm     1480:        if (selected)
                   1481:                skip = 0;
1.1       nicm     1482:
1.64      nicm     1483:        /*
                   1484:         * Move the cursor. If not wrapping, stick at the last character and
                   1485:         * replace it.
                   1486:         */
1.65      nicm     1487:        last = !(s->mode & MODE_WRAP);
1.92      nicm     1488:        if (s->cx <= sx - last - width)
1.139     nicm     1489:                screen_write_set_cursor(ctx, s->cx + width, -1);
1.64      nicm     1490:        else
1.139     nicm     1491:                screen_write_set_cursor(ctx,  sx - last, -1);
1.1       nicm     1492:
1.89      nicm     1493:        /* Create space for character in insert mode. */
1.109     nicm     1494:        if (s->mode & MODE_INSERT) {
1.113     nicm     1495:                screen_write_collect_flush(ctx, 0);
1.17      nicm     1496:                ttyctx.num = width;
                   1497:                tty_write(tty_cmd_insertcharacter, &ttyctx);
                   1498:        }
1.89      nicm     1499:
                   1500:        /* Write to the screen. */
1.109     nicm     1501:        if (!skip) {
                   1502:                if (selected) {
                   1503:                        screen_select_cell(s, &tmp_gc, gc);
                   1504:                        ttyctx.cell = &tmp_gc;
                   1505:                } else
                   1506:                        ttyctx.cell = gc;
1.16      nicm     1507:                tty_write(tty_cmd_cell, &ttyctx);
1.92      nicm     1508:                ctx->written++;
                   1509:        } else
                   1510:                ctx->skipped++;
1.35      nicm     1511: }
                   1512:
                   1513: /* Combine a UTF-8 zero-width character onto the previous. */
1.104     nicm     1514: static const struct grid_cell *
                   1515: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
                   1516:     u_int *xx)
1.35      nicm     1517: {
                   1518:        struct screen           *s = ctx->s;
                   1519:        struct grid             *gd = s->grid;
1.104     nicm     1520:        static struct grid_cell  gc;
                   1521:        u_int                    n;
1.35      nicm     1522:
                   1523:        /* Can't combine if at 0. */
                   1524:        if (s->cx == 0)
1.104     nicm     1525:                return (NULL);
1.35      nicm     1526:
1.60      nicm     1527:        /* Empty data is out. */
                   1528:        if (ud->size == 0)
1.37      nicm     1529:                fatalx("UTF-8 data empty");
                   1530:
1.60      nicm     1531:        /* Retrieve the previous cell. */
1.119     nicm     1532:        for (n = 1; n <= s->cx; n++) {
1.104     nicm     1533:                grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
                   1534:                if (~gc.flags & GRID_FLAG_PADDING)
                   1535:                        break;
                   1536:        }
1.119     nicm     1537:        if (n > s->cx)
1.104     nicm     1538:                return (NULL);
                   1539:        *xx = s->cx - n;
1.35      nicm     1540:
1.60      nicm     1541:        /* Check there is enough space. */
1.77      nicm     1542:        if (gc.data.size + ud->size > sizeof gc.data.data)
1.104     nicm     1543:                return (NULL);
                   1544:
                   1545:        log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
                   1546:            ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35      nicm     1547:
1.77      nicm     1548:        /* Append the data. */
                   1549:        memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
                   1550:        gc.data.size += ud->size;
                   1551:
                   1552:        /* Set the new cell. */
1.104     nicm     1553:        grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35      nicm     1554:
1.104     nicm     1555:        return (&gc);
1.1       nicm     1556: }
                   1557:
                   1558: /*
                   1559:  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
                   1560:  * cell on the screen, so following cells must not be drawn by marking them as
                   1561:  * padding.
                   1562:  *
                   1563:  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
                   1564:  * character, it is necessary to also overwrite any other cells which covered
                   1565:  * by the same character.
                   1566:  */
1.89      nicm     1567: static int
                   1568: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
                   1569:     u_int width)
1.1       nicm     1570: {
                   1571:        struct screen           *s = ctx->s;
                   1572:        struct grid             *gd = s->grid;
1.89      nicm     1573:        struct grid_cell         tmp_gc;
1.1       nicm     1574:        u_int                    xx;
1.89      nicm     1575:        int                      done = 0;
1.1       nicm     1576:
1.89      nicm     1577:        if (gc->flags & GRID_FLAG_PADDING) {
1.1       nicm     1578:                /*
                   1579:                 * A padding cell, so clear any following and leading padding
                   1580:                 * cells back to the character. Don't overwrite the current
                   1581:                 * cell as that happens later anyway.
                   1582:                 */
                   1583:                xx = s->cx + 1;
                   1584:                while (--xx > 0) {
1.89      nicm     1585:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1586:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1       nicm     1587:                                break;
1.131     nicm     1588:                        log_debug("%s: padding at %u,%u", __func__, xx, s->cy);
1.1       nicm     1589:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1590:                }
                   1591:
                   1592:                /* Overwrite the character at the start of this padding. */
1.131     nicm     1593:                log_debug("%s: character at %u,%u", __func__, xx, s->cy);
1.1       nicm     1594:                grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89      nicm     1595:                done = 1;
1.43      nicm     1596:        }
1.1       nicm     1597:
1.43      nicm     1598:        /*
1.95      nicm     1599:         * Overwrite any padding cells that belong to any UTF-8 characters
                   1600:         * we'll be overwriting with the current character.
1.43      nicm     1601:         */
1.95      nicm     1602:        if (width != 1 ||
                   1603:            gc->data.width != 1 ||
                   1604:            gc->flags & GRID_FLAG_PADDING) {
1.89      nicm     1605:                xx = s->cx + width - 1;
                   1606:                while (++xx < screen_size_x(s)) {
                   1607:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1608:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
                   1609:                                break;
1.131     nicm     1610:                        log_debug("%s: overwrite at %u,%u", __func__, xx, s->cy);
1.89      nicm     1611:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1612:                        done = 1;
                   1613:                }
1.1       nicm     1614:        }
1.89      nicm     1615:
                   1616:        return (done);
1.50      nicm     1617: }
                   1618:
1.107     nicm     1619: /* Set external clipboard. */
1.50      nicm     1620: void
                   1621: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1622: {
                   1623:        struct tty_ctx  ttyctx;
                   1624:
1.87      nicm     1625:        screen_write_initctx(ctx, &ttyctx);
1.50      nicm     1626:        ttyctx.ptr = str;
                   1627:        ttyctx.num = len;
                   1628:
                   1629:        tty_write(tty_cmd_setselection, &ttyctx);
1.47      nicm     1630: }
                   1631:
1.107     nicm     1632: /* Write unmodified string. */
1.47      nicm     1633: void
                   1634: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1635: {
1.87      nicm     1636:        struct tty_ctx  ttyctx;
1.47      nicm     1637:
1.87      nicm     1638:        screen_write_initctx(ctx, &ttyctx);
1.47      nicm     1639:        ttyctx.ptr = str;
                   1640:        ttyctx.num = len;
                   1641:
                   1642:        tty_write(tty_cmd_rawstring, &ttyctx);
1.1       nicm     1643: }