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

1.109   ! nicm        1: /* $OpenBSD: screen-write.c,v 1.108 2017/02/08 15:49:29 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.84      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
1.56      nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
1.87      nicm       26: static void    screen_write_initctx(struct screen_write_ctx *,
                     27:                    struct tty_ctx *);
1.109   ! nicm       28: static void    screen_write_collect_clear(struct screen_write_ctx *, u_int,
        !            29:                    u_int);
        !            30: static void    screen_write_collect_scroll(struct screen_write_ctx *);
        !            31: static void    screen_write_collect_flush(struct screen_write_ctx *);
1.87      nicm       32:
1.89      nicm       33: static int     screen_write_overwrite(struct screen_write_ctx *,
                     34:                    struct grid_cell *, u_int);
1.104     nicm       35: static const struct grid_cell *screen_write_combine(struct screen_write_ctx *,
                     36:                    const struct utf8_data *, u_int *);
1.1       nicm       37:
1.88      nicm       38: static const struct grid_cell screen_write_pad_cell = {
1.91      nicm       39:        GRID_FLAG_PADDING, 0, 8, 8, { { 0 }, 0, 0, 0 }
1.88      nicm       40: };
                     41:
1.109   ! nicm       42: struct screen_write_collect_item {
        !            43:        u_int                    x;
        !            44:
        !            45:        u_int                    used;
        !            46:        char                     data[256];
        !            47:
        !            48:        struct grid_cell         gc;
        !            49:
        !            50:        TAILQ_ENTRY (screen_write_collect_item) entry;
        !            51: };
        !            52: struct screen_write_collect_line {
        !            53:        TAILQ_HEAD(, screen_write_collect_item) items;
        !            54: };
1.92      nicm       55:
                     56: /* Initialize writing with a window. */
1.1       nicm       57: void
1.71      nicm       58: screen_write_start(struct screen_write_ctx *ctx, struct window_pane *wp,
                     59:     struct screen *s)
1.1       nicm       60: {
1.108     nicm       61:        char    tmp[16];
1.109   ! nicm       62:        u_int   y;
        !            63:
        !            64:        memset(ctx, 0, sizeof *ctx);
1.92      nicm       65:
1.1       nicm       66:        ctx->wp = wp;
                     67:        if (wp != NULL && s == NULL)
                     68:                ctx->s = wp->screen;
                     69:        else
                     70:                ctx->s = s;
1.92      nicm       71:
1.109   ! nicm       72:        ctx->list = xcalloc(screen_size_y(ctx->s), sizeof *ctx->list);
        !            73:        for (y = 0; y < screen_size_y(ctx->s); y++)
        !            74:                TAILQ_INIT(&ctx->list[y].items);
        !            75:        ctx->item = xcalloc(1, sizeof *ctx->item);
1.92      nicm       76:
1.108     nicm       77:        if (wp != NULL)
1.92      nicm       78:                snprintf(tmp, sizeof tmp, "pane %%%u", wp->id);
                     79:        log_debug("%s: size %ux%u, %s", __func__, screen_size_x(ctx->s),
1.108     nicm       80:            screen_size_y(ctx->s), wp == NULL ? "no pane" : tmp);
1.1       nicm       81: }
                     82:
                     83: /* Finish writing. */
                     84: void
1.92      nicm       85: screen_write_stop(struct screen_write_ctx *ctx)
                     86: {
1.109   ! nicm       87:        screen_write_collect_end(ctx);
        !            88:        screen_write_collect_flush(ctx);
1.92      nicm       89:
1.109   ! nicm       90:        log_debug("%s: %u cells (%u written, %u skipped)", __func__,
        !            91:            ctx->cells, ctx->written, ctx->skipped);
1.92      nicm       92:
1.109   ! nicm       93:        free(ctx->item);
        !            94:        free(ctx->list); /* flush will have emptied */
1.52      nicm       95: }
                     96:
                     97: /* Reset screen state. */
                     98: void
                     99: screen_write_reset(struct screen_write_ctx *ctx)
                    100: {
1.61      nicm      101:        struct screen   *s = ctx->s;
1.52      nicm      102:
1.61      nicm      103:        screen_reset_tabs(s);
                    104:        screen_write_scrollregion(ctx, 0, screen_size_y(s) - 1);
1.63      nicm      105:
1.67      nicm      106:        s->mode &= ~(MODE_INSERT|MODE_KCURSOR|MODE_KKEYPAD|MODE_FOCUSON);
1.82      nicm      107:        s->mode &= ~(ALL_MOUSE_MODES|MODE_MOUSE_UTF8|MODE_MOUSE_SGR);
1.52      nicm      108:
1.99      nicm      109:        screen_write_clearscreen(ctx, 8);
1.52      nicm      110:        screen_write_cursormove(ctx, 0, 0);
1.1       nicm      111: }
                    112:
                    113: /* Write character. */
                    114: void
1.86      nicm      115: screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.69      nicm      116:     u_char ch)
1.1       nicm      117: {
1.86      nicm      118:        struct grid_cell        gc;
                    119:
                    120:        memcpy(&gc, gcp, sizeof gc);
                    121:
                    122:        utf8_set(&gc.data, ch);
                    123:        screen_write_cell(ctx, &gc);
1.1       nicm      124: }
                    125:
1.24      nicm      126: /* Calculate string length, with embedded formatting. */
1.71      nicm      127: size_t
1.75      nicm      128: screen_write_cstrlen(const char *fmt, ...)
1.24      nicm      129: {
                    130:        va_list ap;
                    131:        char   *msg, *msg2, *ptr, *ptr2;
                    132:        size_t  size;
                    133:
                    134:        va_start(ap, fmt);
                    135:        xvasprintf(&msg, fmt, ap);
                    136:        va_end(ap);
                    137:        msg2 = xmalloc(strlen(msg) + 1);
                    138:
                    139:        ptr = msg;
                    140:        ptr2 = msg2;
                    141:        while (*ptr != '\0') {
                    142:                if (ptr[0] == '#' && ptr[1] == '[') {
                    143:                        while (*ptr != ']' && *ptr != '\0')
                    144:                                ptr++;
                    145:                        if (*ptr == ']')
                    146:                                ptr++;
                    147:                        continue;
                    148:                }
                    149:                *ptr2++ = *ptr++;
                    150:        }
                    151:        *ptr2 = '\0';
                    152:
1.75      nicm      153:        size = screen_write_strlen("%s", msg2);
1.24      nicm      154:
1.56      nicm      155:        free(msg);
                    156:        free(msg2);
1.24      nicm      157:
                    158:        return (size);
                    159: }
                    160:
1.2       nicm      161: /* Calculate string length. */
1.71      nicm      162: size_t
1.75      nicm      163: screen_write_strlen(const char *fmt, ...)
1.2       nicm      164: {
1.36      nicm      165:        va_list                 ap;
                    166:        char                   *msg;
1.76      nicm      167:        struct utf8_data        ud;
1.36      nicm      168:        u_char                 *ptr;
                    169:        size_t                  left, size = 0;
1.79      nicm      170:        enum utf8_state         more;
1.2       nicm      171:
                    172:        va_start(ap, fmt);
                    173:        xvasprintf(&msg, fmt, ap);
                    174:        va_end(ap);
                    175:
                    176:        ptr = msg;
                    177:        while (*ptr != '\0') {
1.79      nicm      178:                if (*ptr > 0x7f && utf8_open(&ud, *ptr) == UTF8_MORE) {
1.36      nicm      179:                        ptr++;
1.2       nicm      180:
                    181:                        left = strlen(ptr);
1.77      nicm      182:                        if (left < (size_t)ud.size - 1)
1.36      nicm      183:                                break;
1.79      nicm      184:                        while ((more = utf8_append(&ud, *ptr)) == UTF8_MORE)
1.2       nicm      185:                                ptr++;
1.36      nicm      186:                        ptr++;
                    187:
1.79      nicm      188:                        if (more == UTF8_DONE)
1.78      nicm      189:                                size += ud.width;
1.2       nicm      190:                } else {
1.75      nicm      191:                        if (*ptr > 0x1f && *ptr < 0x7f)
                    192:                                size++;
1.2       nicm      193:                        ptr++;
                    194:                }
1.7       ray       195:        }
1.2       nicm      196:
1.56      nicm      197:        free(msg);
1.2       nicm      198:        return (size);
                    199: }
                    200:
1.3       nicm      201: /* Write simple string (no UTF-8 or maximum length). */
1.71      nicm      202: void
1.86      nicm      203: screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp,
1.71      nicm      204:     const char *fmt, ...)
1.1       nicm      205: {
                    206:        va_list ap;
                    207:
                    208:        va_start(ap, fmt);
1.86      nicm      209:        screen_write_vnputs(ctx, -1, gcp, fmt, ap);
1.2       nicm      210:        va_end(ap);
                    211: }
                    212:
                    213: /* Write string with length limit (-1 for unlimited). */
1.71      nicm      214: void
                    215: screen_write_nputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      216:     const struct grid_cell *gcp, const char *fmt, ...)
1.2       nicm      217: {
                    218:        va_list ap;
                    219:
                    220:        va_start(ap, fmt);
1.86      nicm      221:        screen_write_vnputs(ctx, maxlen, gcp, fmt, ap);
1.2       nicm      222:        va_end(ap);
                    223: }
                    224:
                    225: void
1.3       nicm      226: screen_write_vnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      227:     const struct grid_cell *gcp, const char *fmt, va_list ap)
1.2       nicm      228: {
1.86      nicm      229:        struct grid_cell        gc;
                    230:        struct utf8_data       *ud = &gc.data;
1.36      nicm      231:        char                   *msg;
                    232:        u_char                 *ptr;
                    233:        size_t                  left, size = 0;
1.79      nicm      234:        enum utf8_state         more;
1.2       nicm      235:
1.86      nicm      236:        memcpy(&gc, gcp, sizeof gc);
1.1       nicm      237:        xvasprintf(&msg, fmt, ap);
                    238:
1.2       nicm      239:        ptr = msg;
                    240:        while (*ptr != '\0') {
1.86      nicm      241:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      242:                        ptr++;
1.2       nicm      243:
                    244:                        left = strlen(ptr);
1.86      nicm      245:                        if (left < (size_t)ud->size - 1)
1.36      nicm      246:                                break;
1.86      nicm      247:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.2       nicm      248:                                ptr++;
1.36      nicm      249:                        ptr++;
1.7       ray       250:
1.86      nicm      251:                        if (more != UTF8_DONE)
                    252:                                continue;
                    253:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    254:                                while (size < (size_t)maxlen) {
                    255:                                        screen_write_putc(ctx, &gc, ' ');
                    256:                                        size++;
1.2       nicm      257:                                }
1.86      nicm      258:                                break;
1.2       nicm      259:                        }
1.86      nicm      260:                        size += ud->width;
                    261:                        screen_write_cell(ctx, &gc);
1.2       nicm      262:                } else {
1.86      nicm      263:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.2       nicm      264:                                break;
                    265:
1.57      nicm      266:                        if (*ptr == '\001')
1.86      nicm      267:                                gc.attr ^= GRID_ATTR_CHARSET;
1.75      nicm      268:                        else if (*ptr > 0x1f && *ptr < 0x7f) {
1.57      nicm      269:                                size++;
1.86      nicm      270:                                screen_write_putc(ctx, &gc, *ptr);
1.57      nicm      271:                        }
1.2       nicm      272:                        ptr++;
                    273:                }
                    274:        }
1.1       nicm      275:
1.56      nicm      276:        free(msg);
1.24      nicm      277: }
                    278:
                    279: /* Write string, similar to nputs, but with embedded formatting (#[]). */
1.71      nicm      280: void
1.75      nicm      281: screen_write_cnputs(struct screen_write_ctx *ctx, ssize_t maxlen,
1.86      nicm      282:     const struct grid_cell *gcp, const char *fmt, ...)
1.24      nicm      283: {
1.86      nicm      284:        struct grid_cell         gc;
                    285:        struct utf8_data        *ud = &gc.data;
1.24      nicm      286:        va_list                  ap;
                    287:        char                    *msg;
1.36      nicm      288:        u_char                  *ptr, *last;
1.24      nicm      289:        size_t                   left, size = 0;
1.79      nicm      290:        enum utf8_state          more;
1.24      nicm      291:
1.86      nicm      292:        memcpy(&gc, gcp, sizeof gc);
                    293:
1.24      nicm      294:        va_start(ap, fmt);
                    295:        xvasprintf(&msg, fmt, ap);
                    296:        va_end(ap);
                    297:
                    298:        ptr = msg;
                    299:        while (*ptr != '\0') {
                    300:                if (ptr[0] == '#' && ptr[1] == '[') {
                    301:                        ptr += 2;
                    302:                        last = ptr + strcspn(ptr, "]");
                    303:                        if (*last == '\0') {
                    304:                                /* No ]. Not much point in doing anything. */
                    305:                                break;
                    306:                        }
                    307:                        *last = '\0';
                    308:
1.86      nicm      309:                        style_parse(gcp, &gc, ptr);
1.24      nicm      310:                        ptr = last + 1;
                    311:                        continue;
                    312:                }
                    313:
1.86      nicm      314:                if (*ptr > 0x7f && utf8_open(ud, *ptr) == UTF8_MORE) {
1.36      nicm      315:                        ptr++;
1.24      nicm      316:
                    317:                        left = strlen(ptr);
1.86      nicm      318:                        if (left < (size_t)ud->size - 1)
1.36      nicm      319:                                break;
1.86      nicm      320:                        while ((more = utf8_append(ud, *ptr)) == UTF8_MORE)
1.24      nicm      321:                                ptr++;
1.36      nicm      322:                        ptr++;
1.24      nicm      323:
1.86      nicm      324:                        if (more != UTF8_DONE)
                    325:                                continue;
                    326:                        if (maxlen > 0 && size + ud->width > (size_t)maxlen) {
                    327:                                while (size < (size_t)maxlen) {
                    328:                                        screen_write_putc(ctx, &gc, ' ');
                    329:                                        size++;
1.24      nicm      330:                                }
1.86      nicm      331:                                break;
1.24      nicm      332:                        }
1.86      nicm      333:                        size += ud->width;
                    334:                        screen_write_cell(ctx, &gc);
1.24      nicm      335:                } else {
1.86      nicm      336:                        if (maxlen > 0 && size + 1 > (size_t)maxlen)
1.24      nicm      337:                                break;
                    338:
1.75      nicm      339:                        if (*ptr > 0x1f && *ptr < 0x7f) {
                    340:                                size++;
1.86      nicm      341:                                screen_write_putc(ctx, &gc, *ptr);
1.75      nicm      342:                        }
1.24      nicm      343:                        ptr++;
                    344:                }
                    345:        }
                    346:
1.56      nicm      347:        free(msg);
1.1       nicm      348: }
                    349:
                    350: /* Copy from another screen. */
                    351: void
1.95      nicm      352: screen_write_copy(struct screen_write_ctx *ctx, struct screen *src, u_int px,
1.102     nicm      353:     u_int py, u_int nx, u_int ny, bitstr_t *markbs,
                    354:     const struct grid_cell *markgc)
1.1       nicm      355: {
                    356:        struct screen           *s = ctx->s;
                    357:        struct grid             *gd = src->grid;
1.77      nicm      358:        struct grid_cell         gc;
1.102     nicm      359:        u_int                    xx, yy, cx, cy, b;
1.1       nicm      360:
                    361:        cx = s->cx;
                    362:        cy = s->cy;
1.96      nicm      363:
1.1       nicm      364:        for (yy = py; yy < py + ny; yy++) {
1.96      nicm      365:                for (xx = px; xx < px + nx; xx++) {
                    366:                        grid_get_cell(gd, xx, yy, &gc);
1.102     nicm      367:                        if (markbs != NULL) {
                    368:                                b = (yy * screen_size_x(src)) + xx;
                    369:                                if (bit_test(markbs, b)) {
                    370:                                        gc.attr = markgc->attr;
                    371:                                        gc.fg = markgc->fg;
                    372:                                        gc.bg = markgc->bg;
                    373:                                }
                    374:                        }
1.96      nicm      375:                        screen_write_cell(ctx, &gc);
                    376:                }
                    377:
1.1       nicm      378:                cy++;
                    379:                screen_write_cursormove(ctx, cx, cy);
                    380:        }
                    381: }
                    382:
1.17      nicm      383: /* Set up context for TTY command. */
1.87      nicm      384: static void
                    385: screen_write_initctx(struct screen_write_ctx *ctx, struct tty_ctx *ttyctx)
1.1       nicm      386: {
1.87      nicm      387:        struct screen   *s = ctx->s;
1.1       nicm      388:
1.17      nicm      389:        ttyctx->wp = ctx->wp;
1.1       nicm      390:
1.17      nicm      391:        ttyctx->ocx = s->cx;
                    392:        ttyctx->ocy = s->cy;
                    393:
                    394:        ttyctx->orlower = s->rlower;
                    395:        ttyctx->orupper = s->rupper;
1.87      nicm      396: }
1.31      nicm      397:
1.61      nicm      398: /* Set a mode. */
                    399: void
                    400: screen_write_mode_set(struct screen_write_ctx *ctx, int mode)
                    401: {
                    402:        struct screen   *s = ctx->s;
                    403:
                    404:        s->mode |= mode;
                    405: }
                    406:
                    407: /* Clear a mode. */
                    408: void
                    409: screen_write_mode_clear(struct screen_write_ctx *ctx, int mode)
                    410: {
                    411:        struct screen   *s = ctx->s;
                    412:
                    413:        s->mode &= ~mode;
                    414: }
                    415:
1.1       nicm      416: /* Cursor up by ny. */
                    417: void
                    418: screen_write_cursorup(struct screen_write_ctx *ctx, u_int ny)
                    419: {
                    420:        struct screen   *s = ctx->s;
                    421:
                    422:        if (ny == 0)
                    423:                ny = 1;
                    424:
1.12      nicm      425:        if (s->cy < s->rupper) {
                    426:                /* Above region. */
                    427:                if (ny > s->cy)
                    428:                        ny = s->cy;
                    429:        } else {
                    430:                /* Below region. */
                    431:                if (ny > s->cy - s->rupper)
                    432:                        ny = s->cy - s->rupper;
                    433:        }
1.66      nicm      434:        if (s->cx == screen_size_x(s))
                    435:            s->cx--;
1.1       nicm      436:        if (ny == 0)
                    437:                return;
                    438:
                    439:        s->cy -= ny;
                    440: }
                    441:
                    442: /* Cursor down by ny. */
                    443: void
                    444: screen_write_cursordown(struct screen_write_ctx *ctx, u_int ny)
                    445: {
                    446:        struct screen   *s = ctx->s;
                    447:
                    448:        if (ny == 0)
                    449:                ny = 1;
                    450:
1.12      nicm      451:        if (s->cy > s->rlower) {
                    452:                /* Below region. */
                    453:                if (ny > screen_size_y(s) - 1 - s->cy)
                    454:                        ny = screen_size_y(s) - 1 - s->cy;
                    455:        } else {
                    456:                /* Above region. */
                    457:                if (ny > s->rlower - s->cy)
                    458:                        ny = s->rlower - s->cy;
                    459:        }
1.66      nicm      460:        if (s->cx == screen_size_x(s))
                    461:            s->cx--;
1.1       nicm      462:        if (ny == 0)
                    463:                return;
                    464:
                    465:        s->cy += ny;
                    466: }
                    467:
1.101     nicm      468: /* Cursor right by nx. */
1.1       nicm      469: void
                    470: screen_write_cursorright(struct screen_write_ctx *ctx, u_int nx)
                    471: {
                    472:        struct screen   *s = ctx->s;
                    473:
                    474:        if (nx == 0)
                    475:                nx = 1;
                    476:
                    477:        if (nx > screen_size_x(s) - 1 - s->cx)
                    478:                nx = screen_size_x(s) - 1 - s->cx;
                    479:        if (nx == 0)
                    480:                return;
                    481:
                    482:        s->cx += nx;
                    483: }
                    484:
                    485: /* Cursor left by nx. */
                    486: void
                    487: screen_write_cursorleft(struct screen_write_ctx *ctx, u_int nx)
                    488: {
                    489:        struct screen   *s = ctx->s;
                    490:
                    491:        if (nx == 0)
                    492:                nx = 1;
                    493:
                    494:        if (nx > s->cx)
                    495:                nx = s->cx;
                    496:        if (nx == 0)
                    497:                return;
                    498:
                    499:        s->cx -= nx;
1.5       nicm      500: }
                    501:
1.29      nicm      502: /* Backspace; cursor left unless at start of wrapped line when can move up. */
                    503: void
                    504: screen_write_backspace(struct screen_write_ctx *ctx)
                    505: {
                    506:        struct screen           *s = ctx->s;
                    507:        struct grid_line        *gl;
                    508:
                    509:        if (s->cx == 0) {
                    510:                if (s->cy == 0)
                    511:                        return;
                    512:                gl = &s->grid->linedata[s->grid->hsize + s->cy - 1];
                    513:                if (gl->flags & GRID_LINE_WRAPPED) {
                    514:                        s->cy--;
                    515:                        s->cx = screen_size_x(s) - 1;
                    516:                }
                    517:        } else
                    518:                s->cx--;
                    519: }
                    520:
1.5       nicm      521: /* VT100 alignment test. */
                    522: void
                    523: screen_write_alignmenttest(struct screen_write_ctx *ctx)
                    524: {
                    525:        struct screen           *s = ctx->s;
1.17      nicm      526:        struct tty_ctx           ttyctx;
1.5       nicm      527:        struct grid_cell         gc;
                    528:        u_int                    xx, yy;
                    529:
1.87      nicm      530:        screen_write_initctx(ctx, &ttyctx);
1.17      nicm      531:
1.5       nicm      532:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.77      nicm      533:        utf8_set(&gc.data, 'E');
1.7       ray       534:
1.5       nicm      535:        for (yy = 0; yy < screen_size_y(s); yy++) {
                    536:                for (xx = 0; xx < screen_size_x(s); xx++)
                    537:                        grid_view_set_cell(s->grid, xx, yy, &gc);
                    538:        }
1.7       ray       539:
1.5       nicm      540:        s->cx = 0;
                    541:        s->cy = 0;
                    542:
                    543:        s->rupper = 0;
                    544:        s->rlower = screen_size_y(s) - 1;
                    545:
1.109   ! nicm      546:        screen_write_collect_clear(ctx, 0, screen_size_y(s) - 1);
1.17      nicm      547:        tty_write(tty_cmd_alignmenttest, &ttyctx);
1.1       nicm      548: }
                    549:
                    550: /* Insert nx characters. */
                    551: void
1.99      nicm      552: screen_write_insertcharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1       nicm      553: {
                    554:        struct screen   *s = ctx->s;
1.17      nicm      555:        struct tty_ctx   ttyctx;
1.1       nicm      556:
                    557:        if (nx == 0)
                    558:                nx = 1;
                    559:
1.9       nicm      560:        if (nx > screen_size_x(s) - s->cx)
                    561:                nx = screen_size_x(s) - s->cx;
1.1       nicm      562:        if (nx == 0)
                    563:                return;
                    564:
1.107     nicm      565:        if (s->cx > screen_size_x(s) - 1)
                    566:                return;
                    567:
1.87      nicm      568:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      569:        ttyctx.bg = bg;
1.1       nicm      570:
1.107     nicm      571:        grid_view_insert_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      572:
1.109   ! nicm      573:        screen_write_collect_flush(ctx);
1.17      nicm      574:        ttyctx.num = nx;
                    575:        tty_write(tty_cmd_insertcharacter, &ttyctx);
1.1       nicm      576: }
                    577:
                    578: /* Delete nx characters. */
                    579: void
1.99      nicm      580: screen_write_deletecharacter(struct screen_write_ctx *ctx, u_int nx, u_int bg)
1.1       nicm      581: {
                    582:        struct screen   *s = ctx->s;
1.17      nicm      583:        struct tty_ctx   ttyctx;
1.1       nicm      584:
                    585:        if (nx == 0)
                    586:                nx = 1;
                    587:
1.9       nicm      588:        if (nx > screen_size_x(s) - s->cx)
                    589:                nx = screen_size_x(s) - s->cx;
1.1       nicm      590:        if (nx == 0)
                    591:                return;
                    592:
1.107     nicm      593:        if (s->cx > screen_size_x(s) - 1)
                    594:                return;
                    595:
1.87      nicm      596:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      597:        ttyctx.bg = bg;
1.1       nicm      598:
1.107     nicm      599:        grid_view_delete_cells(s->grid, s->cx, s->cy, nx, bg);
1.1       nicm      600:
1.109   ! nicm      601:        screen_write_collect_flush(ctx);
1.17      nicm      602:        ttyctx.num = nx;
                    603:        tty_write(tty_cmd_deletecharacter, &ttyctx);
1.59      nicm      604: }
                    605:
                    606: /* Clear nx characters. */
                    607: void
                    608: screen_write_clearcharacter(struct screen_write_ctx *ctx, u_int nx)
                    609: {
                    610:        struct screen   *s = ctx->s;
                    611:        struct tty_ctx   ttyctx;
                    612:
                    613:        if (nx == 0)
                    614:                nx = 1;
                    615:
                    616:        if (nx > screen_size_x(s) - s->cx)
                    617:                nx = screen_size_x(s) - s->cx;
                    618:        if (nx == 0)
                    619:                return;
                    620:
1.107     nicm      621:        if (s->cx > screen_size_x(s) - 1)
                    622:                return;
                    623:
1.87      nicm      624:        screen_write_initctx(ctx, &ttyctx);
1.59      nicm      625:
1.107     nicm      626:        grid_view_clear(s->grid, s->cx, s->cy, nx, 1, 8);
1.59      nicm      627:
1.109   ! nicm      628:        screen_write_collect_flush(ctx);
1.59      nicm      629:        ttyctx.num = nx;
                    630:        tty_write(tty_cmd_clearcharacter, &ttyctx);
1.1       nicm      631: }
                    632:
                    633: /* Insert ny lines. */
                    634: void
1.99      nicm      635: screen_write_insertline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      636: {
                    637:        struct screen   *s = ctx->s;
1.107     nicm      638:        struct grid     *gd = s->grid;
1.17      nicm      639:        struct tty_ctx   ttyctx;
1.1       nicm      640:
                    641:        if (ny == 0)
                    642:                ny = 1;
                    643:
1.11      nicm      644:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    645:                if (ny > screen_size_y(s) - s->cy)
                    646:                        ny = screen_size_y(s) - s->cy;
                    647:                if (ny == 0)
                    648:                        return;
                    649:
1.87      nicm      650:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      651:                ttyctx.bg = bg;
1.11      nicm      652:
1.107     nicm      653:                grid_view_insert_lines(gd, s->cy, ny, bg);
1.11      nicm      654:
1.109   ! nicm      655:                screen_write_collect_flush(ctx);
1.17      nicm      656:                ttyctx.num = ny;
                    657:                tty_write(tty_cmd_insertline, &ttyctx);
1.11      nicm      658:                return;
                    659:        }
                    660:
                    661:        if (ny > s->rlower + 1 - s->cy)
                    662:                ny = s->rlower + 1 - s->cy;
1.1       nicm      663:        if (ny == 0)
                    664:                return;
1.41      nicm      665:
1.87      nicm      666:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      667:        ttyctx.bg = bg;
1.1       nicm      668:
                    669:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      670:                grid_view_insert_lines(gd, s->cy, ny, bg);
                    671:        else
                    672:                grid_view_insert_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      673:
1.109   ! nicm      674:        screen_write_collect_flush(ctx);
1.17      nicm      675:        ttyctx.num = ny;
                    676:        tty_write(tty_cmd_insertline, &ttyctx);
1.1       nicm      677: }
                    678:
                    679: /* Delete ny lines. */
                    680: void
1.99      nicm      681: screen_write_deleteline(struct screen_write_ctx *ctx, u_int ny, u_int bg)
1.1       nicm      682: {
                    683:        struct screen   *s = ctx->s;
1.107     nicm      684:        struct grid     *gd = s->grid;
1.17      nicm      685:        struct tty_ctx   ttyctx;
1.1       nicm      686:
                    687:        if (ny == 0)
                    688:                ny = 1;
                    689:
1.11      nicm      690:        if (s->cy < s->rupper || s->cy > s->rlower) {
                    691:                if (ny > screen_size_y(s) - s->cy)
                    692:                        ny = screen_size_y(s) - s->cy;
                    693:                if (ny == 0)
                    694:                        return;
                    695:
1.87      nicm      696:                screen_write_initctx(ctx, &ttyctx);
1.107     nicm      697:                ttyctx.bg = bg;
1.11      nicm      698:
1.107     nicm      699:                grid_view_delete_lines(gd, s->cy, ny, bg);
1.11      nicm      700:
1.109   ! nicm      701:                screen_write_collect_flush(ctx);
1.17      nicm      702:                ttyctx.num = ny;
                    703:                tty_write(tty_cmd_deleteline, &ttyctx);
1.11      nicm      704:                return;
                    705:        }
1.41      nicm      706:
1.11      nicm      707:        if (ny > s->rlower + 1 - s->cy)
                    708:                ny = s->rlower + 1 - s->cy;
1.1       nicm      709:        if (ny == 0)
                    710:                return;
                    711:
1.87      nicm      712:        screen_write_initctx(ctx, &ttyctx);
1.107     nicm      713:        ttyctx.bg = bg;
1.1       nicm      714:
                    715:        if (s->cy < s->rupper || s->cy > s->rlower)
1.107     nicm      716:                grid_view_delete_lines(gd, s->cy, ny, bg);
                    717:        else
                    718:                grid_view_delete_lines_region(gd, s->rlower, s->cy, ny, bg);
1.1       nicm      719:
1.109   ! nicm      720:        screen_write_collect_flush(ctx);
1.17      nicm      721:        ttyctx.num = ny;
                    722:        tty_write(tty_cmd_deleteline, &ttyctx);
1.1       nicm      723: }
                    724:
                    725: /* Clear line at cursor. */
                    726: void
1.99      nicm      727: screen_write_clearline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      728: {
1.92      nicm      729:        struct screen           *s = ctx->s;
                    730:        struct grid_line        *gl;
                    731:        struct tty_ctx           ttyctx;
                    732:        u_int                    sx = screen_size_x(s);
1.1       nicm      733:
1.92      nicm      734:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
1.99      nicm      735:        if (gl->cellsize == 0 && bg == 8)
1.92      nicm      736:                return;
1.1       nicm      737:
1.108     nicm      738:        screen_write_initctx(ctx, &ttyctx);
                    739:        ttyctx.bg = bg;
                    740:
1.99      nicm      741:        grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
                    742:
1.109   ! nicm      743:        screen_write_collect_clear(ctx, s->cy, 1);
1.17      nicm      744:        tty_write(tty_cmd_clearline, &ttyctx);
1.1       nicm      745: }
                    746:
                    747: /* Clear to end of line from cursor. */
                    748: void
1.99      nicm      749: screen_write_clearendofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      750: {
1.92      nicm      751:        struct screen           *s = ctx->s;
                    752:        struct grid_line        *gl;
                    753:        struct tty_ctx           ttyctx;
                    754:        u_int                    sx = screen_size_x(s);
1.1       nicm      755:
1.92      nicm      756:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
1.99      nicm      757:        if (s->cx > sx - 1 || (s->cx >= gl->cellsize && bg == 8))
1.92      nicm      758:                return;
1.108     nicm      759:
                    760:        screen_write_initctx(ctx, &ttyctx);
                    761:        ttyctx.bg = bg;
1.1       nicm      762:
1.99      nicm      763:        grid_view_clear(s->grid, s->cx, s->cy, sx - s->cx, 1, bg);
                    764:
1.109   ! nicm      765:        if (s->cx == 0)
        !           766:                screen_write_collect_clear(ctx, s->cy, 1);
        !           767:        else
        !           768:                screen_write_collect_flush(ctx);
1.41      nicm      769:        tty_write(tty_cmd_clearendofline, &ttyctx);
1.1       nicm      770: }
                    771:
                    772: /* Clear to start of line from cursor. */
                    773: void
1.99      nicm      774: screen_write_clearstartofline(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      775: {
                    776:        struct screen   *s = ctx->s;
1.17      nicm      777:        struct tty_ctx   ttyctx;
1.92      nicm      778:        u_int            sx = screen_size_x(s);
1.1       nicm      779:
1.87      nicm      780:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm      781:        ttyctx.bg = bg;
1.1       nicm      782:
1.109   ! nicm      783:        if (s->cx > sx - 1)
1.99      nicm      784:                grid_view_clear(s->grid, 0, s->cy, sx, 1, bg);
1.109   ! nicm      785:        else
1.99      nicm      786:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, bg);
1.1       nicm      787:
1.109   ! nicm      788:        if (s->cx > sx - 1)
        !           789:                screen_write_collect_clear(ctx, s->cy, 1);
        !           790:        else
        !           791:                screen_write_collect_flush(ctx);
1.17      nicm      792:        tty_write(tty_cmd_clearstartofline, &ttyctx);
1.1       nicm      793: }
                    794:
1.101     nicm      795: /* Move cursor to px,py. */
1.1       nicm      796: void
                    797: screen_write_cursormove(struct screen_write_ctx *ctx, u_int px, u_int py)
                    798: {
                    799:        struct screen   *s = ctx->s;
                    800:
                    801:        if (px > screen_size_x(s) - 1)
                    802:                px = screen_size_x(s) - 1;
                    803:        if (py > screen_size_y(s) - 1)
                    804:                py = screen_size_y(s) - 1;
                    805:
                    806:        s->cx = px;
                    807:        s->cy = py;
                    808: }
                    809:
1.101     nicm      810: /* Reverse index (up with scroll). */
1.1       nicm      811: void
                    812: screen_write_reverseindex(struct screen_write_ctx *ctx)
                    813: {
                    814:        struct screen   *s = ctx->s;
1.17      nicm      815:        struct tty_ctx   ttyctx;
1.1       nicm      816:
1.87      nicm      817:        screen_write_initctx(ctx, &ttyctx);
1.1       nicm      818:
1.109   ! nicm      819:        if (s->cy == s->rupper)
1.1       nicm      820:                grid_view_scroll_region_down(s->grid, s->rupper, s->rlower);
1.109   ! nicm      821:        else if (s->cy > 0)
1.1       nicm      822:                s->cy--;
                    823:
1.109   ! nicm      824:        screen_write_collect_flush(ctx);
1.17      nicm      825:        tty_write(tty_cmd_reverseindex, &ttyctx);
1.1       nicm      826: }
                    827:
                    828: /* Set scroll region. */
                    829: void
1.83      nicm      830: screen_write_scrollregion(struct screen_write_ctx *ctx, u_int rupper,
                    831:     u_int rlower)
1.1       nicm      832: {
                    833:        struct screen   *s = ctx->s;
                    834:
                    835:        if (rupper > screen_size_y(s) - 1)
                    836:                rupper = screen_size_y(s) - 1;
                    837:        if (rlower > screen_size_y(s) - 1)
                    838:                rlower = screen_size_y(s) - 1;
1.13      nicm      839:        if (rupper >= rlower)   /* cannot be one line */
1.1       nicm      840:                return;
                    841:
                    842:        /* Cursor moves to top-left. */
                    843:        s->cx = 0;
                    844:        s->cy = 0;
                    845:
                    846:        s->rupper = rupper;
                    847:        s->rlower = rlower;
                    848: }
                    849:
1.34      nicm      850: /* Line feed. */
1.1       nicm      851: void
1.34      nicm      852: screen_write_linefeed(struct screen_write_ctx *ctx, int wrapped)
1.1       nicm      853: {
1.20      nicm      854:        struct screen           *s = ctx->s;
1.109   ! nicm      855:        struct grid             *gd = s->grid;
1.20      nicm      856:        struct grid_line        *gl;
1.109   ! nicm      857:        struct tty_ctx           ttyctx;
1.1       nicm      858:
1.109   ! nicm      859:        gl = &gd->linedata[gd->hsize + s->cy];
1.20      nicm      860:        if (wrapped)
                    861:                gl->flags |= GRID_LINE_WRAPPED;
1.73      nicm      862:        else
                    863:                gl->flags &= ~GRID_LINE_WRAPPED;
1.20      nicm      864:
1.92      nicm      865:        if (s->cy == s->rlower) {
1.109   ! nicm      866:                grid_view_scroll_region_up(gd, s->rupper, s->rlower);
        !           867:
        !           868:                screen_write_collect_scroll(ctx);
        !           869:                screen_write_initctx(ctx, &ttyctx);
        !           870:                tty_write(tty_cmd_linefeed, &ttyctx);
        !           871:        } else if (s->cy < screen_size_y(s) - 1)
1.1       nicm      872:                s->cy++;
                    873: }
                    874:
                    875: /* Carriage return (cursor to start of line). */
                    876: void
                    877: screen_write_carriagereturn(struct screen_write_ctx *ctx)
                    878: {
                    879:        struct screen   *s = ctx->s;
                    880:
                    881:        s->cx = 0;
                    882: }
                    883:
                    884: /* Clear to end of screen from cursor. */
                    885: void
1.99      nicm      886: screen_write_clearendofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      887: {
                    888:        struct screen   *s = ctx->s;
1.107     nicm      889:        struct grid     *gd = s->grid;
1.17      nicm      890:        struct tty_ctx   ttyctx;
1.92      nicm      891:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm      892:
1.87      nicm      893:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm      894:        ttyctx.bg = bg;
1.1       nicm      895:
1.46      nicm      896:        /* Scroll into history if it is enabled and clearing entire screen. */
1.109   ! nicm      897:        if (s->cx == 0 && s->cy == 0 && (gd->flags & GRID_HISTORY))
1.107     nicm      898:                grid_view_clear_history(gd, bg);
1.109   ! nicm      899:        else {
        !           900:                if (s->cx <= sx - 1)
1.107     nicm      901:                        grid_view_clear(gd, s->cx, s->cy, sx - s->cx, 1, bg);
                    902:                grid_view_clear(gd, 0, s->cy + 1, sx, sy - (s->cy + 1), bg);
1.46      nicm      903:        }
1.1       nicm      904:
1.109   ! nicm      905:        screen_write_collect_clear(ctx, s->cy + 1, sy - (s->cy + 1));
        !           906:        screen_write_collect_flush(ctx);
1.17      nicm      907:        tty_write(tty_cmd_clearendofscreen, &ttyctx);
1.1       nicm      908: }
                    909:
                    910: /* Clear to start of screen. */
                    911: void
1.105     nicm      912: screen_write_clearstartofscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      913: {
                    914:        struct screen   *s = ctx->s;
1.17      nicm      915:        struct tty_ctx   ttyctx;
1.92      nicm      916:        u_int            sx = screen_size_x(s);
1.1       nicm      917:
1.87      nicm      918:        screen_write_initctx(ctx, &ttyctx);
1.105     nicm      919:        ttyctx.bg = bg;
1.1       nicm      920:
1.109   ! nicm      921:        if (s->cy > 0)
1.105     nicm      922:                grid_view_clear(s->grid, 0, 0, sx, s->cy, bg);
1.109   ! nicm      923:        if (s->cx > sx - 1)
        !           924:                grid_view_clear(s->grid, 0, s->cy, sx, 1, 8);
        !           925:        else
        !           926:                grid_view_clear(s->grid, 0, s->cy, s->cx + 1, 1, 8);
1.1       nicm      927:
1.109   ! nicm      928:        screen_write_collect_clear(ctx, 0, s->cy);
        !           929:        screen_write_collect_flush(ctx);
1.17      nicm      930:        tty_write(tty_cmd_clearstartofscreen, &ttyctx);
1.1       nicm      931: }
                    932:
                    933: /* Clear entire screen. */
                    934: void
1.99      nicm      935: screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
1.1       nicm      936: {
                    937:        struct screen   *s = ctx->s;
1.17      nicm      938:        struct tty_ctx   ttyctx;
1.92      nicm      939:        u_int            sx = screen_size_x(s), sy = screen_size_y(s);
1.1       nicm      940:
1.87      nicm      941:        screen_write_initctx(ctx, &ttyctx);
1.99      nicm      942:        ttyctx.bg = bg;
1.1       nicm      943:
1.46      nicm      944:        /* Scroll into history if it is enabled. */
                    945:        if (s->grid->flags & GRID_HISTORY)
1.99      nicm      946:                grid_view_clear_history(s->grid, bg);
1.83      nicm      947:        else
1.99      nicm      948:                grid_view_clear(s->grid, 0, 0, sx, sy, bg);
1.1       nicm      949:
1.109   ! nicm      950:        screen_write_collect_clear(ctx, 0, sy);
1.17      nicm      951:        tty_write(tty_cmd_clearscreen, &ttyctx);
1.51      nicm      952: }
                    953:
                    954: /* Clear entire history. */
                    955: void
                    956: screen_write_clearhistory(struct screen_write_ctx *ctx)
                    957: {
                    958:        struct screen   *s = ctx->s;
                    959:        struct grid     *gd = s->grid;
                    960:
1.99      nicm      961:        grid_move_lines(gd, 0, gd->hsize, gd->sy, 8);
1.93      nicm      962:        gd->hscrolled = gd->hsize = 0;
1.1       nicm      963: }
                    964:
1.109   ! nicm      965: /* Clear a collected line. */
        !           966: static void
        !           967: screen_write_collect_clear(struct screen_write_ctx *ctx, u_int y, u_int n)
        !           968: {
        !           969:        struct screen_write_collect_item        *ci, *tmp;
        !           970:        u_int                                    i;
        !           971:        size_t                                   size;
        !           972:
        !           973:        for (i = y ; i < y + n; i++) {
        !           974:                if (TAILQ_EMPTY(&ctx->list[i].items))
        !           975:                        continue;
        !           976:                size = 0;
        !           977:                TAILQ_FOREACH_SAFE(ci, &ctx->list[i].items, entry, tmp) {
        !           978:                        size += ci->used;
        !           979:                        TAILQ_REMOVE(&ctx->list[i].items, ci, entry);
        !           980:                        free(ci);
        !           981:                }
        !           982:                ctx->skipped += size;
        !           983:                log_debug("discarding %zu bytes on line %u", size, i);
        !           984:        }
        !           985: }
        !           986:
        !           987: /* Scroll collected lines up. */
        !           988: static void
        !           989: screen_write_collect_scroll(struct screen_write_ctx *ctx)
        !           990: {
        !           991:        struct screen                           *s = ctx->s;
        !           992:        struct screen_write_collect_line        *cl;
        !           993:        u_int                                    y;
        !           994:
        !           995:        screen_write_collect_clear(ctx, s->rupper, 1);
        !           996:        for (y = s->rupper; y < s->rlower; y++) {
        !           997:                cl = &ctx->list[y + 1];
        !           998:                TAILQ_CONCAT(&ctx->list[y].items, &cl->items, entry);
        !           999:                TAILQ_INIT(&cl->items);
        !          1000:        }
        !          1001: }
        !          1002:
        !          1003: /* Flush collected lines. */
        !          1004: static void
        !          1005: screen_write_collect_flush(struct screen_write_ctx *ctx)
        !          1006: {
        !          1007:        struct screen                           *s = ctx->s;
        !          1008:        struct screen_write_collect_item        *ci, *tmp;
        !          1009:        u_int                                    y, cx, cy;
        !          1010:        struct tty_ctx                           ttyctx;
        !          1011:
        !          1012:        cx = s->cx; cy = s->cy;
        !          1013:        for (y = 0; y < screen_size_y(s); y++) {
        !          1014:                TAILQ_FOREACH_SAFE(ci, &ctx->list[y].items, entry, tmp) {
        !          1015:                        screen_write_cursormove(ctx, ci->x, y);
        !          1016:                        screen_write_initctx(ctx, &ttyctx);
        !          1017:                        ttyctx.cell = &ci->gc;
        !          1018:                        ttyctx.ptr = ci->data;
        !          1019:                        ttyctx.num = ci->used;
        !          1020:                        tty_write(tty_cmd_cells, &ttyctx);
        !          1021:                        ctx->written += ci->used;
        !          1022:
        !          1023:                        TAILQ_REMOVE(&ctx->list[y].items, ci, entry);
        !          1024:                        free(ci);
        !          1025:                }
        !          1026:        }
        !          1027:        s->cx = cx; s->cy = cy;
        !          1028: }
        !          1029:
        !          1030: /* Finish and store collected cells. */
        !          1031: void
        !          1032: screen_write_collect_end(struct screen_write_ctx *ctx)
        !          1033: {
        !          1034:        struct screen                           *s = ctx->s;
        !          1035:        struct screen_write_collect_item        *ci = ctx->item;
        !          1036:        struct grid_cell                         gc;
        !          1037:
        !          1038:        if (ci->used == 0)
        !          1039:                return;
        !          1040:        ci->data[ci->used] = '\0';
        !          1041:
        !          1042:        ci->x = s->cx;
        !          1043:        TAILQ_INSERT_TAIL(&ctx->list[s->cy].items, ci, entry);
        !          1044:        ctx->item = xcalloc(1, sizeof *ctx->item);
        !          1045:
        !          1046:        log_debug("%s: %u %s (at %u,%u)", __func__, ci->used, ci->data, s->cx,
        !          1047:            s->cy);
        !          1048:
        !          1049:        memcpy(&gc, &ci->gc, sizeof gc);
        !          1050:        grid_view_set_cells(s->grid, s->cx, s->cy, &gc, ci->data, ci->used);
        !          1051:        s->cx += ci->used;
        !          1052: }
        !          1053:
        !          1054: /* Write cell data, collecting if necessary. */
        !          1055: void
        !          1056: screen_write_collect_add(struct screen_write_ctx *ctx,
        !          1057:     const struct grid_cell *gc)
        !          1058: {
        !          1059:        struct screen                           *s = ctx->s;
        !          1060:        struct screen_write_collect_item        *ci;
        !          1061:        u_int                                    sx = screen_size_x(s);
        !          1062:        int                                      collect;
        !          1063:
        !          1064:        /*
        !          1065:         * Don't need to check that the attributes and whatnot are still the
        !          1066:         * same - input_parse will do a flush when anything that isn't a plain
        !          1067:         * character is encountered. Also nothing should make it here that
        !          1068:         * isn't a single ASCII character.
        !          1069:         */
        !          1070:
        !          1071:        collect = 1;
        !          1072:        if (gc->data.width != 1)
        !          1073:                collect = 0;
        !          1074:        else if (gc->attr & GRID_ATTR_CHARSET)
        !          1075:                collect = 0;
        !          1076:        else if (~s->mode & MODE_WRAP)
        !          1077:                collect = 0;
        !          1078:        else if (s->mode & MODE_INSERT)
        !          1079:                collect = 0;
        !          1080:        else if (s->sel.flag)
        !          1081:                collect = 0;
        !          1082:        if (!collect) {
        !          1083:                screen_write_collect_end(ctx);
        !          1084:                screen_write_cell(ctx, gc);
        !          1085:                return;
        !          1086:        }
        !          1087:        ctx->cells++;
        !          1088:
        !          1089:        if (s->cx > sx - 1 || ctx->item->used > sx - 1 - s->cx)
        !          1090:                screen_write_collect_end(ctx);
        !          1091:        if (s->cx > sx - 1) {
        !          1092:                screen_write_linefeed(ctx, 1);
        !          1093:                s->cx = 0;
        !          1094:        }
        !          1095:
        !          1096:        ci = ctx->item; /* may have changed */
        !          1097:        if (ci->used == 0)
        !          1098:                memcpy(&ci->gc, gc, sizeof ci->gc);
        !          1099:        ci->data[ci->used++] = gc->data.data[0];
        !          1100:        if (ci->used == (sizeof ci->data) - 1)
        !          1101:                screen_write_collect_end(ctx);
        !          1102: }
        !          1103:
1.1       nicm     1104: /* Write cell data. */
                   1105: void
1.60      nicm     1106: screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
1.1       nicm     1107: {
                   1108:        struct screen           *s = ctx->s;
                   1109:        struct grid             *gd = s->grid;
1.109   ! nicm     1110:        struct grid_line        *gl;
        !          1111:        struct grid_cell_entry  *gce;
        !          1112:        struct grid_cell         tmp_gc, now_gc;
1.15      nicm     1113:        struct tty_ctx           ttyctx;
1.92      nicm     1114:        u_int                    sx = screen_size_x(s), sy = screen_size_y(s);
1.109   ! nicm     1115:        u_int                    width = gc->data.width, xx, last;
        !          1116:        int                      selected, skip = 1;
1.1       nicm     1117:
1.109   ! nicm     1118:        /* Ignore padding cells. */
1.1       nicm     1119:        if (gc->flags & GRID_FLAG_PADDING)
                   1120:                return;
1.109   ! nicm     1121:        ctx->cells++;
1.32      nicm     1122:
1.109   ! nicm     1123:        /* If the width is zero, combine onto the previous character. */
1.1       nicm     1124:        if (width == 0) {
1.104     nicm     1125:                if ((gc = screen_write_combine(ctx, &gc->data, &xx)) != 0) {
                   1126:                        screen_write_cursormove(ctx, xx, s->cy);
1.87      nicm     1127:                        screen_write_initctx(ctx, &ttyctx);
1.104     nicm     1128:                        ttyctx.cell = gc;
                   1129:                        tty_write(tty_cmd_cell, &ttyctx);
1.1       nicm     1130:                }
                   1131:                return;
                   1132:        }
                   1133:
1.109   ! nicm     1134:        /* If this character doesn't fit, ignore it. */
        !          1135:        if ((~s->mode & MODE_WRAP) &&
        !          1136:            width > 1 &&
        !          1137:            (width > sx || (s->cx != sx && s->cx > sx - width)))
        !          1138:                return;
        !          1139:
1.6       nicm     1140:        /* If in insert mode, make space for the cells. */
1.98      nicm     1141:        if (s->mode & MODE_INSERT) {
1.109   ! nicm     1142:                grid_view_insert_cells(s->grid, s->cx, s->cy, width, 8);
        !          1143:                skip = 0;
        !          1144:        }
1.6       nicm     1145:
1.20      nicm     1146:        /* Check this will fit on the current line and wrap if not. */
1.92      nicm     1147:        if ((s->mode & MODE_WRAP) && s->cx > sx - width) {
1.34      nicm     1148:                screen_write_linefeed(ctx, 1);
1.109   ! nicm     1149:                s->cx = 0;
1.1       nicm     1150:        }
                   1151:
1.64      nicm     1152:        /* Sanity check cursor position. */
1.92      nicm     1153:        if (s->cx > sx - width || s->cy > sy - 1)
1.1       nicm     1154:                return;
1.106     nicm     1155:        screen_write_initctx(ctx, &ttyctx);
                   1156:
1.1       nicm     1157:        /* Handle overwriting of UTF-8 characters. */
1.92      nicm     1158:        gl = &s->grid->linedata[s->grid->hsize + s->cy];
                   1159:        if (gl->flags & GRID_LINE_EXTENDED) {
                   1160:                grid_view_get_cell(gd, s->cx, s->cy, &now_gc);
                   1161:                if (screen_write_overwrite(ctx, &now_gc, width))
                   1162:                        skip = 0;
                   1163:        }
1.1       nicm     1164:
                   1165:        /*
                   1166:         * If the new character is UTF-8 wide, fill in padding cells. Have
                   1167:         * already ensured there is enough room.
                   1168:         */
1.89      nicm     1169:        for (xx = s->cx + 1; xx < s->cx + width; xx++) {
1.88      nicm     1170:                grid_view_set_cell(gd, xx, s->cy, &screen_write_pad_cell);
1.89      nicm     1171:                skip = 0;
                   1172:        }
                   1173:
                   1174:        /* If no change, do not draw. */
1.92      nicm     1175:        if (skip) {
                   1176:                if (s->cx >= gl->cellsize)
                   1177:                        skip = grid_cells_equal(gc, &grid_default_cell);
                   1178:                else {
                   1179:                        gce = &gl->celldata[s->cx];
                   1180:                        if (gce->flags & GRID_FLAG_EXTENDED)
                   1181:                                skip = 0;
1.95      nicm     1182:                        else if (gc->flags != gce->flags)
1.92      nicm     1183:                                skip = 0;
                   1184:                        else if (gc->attr != gce->data.attr)
                   1185:                                skip = 0;
                   1186:                        else if (gc->fg != gce->data.fg)
                   1187:                                skip = 0;
                   1188:                        else if (gc->bg != gce->data.bg)
                   1189:                                skip = 0;
1.95      nicm     1190:                        else if (gc->data.width != 1)
                   1191:                                skip = 0;
1.109   ! nicm     1192:                        else if (gc->data.size != 1)
        !          1193:                                skip = 0;
1.95      nicm     1194:                        else if (gce->data.data != gc->data.data[0])
1.92      nicm     1195:                                skip = 0;
                   1196:                }
                   1197:        }
1.1       nicm     1198:
1.90      nicm     1199:        /* Update the selection the flag and set the cell. */
                   1200:        selected = screen_check_selection(s, s->cx, s->cy);
1.109   ! nicm     1201:        if (selected && (~gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1202:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1203:                tmp_gc.flags |= GRID_FLAG_SELECTED;
                   1204:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
1.109   ! nicm     1205:        } else if (!selected && (gc->flags & GRID_FLAG_SELECTED)) {
1.90      nicm     1206:                memcpy(&tmp_gc, gc, sizeof tmp_gc);
                   1207:                tmp_gc.flags &= ~GRID_FLAG_SELECTED;
                   1208:                grid_view_set_cell(gd, s->cx, s->cy, &tmp_gc);
                   1209:        } else if (!skip)
1.89      nicm     1210:                grid_view_set_cell(gd, s->cx, s->cy, gc);
1.109   ! nicm     1211:        if (selected)
        !          1212:                skip = 0;
1.1       nicm     1213:
1.64      nicm     1214:        /*
                   1215:         * Move the cursor. If not wrapping, stick at the last character and
                   1216:         * replace it.
                   1217:         */
1.65      nicm     1218:        last = !(s->mode & MODE_WRAP);
1.92      nicm     1219:        if (s->cx <= sx - last - width)
1.64      nicm     1220:                s->cx += width;
                   1221:        else
1.92      nicm     1222:                s->cx = sx - last;
1.1       nicm     1223:
1.89      nicm     1224:        /* Create space for character in insert mode. */
1.109   ! nicm     1225:        if (s->mode & MODE_INSERT) {
        !          1226:                screen_write_collect_flush(ctx);
1.17      nicm     1227:                ttyctx.num = width;
                   1228:                tty_write(tty_cmd_insertcharacter, &ttyctx);
                   1229:        }
1.89      nicm     1230:
                   1231:        /* Write to the screen. */
1.109   ! nicm     1232:        if (!skip) {
        !          1233:                if (selected) {
        !          1234:                        screen_select_cell(s, &tmp_gc, gc);
        !          1235:                        ttyctx.cell = &tmp_gc;
        !          1236:                } else
        !          1237:                        ttyctx.cell = gc;
1.16      nicm     1238:                tty_write(tty_cmd_cell, &ttyctx);
1.92      nicm     1239:                ctx->written++;
                   1240:        } else
                   1241:                ctx->skipped++;
1.35      nicm     1242: }
                   1243:
                   1244: /* Combine a UTF-8 zero-width character onto the previous. */
1.104     nicm     1245: static const struct grid_cell *
                   1246: screen_write_combine(struct screen_write_ctx *ctx, const struct utf8_data *ud,
                   1247:     u_int *xx)
1.35      nicm     1248: {
                   1249:        struct screen           *s = ctx->s;
                   1250:        struct grid             *gd = s->grid;
1.104     nicm     1251:        static struct grid_cell  gc;
                   1252:        u_int                    n;
1.35      nicm     1253:
                   1254:        /* Can't combine if at 0. */
                   1255:        if (s->cx == 0)
1.104     nicm     1256:                return (NULL);
1.35      nicm     1257:
1.60      nicm     1258:        /* Empty data is out. */
                   1259:        if (ud->size == 0)
1.37      nicm     1260:                fatalx("UTF-8 data empty");
                   1261:
1.60      nicm     1262:        /* Retrieve the previous cell. */
1.104     nicm     1263:        for (n = 1; n < s->cx; n++) {
                   1264:                grid_view_get_cell(gd, s->cx - n, s->cy, &gc);
                   1265:                if (~gc.flags & GRID_FLAG_PADDING)
                   1266:                        break;
                   1267:        }
                   1268:        if (n == s->cx)
                   1269:                return (NULL);
                   1270:        *xx = s->cx - n;
1.35      nicm     1271:
1.60      nicm     1272:        /* Check there is enough space. */
1.77      nicm     1273:        if (gc.data.size + ud->size > sizeof gc.data.data)
1.104     nicm     1274:                return (NULL);
                   1275:
                   1276:        log_debug("%s: %.*s onto %.*s at %u,%u", __func__, (int)ud->size,
                   1277:            ud->data, (int)gc.data.size, gc.data.data, *xx, s->cy);
1.35      nicm     1278:
1.77      nicm     1279:        /* Append the data. */
                   1280:        memcpy(gc.data.data + gc.data.size, ud->data, ud->size);
                   1281:        gc.data.size += ud->size;
                   1282:
                   1283:        /* Set the new cell. */
1.104     nicm     1284:        grid_view_set_cell(gd, *xx, s->cy, &gc);
1.35      nicm     1285:
1.104     nicm     1286:        return (&gc);
1.1       nicm     1287: }
                   1288:
                   1289: /*
                   1290:  * UTF-8 wide characters are a bit of an annoyance. They take up more than one
                   1291:  * cell on the screen, so following cells must not be drawn by marking them as
                   1292:  * padding.
                   1293:  *
                   1294:  * So far, so good. The problem is, when overwriting a padding cell, or a UTF-8
                   1295:  * character, it is necessary to also overwrite any other cells which covered
                   1296:  * by the same character.
                   1297:  */
1.89      nicm     1298: static int
                   1299: screen_write_overwrite(struct screen_write_ctx *ctx, struct grid_cell *gc,
                   1300:     u_int width)
1.1       nicm     1301: {
                   1302:        struct screen           *s = ctx->s;
                   1303:        struct grid             *gd = s->grid;
1.89      nicm     1304:        struct grid_cell         tmp_gc;
1.1       nicm     1305:        u_int                    xx;
1.89      nicm     1306:        int                      done = 0;
1.1       nicm     1307:
1.89      nicm     1308:        if (gc->flags & GRID_FLAG_PADDING) {
1.1       nicm     1309:                /*
                   1310:                 * A padding cell, so clear any following and leading padding
                   1311:                 * cells back to the character. Don't overwrite the current
                   1312:                 * cell as that happens later anyway.
                   1313:                 */
                   1314:                xx = s->cx + 1;
                   1315:                while (--xx > 0) {
1.89      nicm     1316:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1317:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
1.1       nicm     1318:                                break;
                   1319:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1320:                }
                   1321:
                   1322:                /* Overwrite the character at the start of this padding. */
                   1323:                grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
1.89      nicm     1324:                done = 1;
1.43      nicm     1325:        }
1.1       nicm     1326:
1.43      nicm     1327:        /*
1.95      nicm     1328:         * Overwrite any padding cells that belong to any UTF-8 characters
                   1329:         * we'll be overwriting with the current character.
1.43      nicm     1330:         */
1.95      nicm     1331:        if (width != 1 ||
                   1332:            gc->data.width != 1 ||
                   1333:            gc->flags & GRID_FLAG_PADDING) {
1.89      nicm     1334:                xx = s->cx + width - 1;
                   1335:                while (++xx < screen_size_x(s)) {
                   1336:                        grid_view_get_cell(gd, xx, s->cy, &tmp_gc);
                   1337:                        if (~tmp_gc.flags & GRID_FLAG_PADDING)
                   1338:                                break;
                   1339:                        grid_view_set_cell(gd, xx, s->cy, &grid_default_cell);
                   1340:                        done = 1;
                   1341:                }
1.1       nicm     1342:        }
1.89      nicm     1343:
                   1344:        return (done);
1.50      nicm     1345: }
                   1346:
1.107     nicm     1347: /* Set external clipboard. */
1.50      nicm     1348: void
                   1349: screen_write_setselection(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1350: {
                   1351:        struct tty_ctx  ttyctx;
                   1352:
1.87      nicm     1353:        screen_write_initctx(ctx, &ttyctx);
1.50      nicm     1354:        ttyctx.ptr = str;
                   1355:        ttyctx.num = len;
                   1356:
                   1357:        tty_write(tty_cmd_setselection, &ttyctx);
1.47      nicm     1358: }
                   1359:
1.107     nicm     1360: /* Write unmodified string. */
1.47      nicm     1361: void
                   1362: screen_write_rawstring(struct screen_write_ctx *ctx, u_char *str, u_int len)
                   1363: {
1.87      nicm     1364:        struct tty_ctx  ttyctx;
1.47      nicm     1365:
1.87      nicm     1366:        screen_write_initctx(ctx, &ttyctx);
1.47      nicm     1367:        ttyctx.ptr = str;
                   1368:        ttyctx.num = len;
                   1369:
                   1370:        tty_write(tty_cmd_rawstring, &ttyctx);
1.1       nicm     1371: }