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

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