[BACK]Return to grid.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/grid.c, Revision 1.52

1.52    ! nicm        1: /* $OpenBSD: grid.c,v 1.51 2016/01/29 11:13:56 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.50      nicm        4:  * Copyright (c) 2008 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.20      nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Grid data. This is the basic data structure that represents what is shown on
                     28:  * screen.
                     29:  *
                     30:  * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
                     31:  * cells in that line are written to. The grid is split into history and
                     32:  * viewable data with the history starting at row (line) 0 and extending to
                     33:  * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
                     34:  * functions in this file work on absolute coordinates, grid-view.c has
                     35:  * functions which work on the screen data.
                     36:  */
                     37:
                     38: /* Default grid cell data. */
1.48      nicm       39: const struct grid_cell grid_default_cell = {
1.51      nicm       40:        0, 0, { .fg = 8 }, { .bg = 8 }, { { ' ' }, 0, 1, 1 }
1.48      nicm       41: };
                     42: const struct grid_cell_entry grid_default_entry = {
                     43:        0, { .data = { 0, 8, 8, ' ' } }
                     44: };
1.1       nicm       45:
                     46: int    grid_check_y(struct grid *, u_int);
                     47:
1.48      nicm       48: void   grid_reflow_copy(struct grid_line *, u_int, struct grid_line *l,
                     49:            u_int, u_int);
1.43      nicm       50: void   grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
                     51: void   grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
                     52:            u_int);
                     53: void   grid_reflow_move(struct grid *, u_int *, struct grid_line *);
                     54: size_t grid_string_cells_fg(const struct grid_cell *, int *);
                     55: size_t grid_string_cells_bg(const struct grid_cell *, int *);
                     56: void   grid_string_cells_code(const struct grid_cell *,
                     57:            const struct grid_cell *, char *, size_t, int);
                     58:
1.48      nicm       59: /* Copy default into a cell. */
                     60: static void
                     61: grid_clear_cell(struct grid *gd, u_int px, u_int py)
                     62: {
                     63:        gd->linedata[py].celldata[px] = grid_default_entry;
                     64: }
                     65:
1.43      nicm       66: /* Check grid y position. */
1.1       nicm       67: int
                     68: grid_check_y(struct grid *gd, u_int py)
                     69: {
                     70:        if ((py) >= (gd)->hsize + (gd)->sy) {
                     71:                log_debug("y out of range: %u", py);
                     72:                return (-1);
                     73:        }
                     74:        return (0);
                     75: }
1.23      nicm       76:
1.1       nicm       77: /* Create a new grid. */
                     78: struct grid *
                     79: grid_create(u_int sx, u_int sy, u_int hlimit)
                     80: {
                     81:        struct grid     *gd;
                     82:
                     83:        gd = xmalloc(sizeof *gd);
                     84:        gd->sx = sx;
                     85:        gd->sy = sy;
                     86:
1.7       nicm       87:        gd->flags = GRID_HISTORY;
                     88:
1.1       nicm       89:        gd->hsize = 0;
                     90:        gd->hlimit = hlimit;
                     91:
1.10      nicm       92:        gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
1.1       nicm       93:
                     94:        return (gd);
                     95: }
                     96:
                     97: /* Destroy grid. */
                     98: void
                     99: grid_destroy(struct grid *gd)
                    100: {
1.10      nicm      101:        struct grid_line        *gl;
                    102:        u_int                    yy;
1.1       nicm      103:
                    104:        for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1.10      nicm      105:                gl = &gd->linedata[yy];
1.20      nicm      106:                free(gl->celldata);
1.48      nicm      107:                free(gl->extddata);
1.1       nicm      108:        }
                    109:
1.20      nicm      110:        free(gd->linedata);
1.1       nicm      111:
1.20      nicm      112:        free(gd);
1.1       nicm      113: }
                    114:
                    115: /* Compare grids. */
                    116: int
                    117: grid_compare(struct grid *ga, struct grid *gb)
                    118: {
1.10      nicm      119:        struct grid_line        *gla, *glb;
1.48      nicm      120:        struct grid_cell         gca, gcb;
1.1       nicm      121:        u_int                    xx, yy;
                    122:
1.33      nicm      123:        if (ga->sx != gb->sx || ga->sy != gb->sy)
1.1       nicm      124:                return (1);
                    125:
                    126:        for (yy = 0; yy < ga->sy; yy++) {
1.10      nicm      127:                gla = &ga->linedata[yy];
                    128:                glb = &gb->linedata[yy];
                    129:                if (gla->cellsize != glb->cellsize)
1.1       nicm      130:                        return (1);
1.48      nicm      131:                for (xx = 0; xx < gla->cellsize; xx++) {
                    132:                        grid_get_cell(ga, xx, yy, &gca);
                    133:                        grid_get_cell(gb, xx, yy, &gcb);
                    134:                        if (memcmp(&gca, &gcb, sizeof (struct grid_cell)) != 0)
1.1       nicm      135:                                return (1);
                    136:                }
                    137:        }
                    138:
                    139:        return (0);
                    140: }
                    141:
1.15      nicm      142: /*
                    143:  * Collect lines from the history if at the limit. Free the top (oldest) 10%
                    144:  * and shift up.
                    145:  */
1.1       nicm      146: void
1.15      nicm      147: grid_collect_history(struct grid *gd)
1.1       nicm      148: {
                    149:        u_int   yy;
                    150:
1.15      nicm      151:        if (gd->hsize < gd->hlimit)
                    152:                return;
                    153:
                    154:        yy = gd->hlimit / 10;
                    155:        if (yy < 1)
                    156:                yy = 1;
                    157:
                    158:        grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
                    159:        gd->hsize -= yy;
                    160: }
                    161:
1.17      nicm      162: /*
1.15      nicm      163:  * Scroll the entire visible screen, moving one line into the history. Just
                    164:  * allocate a new line at the bottom and move the history size indicator.
                    165:  */
                    166: void
                    167: grid_scroll_history(struct grid *gd)
                    168: {
                    169:        u_int   yy;
1.1       nicm      170:
                    171:        yy = gd->hsize + gd->sy;
1.41      nicm      172:        gd->linedata = xreallocarray(gd->linedata, yy + 1,
                    173:            sizeof *gd->linedata);
1.15      nicm      174:        memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.17      nicm      175:
1.15      nicm      176:        gd->hsize++;
                    177: }
1.1       nicm      178:
1.46      nicm      179: /* Clear the history. */
                    180: void
                    181: grid_clear_history(struct grid *gd)
                    182: {
                    183:        grid_clear_lines(gd, 0, gd->hsize);
                    184:        grid_move_lines(gd, 0, gd->hsize, gd->sy);
                    185:
                    186:        gd->hsize = 0;
                    187:        gd->linedata = xreallocarray(gd->linedata, gd->sy,
                    188:            sizeof *gd->linedata);
                    189: }
                    190:
1.15      nicm      191: /* Scroll a region up, moving the top line into the history. */
                    192: void
                    193: grid_scroll_history_region(struct grid *gd, u_int upper, u_int lower)
                    194: {
                    195:        struct grid_line        *gl_history, *gl_upper, *gl_lower;
                    196:        u_int                    yy;
                    197:
                    198:        /* Create a space for a new line. */
                    199:        yy = gd->hsize + gd->sy;
1.41      nicm      200:        gd->linedata = xreallocarray(gd->linedata, yy + 1,
                    201:            sizeof *gd->linedata);
1.1       nicm      202:
1.15      nicm      203:        /* Move the entire screen down to free a space for this line. */
                    204:        gl_history = &gd->linedata[gd->hsize];
                    205:        memmove(gl_history + 1, gl_history, gd->sy * sizeof *gl_history);
                    206:
                    207:        /* Adjust the region and find its start and end. */
                    208:        upper++;
                    209:        gl_upper = &gd->linedata[upper];
                    210:        lower++;
                    211:        gl_lower = &gd->linedata[lower];
                    212:
                    213:        /* Move the line into the history. */
                    214:        memcpy(gl_history, gl_upper, sizeof *gl_history);
                    215:
                    216:        /* Then move the region up and clear the bottom line. */
                    217:        memmove(gl_upper, gl_upper + 1, (lower - upper) * sizeof *gl_upper);
                    218:        memset(gl_lower, 0, sizeof *gl_lower);
                    219:
                    220:        /* Move the history offset down over the line. */
1.1       nicm      221:        gd->hsize++;
                    222: }
                    223:
                    224: /* Expand line to fit to cell. */
                    225: void
1.14      nicm      226: grid_expand_line(struct grid *gd, u_int py, u_int sx)
1.1       nicm      227: {
1.10      nicm      228:        struct grid_line        *gl;
1.14      nicm      229:        u_int                    xx;
1.1       nicm      230:
1.10      nicm      231:        gl = &gd->linedata[py];
1.14      nicm      232:        if (sx <= gl->cellsize)
1.1       nicm      233:                return;
                    234:
1.41      nicm      235:        gl->celldata = xreallocarray(gl->celldata, sx, sizeof *gl->celldata);
1.10      nicm      236:        for (xx = gl->cellsize; xx < sx; xx++)
1.48      nicm      237:                grid_clear_cell(gd, xx, py);
1.10      nicm      238:        gl->cellsize = sx;
1.1       nicm      239: }
                    240:
1.26      nicm      241: /* Peek at grid line. */
                    242: const struct grid_line *
                    243: grid_peek_line(struct grid *gd, u_int py)
                    244: {
                    245:        if (grid_check_y(gd, py) != 0)
                    246:                return (NULL);
                    247:        return (&gd->linedata[py]);
                    248: }
                    249:
1.1       nicm      250: /* Get cell for reading. */
1.48      nicm      251: void
                    252: grid_get_cell(struct grid *gd, u_int px, u_int py, struct grid_cell *gc)
1.1       nicm      253: {
1.48      nicm      254:        struct grid_line        *gl;
                    255:        struct grid_cell_entry  *gce;
                    256:
                    257:        if (grid_check_y(gd, py) != 0 || px >= gd->linedata[py].cellsize) {
                    258:                memcpy(gc, &grid_default_cell, sizeof *gc);
                    259:                return;
                    260:        }
1.1       nicm      261:
1.48      nicm      262:        gl = &gd->linedata[py];
                    263:        gce = &gl->celldata[px];
1.1       nicm      264:
1.48      nicm      265:        if (gce->flags & GRID_FLAG_EXTENDED) {
                    266:                if (gce->offset >= gl->extdsize)
                    267:                        memcpy(gc, &grid_default_cell, sizeof *gc);
                    268:                else
                    269:                        memcpy(gc, &gl->extddata[gce->offset], sizeof *gc);
                    270:                return;
                    271:        }
1.1       nicm      272:
1.48      nicm      273:        gc->flags = gce->flags & ~GRID_FLAG_EXTENDED;
                    274:        gc->attr = gce->data.attr;
                    275:        gc->fg = gce->data.fg;
                    276:        gc->bg = gce->data.bg;
                    277:        utf8_set(&gc->data, gce->data.data);
1.1       nicm      278: }
                    279:
                    280: /* Set cell at relative position. */
                    281: void
1.31      nicm      282: grid_set_cell(struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
1.1       nicm      283: {
1.48      nicm      284:        struct grid_line        *gl;
                    285:        struct grid_cell_entry  *gce;
                    286:        struct grid_cell        *gcp;
1.51      nicm      287:        int                      extended;
1.48      nicm      288:
1.1       nicm      289:        if (grid_check_y(gd, py) != 0)
                    290:                return;
                    291:
                    292:        grid_expand_line(gd, py, px + 1);
1.48      nicm      293:
                    294:        gl = &gd->linedata[py];
                    295:        gce = &gl->celldata[px];
                    296:
1.51      nicm      297:        extended = (gce->flags & GRID_FLAG_EXTENDED);
                    298:        if (!extended && (gc->data.size != 1 || gc->data.width != 1))
                    299:                extended = 1;
                    300:        if (!extended && (gc->flags & (GRID_FLAG_FGRGB|GRID_FLAG_BGRGB)))
                    301:                extended = 1;
                    302:        if (extended) {
1.48      nicm      303:                if (~gce->flags & GRID_FLAG_EXTENDED) {
                    304:                        gl->extddata = xreallocarray(gl->extddata,
                    305:                            gl->extdsize + 1, sizeof *gl->extddata);
                    306:                        gce->offset = gl->extdsize++;
                    307:                        gce->flags = gc->flags | GRID_FLAG_EXTENDED;
                    308:                }
                    309:
                    310:                if (gce->offset >= gl->extdsize)
                    311:                        fatalx("offset too big");
                    312:                gcp = &gl->extddata[gce->offset];
                    313:                memcpy(gcp, gc, sizeof *gcp);
                    314:                return;
                    315:        }
                    316:
                    317:        gce->flags = gc->flags & ~GRID_FLAG_EXTENDED;
                    318:        gce->data.attr = gc->attr;
                    319:        gce->data.fg = gc->fg;
                    320:        gce->data.bg = gc->bg;
                    321:        gce->data.data = gc->data.data[0];
1.1       nicm      322: }
                    323:
1.14      nicm      324: /* Clear area. */
1.1       nicm      325: void
                    326: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
                    327: {
                    328:        u_int   xx, yy;
                    329:
                    330:        if (nx == 0 || ny == 0)
                    331:                return;
                    332:
                    333:        if (px == 0 && nx == gd->sx) {
                    334:                grid_clear_lines(gd, py, ny);
                    335:                return;
                    336:        }
                    337:
                    338:        if (grid_check_y(gd, py) != 0)
                    339:                return;
                    340:        if (grid_check_y(gd, py + ny - 1) != 0)
                    341:                return;
                    342:
                    343:        for (yy = py; yy < py + ny; yy++) {
1.14      nicm      344:                if (px >= gd->linedata[yy].cellsize)
                    345:                        continue;
                    346:                if (px + nx >= gd->linedata[yy].cellsize) {
                    347:                        gd->linedata[yy].cellsize = px;
                    348:                        continue;
                    349:                }
1.1       nicm      350:                for (xx = px; xx < px + nx; xx++) {
1.10      nicm      351:                        if (xx >= gd->linedata[yy].cellsize)
1.1       nicm      352:                                break;
1.48      nicm      353:                        grid_clear_cell(gd, xx, yy);
1.1       nicm      354:                }
                    355:        }
                    356: }
                    357:
                    358: /* Clear lines. This just frees and truncates the lines. */
                    359: void
                    360: grid_clear_lines(struct grid *gd, u_int py, u_int ny)
                    361: {
1.10      nicm      362:        struct grid_line        *gl;
                    363:        u_int                    yy;
1.1       nicm      364:
                    365:        if (ny == 0)
                    366:                return;
                    367:
                    368:        if (grid_check_y(gd, py) != 0)
                    369:                return;
                    370:        if (grid_check_y(gd, py + ny - 1) != 0)
                    371:                return;
                    372:
                    373:        for (yy = py; yy < py + ny; yy++) {
1.10      nicm      374:                gl = &gd->linedata[yy];
1.20      nicm      375:                free(gl->celldata);
1.49      nicm      376:                free(gl->extddata);
1.10      nicm      377:                memset(gl, 0, sizeof *gl);
1.1       nicm      378:        }
                    379: }
                    380:
                    381: /* Move a group of lines. */
                    382: void
                    383: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
                    384: {
                    385:        u_int   yy;
                    386:
                    387:        if (ny == 0 || py == dy)
                    388:                return;
                    389:
                    390:        if (grid_check_y(gd, py) != 0)
                    391:                return;
                    392:        if (grid_check_y(gd, py + ny - 1) != 0)
                    393:                return;
                    394:        if (grid_check_y(gd, dy) != 0)
                    395:                return;
                    396:        if (grid_check_y(gd, dy + ny - 1) != 0)
                    397:                return;
                    398:
                    399:        /* Free any lines which are being replaced. */
                    400:        for (yy = dy; yy < dy + ny; yy++) {
                    401:                if (yy >= py && yy < py + ny)
                    402:                        continue;
                    403:                grid_clear_lines(gd, yy, 1);
                    404:        }
                    405:
1.46      nicm      406:        memmove(&gd->linedata[dy], &gd->linedata[py],
                    407:            ny * (sizeof *gd->linedata));
1.1       nicm      408:
                    409:        /* Wipe any lines that have been moved (without freeing them). */
                    410:        for (yy = py; yy < py + ny; yy++) {
                    411:                if (yy >= dy && yy < dy + ny)
                    412:                        continue;
1.10      nicm      413:                memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1       nicm      414:        }
                    415: }
                    416:
                    417: /* Move a group of cells. */
                    418: void
                    419: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
                    420: {
1.10      nicm      421:        struct grid_line        *gl;
                    422:        u_int                    xx;
1.1       nicm      423:
                    424:        if (nx == 0 || px == dx)
                    425:                return;
                    426:
                    427:        if (grid_check_y(gd, py) != 0)
                    428:                return;
1.10      nicm      429:        gl = &gd->linedata[py];
1.1       nicm      430:
                    431:        grid_expand_line(gd, py, px + nx);
                    432:        grid_expand_line(gd, py, dx + nx);
1.46      nicm      433:        memmove(&gl->celldata[dx], &gl->celldata[px],
                    434:            nx * sizeof *gl->celldata);
1.1       nicm      435:
                    436:        /* Wipe any cells that have been moved. */
                    437:        for (xx = px; xx < px + nx; xx++) {
                    438:                if (xx >= dx && xx < dx + nx)
                    439:                        continue;
1.48      nicm      440:                grid_clear_cell(gd, xx, py);
1.1       nicm      441:        }
1.3       nicm      442: }
                    443:
1.24      nicm      444: /* Get ANSI foreground sequence. */
                    445: size_t
                    446: grid_string_cells_fg(const struct grid_cell *gc, int *values)
                    447: {
                    448:        size_t  n;
                    449:
                    450:        n = 0;
                    451:        if (gc->flags & GRID_FLAG_FG256) {
                    452:                values[n++] = 38;
                    453:                values[n++] = 5;
                    454:                values[n++] = gc->fg;
1.52    ! nicm      455:        } else if (gc->flags & GRID_FLAG_FGRGB) {
        !           456:                values[n++] = 38;
        !           457:                values[n++] = 2;
        !           458:                values[n++] = gc->fg_rgb.r;
        !           459:                values[n++] = gc->fg_rgb.g;
        !           460:                values[n++] = gc->fg_rgb.b;
1.24      nicm      461:        } else {
                    462:                switch (gc->fg) {
1.45      nicm      463:                case 0:
                    464:                case 1:
                    465:                case 2:
                    466:                case 3:
                    467:                case 4:
                    468:                case 5:
                    469:                case 6:
                    470:                case 7:
                    471:                        values[n++] = gc->fg + 30;
                    472:                        break;
                    473:                case 8:
                    474:                        values[n++] = 39;
                    475:                        break;
                    476:                case 90:
                    477:                case 91:
                    478:                case 92:
                    479:                case 93:
                    480:                case 94:
                    481:                case 95:
                    482:                case 96:
                    483:                case 97:
                    484:                        values[n++] = gc->fg;
                    485:                        break;
1.24      nicm      486:                }
                    487:        }
                    488:        return (n);
                    489: }
                    490:
                    491: /* Get ANSI background sequence. */
                    492: size_t
                    493: grid_string_cells_bg(const struct grid_cell *gc, int *values)
                    494: {
                    495:        size_t  n;
                    496:
                    497:        n = 0;
                    498:        if (gc->flags & GRID_FLAG_BG256) {
                    499:                values[n++] = 48;
                    500:                values[n++] = 5;
                    501:                values[n++] = gc->bg;
1.52    ! nicm      502:        } else if (gc->flags & GRID_FLAG_BGRGB) {
        !           503:                values[n++] = 48;
        !           504:                values[n++] = 2;
        !           505:                values[n++] = gc->bg_rgb.r;
        !           506:                values[n++] = gc->bg_rgb.g;
        !           507:                values[n++] = gc->bg_rgb.b;
1.24      nicm      508:        } else {
                    509:                switch (gc->bg) {
                    510:                case 0:
                    511:                case 1:
                    512:                case 2:
                    513:                case 3:
                    514:                case 4:
                    515:                case 5:
                    516:                case 6:
                    517:                case 7:
                    518:                        values[n++] = gc->bg + 40;
                    519:                        break;
                    520:                case 8:
                    521:                        values[n++] = 49;
                    522:                        break;
                    523:                case 100:
                    524:                case 101:
                    525:                case 102:
                    526:                case 103:
                    527:                case 104:
                    528:                        case 105:
                    529:                case 106:
                    530:                case 107:
                    531:                        values[n++] = gc->bg - 10;
                    532:                        break;
                    533:                }
                    534:        }
                    535:        return (n);
                    536: }
                    537:
                    538: /*
                    539:  * Returns ANSI code to set particular attributes (colour, bold and so on)
                    540:  * given a current state. The output buffer must be able to hold at least 57
                    541:  * bytes.
                    542:  */
                    543: void
                    544: grid_string_cells_code(const struct grid_cell *lastgc,
1.26      nicm      545:     const struct grid_cell *gc, char *buf, size_t len, int escape_c0)
1.24      nicm      546: {
1.52    ! nicm      547:        int     oldc[64], newc[64], s[128];
1.24      nicm      548:        size_t  noldc, nnewc, n, i;
                    549:        u_int   attr = gc->attr;
                    550:        u_int   lastattr = lastgc->attr;
                    551:        char    tmp[64];
                    552:
                    553:        struct {
                    554:                u_int   mask;
                    555:                u_int   code;
                    556:        } attrs[] = {
                    557:                { GRID_ATTR_BRIGHT, 1 },
                    558:                { GRID_ATTR_DIM, 2 },
                    559:                { GRID_ATTR_ITALICS, 3 },
                    560:                { GRID_ATTR_UNDERSCORE, 4 },
                    561:                { GRID_ATTR_BLINK, 5 },
                    562:                { GRID_ATTR_REVERSE, 7 },
                    563:                { GRID_ATTR_HIDDEN, 8 }
                    564:        };
                    565:        n = 0;
                    566:
                    567:        /* If any attribute is removed, begin with 0. */
                    568:        for (i = 0; i < nitems(attrs); i++) {
                    569:                if (!(attr & attrs[i].mask) && (lastattr & attrs[i].mask)) {
                    570:                        s[n++] = 0;
1.25      nicm      571:                        lastattr &= GRID_ATTR_CHARSET;
1.24      nicm      572:                        break;
                    573:                }
                    574:        }
                    575:        /* For each attribute that is newly set, add its code. */
                    576:        for (i = 0; i < nitems(attrs); i++) {
                    577:                if ((attr & attrs[i].mask) && !(lastattr & attrs[i].mask))
                    578:                        s[n++] = attrs[i].code;
                    579:        }
                    580:
1.39      nicm      581:        /* If the foreground colour changed, append its parameters. */
1.24      nicm      582:        nnewc = grid_string_cells_fg(gc, newc);
                    583:        noldc = grid_string_cells_fg(lastgc, oldc);
1.39      nicm      584:        if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24      nicm      585:                for (i = 0; i < nnewc; i++)
                    586:                        s[n++] = newc[i];
                    587:        }
                    588:
1.39      nicm      589:        /* If the background colour changed, append its parameters. */
1.24      nicm      590:        nnewc = grid_string_cells_bg(gc, newc);
                    591:        noldc = grid_string_cells_bg(lastgc, oldc);
1.39      nicm      592:        if (nnewc != noldc || memcmp(newc, oldc, nnewc * sizeof newc[0]) != 0) {
1.24      nicm      593:                for (i = 0; i < nnewc; i++)
                    594:                        s[n++] = newc[i];
                    595:        }
                    596:
                    597:        /* If there are any parameters, append an SGR code. */
                    598:        *buf = '\0';
                    599:        if (n > 0) {
1.26      nicm      600:                if (escape_c0)
                    601:                        strlcat(buf, "\\033[", len);
                    602:                else
                    603:                        strlcat(buf, "\033[", len);
1.24      nicm      604:                for (i = 0; i < n; i++) {
                    605:                        if (i + 1 < n)
                    606:                                xsnprintf(tmp, sizeof tmp, "%d;", s[i]);
                    607:                        else
                    608:                                xsnprintf(tmp, sizeof tmp, "%d", s[i]);
                    609:                        strlcat(buf, tmp, len);
                    610:                }
                    611:                strlcat(buf, "m", len);
                    612:        }
                    613:
                    614:        /* Append shift in/shift out if needed. */
1.26      nicm      615:        if ((attr & GRID_ATTR_CHARSET) && !(lastattr & GRID_ATTR_CHARSET)) {
                    616:                if (escape_c0)
                    617:                        strlcat(buf, "\\016", len);  /* SO */
                    618:                else
                    619:                        strlcat(buf, "\016", len);  /* SO */
                    620:        }
                    621:        if (!(attr & GRID_ATTR_CHARSET) && (lastattr & GRID_ATTR_CHARSET)) {
                    622:                if (escape_c0)
                    623:                        strlcat(buf, "\\017", len);  /* SI */
                    624:                else
                    625:                        strlcat(buf, "\017", len);  /* SI */
                    626:        }
1.24      nicm      627: }
                    628:
1.3       nicm      629: /* Convert cells into a string. */
                    630: char *
1.24      nicm      631: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx,
1.28      nicm      632:     struct grid_cell **lastgc, int with_codes, int escape_c0, int trim)
1.3       nicm      633: {
1.48      nicm      634:        struct grid_cell         gc;
1.24      nicm      635:        static struct grid_cell  lastgc1;
1.38      nicm      636:        const char              *data;
1.24      nicm      637:        char                    *buf, code[128];
1.26      nicm      638:        size_t                   len, off, size, codelen;
1.16      nicm      639:        u_int                    xx;
1.30      nicm      640:        const struct grid_line  *gl;
1.3       nicm      641:
1.29      nicm      642:        if (lastgc != NULL && *lastgc == NULL) {
1.24      nicm      643:                memcpy(&lastgc1, &grid_default_cell, sizeof lastgc1);
                    644:                *lastgc = &lastgc1;
                    645:        }
                    646:
1.3       nicm      647:        len = 128;
                    648:        buf = xmalloc(len);
                    649:        off = 0;
                    650:
1.30      nicm      651:        gl = grid_peek_line(gd, py);
1.3       nicm      652:        for (xx = px; xx < px + nx; xx++) {
1.30      nicm      653:                if (gl == NULL || xx >= gl->cellsize)
                    654:                        break;
1.48      nicm      655:                grid_get_cell(gd, xx, py, &gc);
                    656:                if (gc.flags & GRID_FLAG_PADDING)
1.3       nicm      657:                        continue;
                    658:
1.24      nicm      659:                if (with_codes) {
1.48      nicm      660:                        grid_string_cells_code(*lastgc, &gc, code, sizeof code,
1.26      nicm      661:                            escape_c0);
1.24      nicm      662:                        codelen = strlen(code);
1.48      nicm      663:                        memcpy(*lastgc, &gc, sizeof **lastgc);
1.24      nicm      664:                } else
                    665:                        codelen = 0;
                    666:
1.48      nicm      667:                data = gc.data.data;
                    668:                size = gc.data.size;
1.26      nicm      669:                if (escape_c0 && size == 1 && *data == '\\') {
1.27      nicm      670:                        data = "\\\\";
1.26      nicm      671:                        size = 2;
                    672:                }
                    673:
                    674:                while (len < off + size + codelen + 1) {
1.41      nicm      675:                        buf = xreallocarray(buf, 2, len);
1.21      nicm      676:                        len *= 2;
                    677:                }
1.3       nicm      678:
1.24      nicm      679:                if (codelen != 0) {
                    680:                        memcpy(buf + off, code, codelen);
                    681:                        off += codelen;
                    682:                }
1.26      nicm      683:                memcpy(buf + off, data, size);
                    684:                off += size;
1.3       nicm      685:        }
1.17      nicm      686:
1.37      nicm      687:        if (trim) {
1.28      nicm      688:                while (off > 0 && buf[off - 1] == ' ')
                    689:                        off--;
1.32      nicm      690:        }
1.3       nicm      691:        buf[off] = '\0';
1.26      nicm      692:
1.3       nicm      693:        return (buf);
1.7       nicm      694: }
                    695:
1.17      nicm      696: /*
1.7       nicm      697:  * Duplicate a set of lines between two grids. If there aren't enough lines in
                    698:  * either source or destination, the number of lines is limited to the number
                    699:  * available.
                    700:  */
                    701: void
1.31      nicm      702: grid_duplicate_lines(struct grid *dst, u_int dy, struct grid *src, u_int sy,
                    703:     u_int ny)
1.7       nicm      704: {
1.10      nicm      705:        struct grid_line        *dstl, *srcl;
                    706:        u_int                    yy;
1.7       nicm      707:
                    708:        if (dy + ny > dst->hsize + dst->sy)
                    709:                ny = dst->hsize + dst->sy - dy;
                    710:        if (sy + ny > src->hsize + src->sy)
                    711:                ny = src->hsize + src->sy - sy;
                    712:        grid_clear_lines(dst, dy, ny);
                    713:
                    714:        for (yy = 0; yy < ny; yy++) {
1.11      nicm      715:                srcl = &src->linedata[sy];
                    716:                dstl = &dst->linedata[dy];
1.10      nicm      717:
                    718:                memcpy(dstl, srcl, sizeof *dstl);
                    719:                if (srcl->cellsize != 0) {
1.42      deraadt   720:                        dstl->celldata = xreallocarray(NULL,
1.10      nicm      721:                            srcl->cellsize, sizeof *dstl->celldata);
                    722:                        memcpy(dstl->celldata, srcl->celldata,
                    723:                            srcl->cellsize * sizeof *dstl->celldata);
1.44      nicm      724:                } else
                    725:                        dstl->celldata = NULL;
1.7       nicm      726:
1.48      nicm      727:                if (srcl->extdsize != 0) {
                    728:                        dstl->extdsize = srcl->extdsize;
                    729:                        dstl->extddata = xreallocarray(NULL, dstl->extdsize,
                    730:                            sizeof *dstl->extddata);
                    731:                        memcpy(dstl->extddata, srcl->extddata, dstl->extdsize *
                    732:                            sizeof *dstl->extddata);
                    733:                }
                    734:
1.10      nicm      735:                sy++;
                    736:                dy++;
1.7       nicm      737:        }
1.22      nicm      738: }
                    739:
1.48      nicm      740: /* Copy a section of a line. */
                    741: void
                    742: grid_reflow_copy(struct grid_line *dst_gl, u_int to, struct grid_line *src_gl,
                    743:     u_int from, u_int to_copy)
                    744: {
                    745:        struct grid_cell_entry  *gce;
                    746:        u_int                    i, was;
                    747:
                    748:        memcpy(&dst_gl->celldata[to], &src_gl->celldata[from],
                    749:            to_copy * sizeof *dst_gl->celldata);
                    750:
                    751:        for (i = to; i < to + to_copy; i++) {
                    752:                gce = &dst_gl->celldata[i];
                    753:                if (~gce->flags & GRID_FLAG_EXTENDED)
                    754:                        continue;
                    755:                was = gce->offset;
                    756:
                    757:                dst_gl->extddata = xreallocarray(dst_gl->extddata,
                    758:                    dst_gl->extdsize + 1, sizeof *dst_gl->extddata);
                    759:                gce->offset = dst_gl->extdsize++;
                    760:                memcpy(&dst_gl->extddata[gce->offset], &src_gl->extddata[was],
                    761:                    sizeof *dst_gl->extddata);
                    762:        }
                    763: }
                    764:
1.23      nicm      765: /* Join line data. */
                    766: void
                    767: grid_reflow_join(struct grid *dst, u_int *py, struct grid_line *src_gl,
                    768:     u_int new_x)
                    769: {
                    770:        struct grid_line        *dst_gl = &dst->linedata[(*py) - 1];
                    771:        u_int                    left, to_copy, ox, nx;
                    772:
                    773:        /* How much is left on the old line? */
                    774:        left = new_x - dst_gl->cellsize;
                    775:
                    776:        /* Work out how much to append. */
                    777:        to_copy = src_gl->cellsize;
                    778:        if (to_copy > left)
                    779:                to_copy = left;
                    780:        ox = dst_gl->cellsize;
                    781:        nx = ox + to_copy;
                    782:
                    783:        /* Resize the destination line. */
1.41      nicm      784:        dst_gl->celldata = xreallocarray(dst_gl->celldata, nx,
1.23      nicm      785:            sizeof *dst_gl->celldata);
                    786:        dst_gl->cellsize = nx;
                    787:
                    788:        /* Append as much as possible. */
1.48      nicm      789:        grid_reflow_copy(dst_gl, ox, src_gl, 0, to_copy);
1.23      nicm      790:
                    791:        /* If there is any left in the source, split it. */
                    792:        if (src_gl->cellsize > to_copy) {
                    793:                dst_gl->flags |= GRID_LINE_WRAPPED;
                    794:
                    795:                src_gl->cellsize -= to_copy;
                    796:                grid_reflow_split(dst, py, src_gl, new_x, to_copy);
                    797:        }
                    798: }
                    799:
                    800: /* Split line data. */
                    801: void
                    802: grid_reflow_split(struct grid *dst, u_int *py, struct grid_line *src_gl,
                    803:     u_int new_x, u_int offset)
                    804: {
                    805:        struct grid_line        *dst_gl = NULL;
                    806:        u_int                    to_copy;
                    807:
                    808:        /* Loop and copy sections of the source line. */
                    809:        while (src_gl->cellsize > 0) {
                    810:                /* Create new line. */
                    811:                if (*py >= dst->hsize + dst->sy)
                    812:                        grid_scroll_history(dst);
                    813:                dst_gl = &dst->linedata[*py];
                    814:                (*py)++;
                    815:
                    816:                /* How much should we copy? */
                    817:                to_copy = new_x;
                    818:                if (to_copy > src_gl->cellsize)
                    819:                        to_copy = src_gl->cellsize;
                    820:
                    821:                /* Expand destination line. */
1.41      nicm      822:                dst_gl->celldata = xreallocarray(NULL, to_copy,
1.40      nicm      823:                    sizeof *dst_gl->celldata);
1.23      nicm      824:                dst_gl->cellsize = to_copy;
                    825:                dst_gl->flags |= GRID_LINE_WRAPPED;
                    826:
                    827:                /* Copy the data. */
1.48      nicm      828:                grid_reflow_copy(dst_gl, 0, src_gl, offset, to_copy);
1.23      nicm      829:
                    830:                /* Move offset and reduce old line size. */
                    831:                offset += to_copy;
                    832:                src_gl->cellsize -= to_copy;
                    833:        }
                    834:
                    835:        /* Last line is not wrapped. */
                    836:        if (dst_gl != NULL)
                    837:                dst_gl->flags &= ~GRID_LINE_WRAPPED;
                    838: }
                    839:
                    840: /* Move line data. */
                    841: void
                    842: grid_reflow_move(struct grid *dst, u_int *py, struct grid_line *src_gl)
                    843: {
                    844:        struct grid_line        *dst_gl;
                    845:
                    846:        /* Create new line. */
                    847:        if (*py >= dst->hsize + dst->sy)
                    848:                grid_scroll_history(dst);
                    849:        dst_gl = &dst->linedata[*py];
                    850:        (*py)++;
                    851:
                    852:        /* Copy the old line. */
                    853:        memcpy(dst_gl, src_gl, sizeof *dst_gl);
                    854:        dst_gl->flags &= ~GRID_LINE_WRAPPED;
                    855:
                    856:        /* Clear old line. */
                    857:        src_gl->celldata = NULL;
1.48      nicm      858:        src_gl->extddata = NULL;
1.23      nicm      859: }
                    860:
1.22      nicm      861: /*
1.23      nicm      862:  * Reflow lines from src grid into dst grid of width new_x. Returns number of
                    863:  * lines fewer in the visible area. The source grid is destroyed.
1.22      nicm      864:  */
                    865: u_int
1.23      nicm      866: grid_reflow(struct grid *dst, struct grid *src, u_int new_x)
1.22      nicm      867: {
1.23      nicm      868:        u_int                    py, sy, line;
1.22      nicm      869:        int                      previous_wrapped;
1.23      nicm      870:        struct grid_line        *src_gl;
                    871:
                    872:        py = 0;
                    873:        sy = src->sy;
1.22      nicm      874:
1.23      nicm      875:        previous_wrapped = 0;
                    876:        for (line = 0; line < sy + src->hsize; line++) {
                    877:                src_gl = src->linedata + line;
1.22      nicm      878:                if (!previous_wrapped) {
1.23      nicm      879:                        /* Wasn't wrapped. If smaller, move to destination. */
                    880:                        if (src_gl->cellsize <= new_x)
                    881:                                grid_reflow_move(dst, &py, src_gl);
                    882:                        else
                    883:                                grid_reflow_split(dst, &py, src_gl, new_x, 0);
                    884:                } else {
                    885:                        /* Previous was wrapped. Try to join. */
                    886:                        grid_reflow_join(dst, &py, src_gl, new_x);
1.22      nicm      887:                }
1.48      nicm      888:                previous_wrapped = (src_gl->flags & GRID_LINE_WRAPPED);
1.22      nicm      889:        }
                    890:
1.23      nicm      891:        grid_destroy(src);
                    892:
                    893:        if (py > sy)
1.22      nicm      894:                return (0);
1.23      nicm      895:        return (sy - py);
1.1       nicm      896: }