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

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