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

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