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

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