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

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