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

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