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

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