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

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