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

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