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

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