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

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