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

1.12    ! nicm        1: /* $OpenBSD: grid.c,v 1.11 2009/08/10 17:59:59 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
                      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:
                     21: #include <string.h>
                     22:
                     23: #include "tmux.h"
                     24:
                     25: /*
                     26:  * Grid data. This is the basic data structure that represents what is shown on
                     27:  * screen.
                     28:  *
                     29:  * A grid is a grid of cells (struct grid_cell). Lines are not allocated until
                     30:  * cells in that line are written to. The grid is split into history and
                     31:  * viewable data with the history starting at row (line) 0 and extending to
                     32:  * (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
                     33:  * functions in this file work on absolute coordinates, grid-view.c has
                     34:  * functions which work on the screen data.
                     35:  */
                     36:
                     37: /* Default grid cell data. */
                     38: const struct grid_cell grid_default_cell = { 0, 0, 8, 8, ' ' };
                     39:
                     40: #define grid_put_cell(gd, px, py, gc) do {                     \
1.10      nicm       41:        memcpy(&gd->linedata[py].celldata[px],                  \
                     42:            gc, sizeof gd->linedata[py].celldata[px]);          \
1.1       nicm       43: } while (0)
                     44: #define grid_put_utf8(gd, px, py, gc) do {                     \
1.10      nicm       45:        memcpy(&gd->linedata[py].utf8data[px],                  \
                     46:            gc, sizeof gd->linedata[py].utf8data[px]);          \
1.1       nicm       47: } while (0)
                     48:
                     49: int    grid_check_x(struct grid *, u_int);
                     50: int    grid_check_y(struct grid *, u_int);
                     51:
                     52: #ifdef DEBUG
                     53: int
                     54: grid_check_x(struct grid *gd, u_int px)
                     55: {
                     56:        if ((px) >= (gd)->sx)
                     57:                log_fatalx("x out of range: %u", px);
                     58:        return (0);
                     59: }
                     60:
                     61: int
                     62: grid_check_y(struct grid *gd, u_int py)
                     63: {
                     64:        if ((py) >= (gd)->hsize + (gd)->sy)
                     65:                log_fatalx("y out of range: %u", py);
                     66:        return (0);
                     67: }
                     68: #else
                     69: int
                     70: grid_check_x(struct grid *gd, u_int px)
                     71: {
                     72:        if ((px) >= (gd)->sx) {
                     73:                log_debug("x out of range: %u", px);
                     74:                return (-1);
                     75:        }
                     76:        return (0);
                     77: }
                     78:
                     79: int
                     80: grid_check_y(struct grid *gd, u_int py)
                     81: {
                     82:        if ((py) >= (gd)->hsize + (gd)->sy) {
                     83:                log_debug("y out of range: %u", py);
                     84:                return (-1);
                     85:        }
                     86:        return (0);
                     87: }
                     88: #endif
                     89:
                     90: /* Create a new grid. */
                     91: struct grid *
                     92: grid_create(u_int sx, u_int sy, u_int hlimit)
                     93: {
                     94:        struct grid     *gd;
                     95:
                     96:        gd = xmalloc(sizeof *gd);
                     97:        gd->sx = sx;
                     98:        gd->sy = sy;
                     99:
1.7       nicm      100:        gd->flags = GRID_HISTORY;
                    101:
1.1       nicm      102:        gd->hsize = 0;
                    103:        gd->hlimit = hlimit;
                    104:
1.10      nicm      105:        gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
1.1       nicm      106:
                    107:        return (gd);
                    108: }
                    109:
                    110: /* Destroy grid. */
                    111: void
                    112: grid_destroy(struct grid *gd)
                    113: {
1.10      nicm      114:        struct grid_line        *gl;
                    115:        u_int                    yy;
1.1       nicm      116:
                    117:        for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
1.10      nicm      118:                gl = &gd->linedata[yy];
                    119:                if (gl->celldata != NULL)
                    120:                        xfree(gl->celldata);
                    121:                if (gl->utf8data != NULL)
                    122:                        xfree(gl->utf8data);
1.1       nicm      123:        }
                    124:
1.10      nicm      125:        xfree(gd->linedata);
1.1       nicm      126:
                    127:        xfree(gd);
                    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.1       nicm      135:        struct grid_cell        *gca, *gcb;
                    136:        struct grid_utf8        *gua, *gub;
                    137:        u_int                    xx, yy;
                    138:
                    139:        if (ga->sx != gb->sx || ga->sy != ga->sy)
                    140:                return (1);
                    141:
                    142:        for (yy = 0; yy < ga->sy; yy++) {
1.10      nicm      143:                gla = &ga->linedata[yy];
                    144:                glb = &gb->linedata[yy];
                    145:                if (gla->cellsize != glb->cellsize)
1.1       nicm      146:                        return (1);
                    147:                for (xx = 0; xx < ga->sx; xx++) {
1.10      nicm      148:                        gca = &gla->celldata[xx];
                    149:                        gcb = &glb->celldata[xx];
1.1       nicm      150:                        if (memcmp(gca, gcb, sizeof (struct grid_cell)) != 0)
                    151:                                return (1);
                    152:                        if (!(gca->flags & GRID_FLAG_UTF8))
                    153:                                continue;
1.10      nicm      154:                        gua = &gla->utf8data[xx];
                    155:                        gub = &glb->utf8data[xx];
1.1       nicm      156:                        if (memcmp(gua, gub, sizeof (struct grid_utf8)) != 0)
                    157:                                return (1);
                    158:                }
                    159:        }
                    160:
                    161:        return (0);
                    162: }
                    163:
                    164: /* Scroll a line into the history. */
                    165: void
                    166: grid_scroll_line(struct grid *gd)
                    167: {
                    168:        u_int   yy;
                    169:
                    170:        GRID_DEBUG(gd, "");
                    171:
1.6       nicm      172:        if (gd->hsize >= gd->hlimit) {
1.1       nicm      173:                /* If the limit is hit, free the bottom 10% and shift up. */
                    174:                yy = gd->hlimit / 10;
                    175:                if (yy < 1)
                    176:                        yy = 1;
                    177:
                    178:                grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);
                    179:                gd->hsize -= yy;
                    180:        }
                    181:
                    182:        yy = gd->hsize + gd->sy;
                    183:
1.10      nicm      184:        gd->linedata = xrealloc(gd->linedata, yy + 1, sizeof *gd->linedata);
                    185:        memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1       nicm      186:
                    187:        gd->hsize++;
                    188: }
                    189:
                    190: /* Expand line to fit to cell. */
                    191: void
                    192: grid_expand_line(struct grid *gd, u_int py, u_int sx)
                    193: {
1.10      nicm      194:        struct grid_line        *gl;
                    195:        u_int                    xx;
1.1       nicm      196:
1.10      nicm      197:        gl = &gd->linedata[py];
                    198:        if (sx <= gl->cellsize)
1.1       nicm      199:                return;
                    200:
1.12    ! nicm      201:        if (gl->cellsize > gd->sx / 2)
        !           202:                sx = gd->sx;
        !           203:        else
        !           204:                sx = 1 + gl->cellsize * 2;
1.10      nicm      205:        gl->celldata = xrealloc(gl->celldata, sx, sizeof *gl->celldata);
                    206:        for (xx = gl->cellsize; xx < sx; xx++)
1.1       nicm      207:                grid_put_cell(gd, xx, py, &grid_default_cell);
1.10      nicm      208:        gl->cellsize = sx;
1.1       nicm      209: }
                    210:
                    211: /* Expand line to fit to cell for UTF-8. */
                    212: void
                    213: grid_expand_line_utf8(struct grid *gd, u_int py, u_int sx)
                    214: {
1.10      nicm      215:        struct grid_line        *gl;
                    216:
                    217:        gl = &gd->linedata[py];
                    218:        if (sx <= gl->utf8size)
1.1       nicm      219:                return;
                    220:
1.10      nicm      221:        gl->utf8data = xrealloc(gl->utf8data, sx, sizeof *gl->utf8data);
                    222:        gl->utf8size = sx;
1.1       nicm      223: }
                    224:
                    225: /* Get cell for reading. */
                    226: const struct grid_cell *
                    227: grid_peek_cell(struct grid *gd, u_int px, u_int py)
                    228: {
                    229:        if (grid_check_x(gd, px) != 0)
                    230:                return (&grid_default_cell);
                    231:        if (grid_check_y(gd, py) != 0)
                    232:                return (&grid_default_cell);
                    233:
1.10      nicm      234:        if (px >= gd->linedata[py].cellsize)
1.1       nicm      235:                return (&grid_default_cell);
1.10      nicm      236:        return (&gd->linedata[py].celldata[px]);
1.1       nicm      237: }
                    238:
                    239: /* Get cell at relative position (for writing). */
                    240: struct grid_cell *
                    241: grid_get_cell(struct grid *gd, u_int px, u_int py)
                    242: {
                    243:        if (grid_check_x(gd, px) != 0)
                    244:                return (NULL);
                    245:        if (grid_check_y(gd, py) != 0)
                    246:                return (NULL);
                    247:
                    248:        grid_expand_line(gd, py, px + 1);
1.10      nicm      249:        return (&gd->linedata[py].celldata[px]);
1.1       nicm      250: }
                    251:
                    252: /* Set cell at relative position. */
                    253: void
                    254: grid_set_cell(
                    255:     struct grid *gd, u_int px, u_int py, const struct grid_cell *gc)
                    256: {
                    257:        if (grid_check_x(gd, px) != 0)
                    258:                return;
                    259:        if (grid_check_y(gd, py) != 0)
                    260:                return;
                    261:
                    262:        grid_expand_line(gd, py, px + 1);
                    263:        grid_put_cell(gd, px, py, gc);
                    264: }
                    265:
                    266: /* Get UTF-8 for reading. */
                    267: const struct grid_utf8 *
                    268: grid_peek_utf8(struct grid *gd, u_int px, u_int py)
                    269: {
                    270:        if (grid_check_x(gd, px) != 0)
                    271:                return (NULL);
                    272:        if (grid_check_y(gd, py) != 0)
                    273:                return (NULL);
                    274:
1.10      nicm      275:        if (px >= gd->linedata[py].utf8size)
1.1       nicm      276:                return (NULL);
1.10      nicm      277:        return (&gd->linedata[py].utf8data[px]);
1.1       nicm      278: }
                    279:
                    280: /* Get utf8 at relative position (for writing). */
                    281: struct grid_utf8 *
                    282: grid_get_utf8(struct grid *gd, u_int px, u_int py)
                    283: {
                    284:        if (grid_check_x(gd, px) != 0)
                    285:                return (NULL);
                    286:        if (grid_check_y(gd, py) != 0)
                    287:                return (NULL);
                    288:
                    289:        grid_expand_line_utf8(gd, py, px + 1);
1.10      nicm      290:        return (&gd->linedata[py].utf8data[px]);
1.1       nicm      291: }
                    292:
                    293: /* Set utf8 at relative position. */
                    294: void
                    295: grid_set_utf8(
                    296:     struct grid *gd, u_int px, u_int py, const struct grid_utf8 *gc)
                    297: {
                    298:        if (grid_check_x(gd, px) != 0)
                    299:                return;
                    300:        if (grid_check_y(gd, py) != 0)
                    301:                return;
                    302:
                    303:        grid_expand_line_utf8(gd, py, px + 1);
                    304:        grid_put_utf8(gd, px, py, gc);
                    305: }
                    306:
                    307: /*
                    308:  * Clear area. Note this is different from a fill as it just omits unallocated
                    309:  * cells.
                    310:  */
                    311: void
                    312: grid_clear(struct grid *gd, u_int px, u_int py, u_int nx, u_int ny)
                    313: {
                    314:        u_int   xx, yy;
                    315:
                    316:        GRID_DEBUG(gd, "px=%u, py=%u, nx=%u, ny=%u", px, py, nx, ny);
                    317:
                    318:        if (nx == 0 || ny == 0)
                    319:                return;
                    320:
                    321:        if (px == 0 && nx == gd->sx) {
                    322:                grid_clear_lines(gd, py, ny);
                    323:                return;
                    324:        }
                    325:
                    326:        if (grid_check_x(gd, px) != 0)
                    327:                return;
                    328:        if (grid_check_x(gd, px + nx - 1) != 0)
                    329:                return;
                    330:        if (grid_check_y(gd, py) != 0)
                    331:                return;
                    332:        if (grid_check_y(gd, py + ny - 1) != 0)
                    333:                return;
                    334:
                    335:        for (yy = py; yy < py + ny; yy++) {
                    336:                for (xx = px; xx < px + nx; xx++) {
1.10      nicm      337:                        if (xx >= gd->linedata[yy].cellsize)
1.1       nicm      338:                                break;
                    339:                        grid_put_cell(gd, xx, yy, &grid_default_cell);
                    340:                }
                    341:        }
                    342: }
                    343:
                    344: /* Clear lines. This just frees and truncates the lines. */
                    345: void
                    346: grid_clear_lines(struct grid *gd, u_int py, u_int ny)
                    347: {
1.10      nicm      348:        struct grid_line        *gl;
                    349:        u_int                    yy;
1.1       nicm      350:
                    351:        GRID_DEBUG(gd, "py=%u, ny=%u", py, ny);
                    352:
                    353:        if (ny == 0)
                    354:                return;
                    355:
                    356:        if (grid_check_y(gd, py) != 0)
                    357:                return;
                    358:        if (grid_check_y(gd, py + ny - 1) != 0)
                    359:                return;
                    360:
                    361:        for (yy = py; yy < py + ny; yy++) {
1.10      nicm      362:                gl = &gd->linedata[yy];
                    363:                if (gl->celldata != NULL)
                    364:                        xfree(gl->celldata);
                    365:                if (gl->utf8data != NULL)
                    366:                        xfree(gl->utf8data);
                    367:                memset(gl, 0, sizeof *gl);
1.1       nicm      368:        }
                    369: }
                    370:
                    371: /* Move a group of lines. */
                    372: void
                    373: grid_move_lines(struct grid *gd, u_int dy, u_int py, u_int ny)
                    374: {
                    375:        u_int   yy;
                    376:
                    377:        GRID_DEBUG(gd, "dy=%u, py=%u, ny=%u", dy, py, ny);
                    378:
                    379:        if (ny == 0 || py == dy)
                    380:                return;
                    381:
                    382:        if (grid_check_y(gd, py) != 0)
                    383:                return;
                    384:        if (grid_check_y(gd, py + ny - 1) != 0)
                    385:                return;
                    386:        if (grid_check_y(gd, dy) != 0)
                    387:                return;
                    388:        if (grid_check_y(gd, dy + ny - 1) != 0)
                    389:                return;
                    390:
                    391:        /* Free any lines which are being replaced. */
                    392:        for (yy = dy; yy < dy + ny; yy++) {
                    393:                if (yy >= py && yy < py + ny)
                    394:                        continue;
                    395:                grid_clear_lines(gd, yy, 1);
                    396:        }
                    397:
1.10      nicm      398:        memmove(
                    399:            &gd->linedata[dy], &gd->linedata[py], ny * (sizeof *gd->linedata));
1.1       nicm      400:
                    401:        /* Wipe any lines that have been moved (without freeing them). */
                    402:        for (yy = py; yy < py + ny; yy++) {
                    403:                if (yy >= dy && yy < dy + ny)
                    404:                        continue;
1.10      nicm      405:                memset(&gd->linedata[yy], 0, sizeof gd->linedata[yy]);
1.1       nicm      406:        }
                    407: }
                    408:
                    409: /* Move a group of cells. */
                    410: void
                    411: grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
                    412: {
1.10      nicm      413:        struct grid_line        *gl;
                    414:        u_int                    xx;
1.1       nicm      415:
                    416:        GRID_DEBUG(gd, "dx=%u, px=%u, py=%u, nx=%u", dx, px, py, nx);
                    417:
                    418:        if (nx == 0 || px == dx)
                    419:                return;
                    420:
                    421:        if (grid_check_x(gd, px) != 0)
                    422:                return;
                    423:        if (grid_check_x(gd, px + nx - 1) != 0)
                    424:                return;
                    425:        if (grid_check_x(gd, dx + nx - 1) != 0)
                    426:                return;
                    427:        if (grid_check_y(gd, py) != 0)
                    428:                return;
1.10      nicm      429:        gl = &gd->linedata[py];
1.1       nicm      430:
                    431:        grid_expand_line(gd, py, px + nx);
                    432:        grid_expand_line(gd, py, dx + nx);
1.10      nicm      433:        memmove(
                    434:            &gl->celldata[dx], &gl->celldata[px], nx * sizeof *gl->celldata);
1.1       nicm      435:
1.10      nicm      436:        if (gl->utf8data != NULL) {
1.1       nicm      437:                grid_expand_line_utf8(gd, py, px + nx);
                    438:                grid_expand_line_utf8(gd, py, dx + nx);
1.10      nicm      439:                memmove(&gl->utf8data[dx],
                    440:                    &gl->utf8data[px], nx * sizeof *gl->utf8data);
1.1       nicm      441:        }
                    442:
                    443:        /* Wipe any cells that have been moved. */
                    444:        for (xx = px; xx < px + nx; xx++) {
                    445:                if (xx >= dx && xx < dx + nx)
                    446:                        continue;
                    447:                grid_put_cell(gd, xx, py, &grid_default_cell);
                    448:        }
1.3       nicm      449: }
                    450:
                    451: /* Convert cells into a string. */
                    452: char *
                    453: grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
                    454: {
                    455:        const struct grid_cell  *gc;
                    456:        const struct grid_utf8  *gu;
                    457:        char                    *buf;
                    458:        size_t                   len, off;
1.4       nicm      459:        u_int                    xx, i;
1.3       nicm      460:
                    461:        GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
                    462:
                    463:        len = 128;
                    464:        buf = xmalloc(len);
                    465:        off = 0;
                    466:
                    467:        for (xx = px; xx < px + nx; xx++) {
                    468:                gc = grid_peek_cell(gd, xx, py);
                    469:                if (gc->flags & GRID_FLAG_PADDING)
                    470:                        continue;
                    471:
                    472:                if (gc->flags & GRID_FLAG_UTF8) {
                    473:                        while (len < off + UTF8_SIZE + 1) {
                    474:                                buf = xrealloc(buf, 2, len);
                    475:                                len *= 2;
                    476:                        }
                    477:
                    478:                        gu = grid_peek_utf8(gd, xx, py);
1.4       nicm      479:                        for (i = 0; i < UTF8_SIZE; i++) {
                    480:                                if (gu->data[i] == 0xff)
                    481:                                        break;
                    482:                                buf[off++] = gu->data[i];
                    483:                        }
1.3       nicm      484:                } else {
                    485:                        while (len < off + 2) {
                    486:                                buf = xrealloc(buf, 2, len);
                    487:                                len *= 2;
                    488:                        }
                    489:
                    490:                        buf[off++] = gc->data;
                    491:                }
                    492:        }
1.4       nicm      493:
                    494:        while (off > 0 && buf[off - 1] == ' ')
                    495:                off--;
1.3       nicm      496:        buf[off] = '\0';
                    497:        return (buf);
1.7       nicm      498: }
                    499:
                    500: /*
                    501:  * Duplicate a set of lines between two grids. If there aren't enough lines in
                    502:  * either source or destination, the number of lines is limited to the number
                    503:  * available.
                    504:  */
                    505: void
                    506: grid_duplicate_lines(
                    507:     struct grid *dst, u_int dy, struct grid *src, u_int sy, u_int ny)
                    508: {
1.10      nicm      509:        struct grid_line        *dstl, *srcl;
                    510:        u_int                    yy;
1.7       nicm      511:
                    512:        GRID_DEBUG(src, "dy=%u, sy=%u, ny=%u", dy, sy, ny);
                    513:
                    514:        if (dy + ny > dst->hsize + dst->sy)
                    515:                ny = dst->hsize + dst->sy - dy;
                    516:        if (sy + ny > src->hsize + src->sy)
                    517:                ny = src->hsize + src->sy - sy;
                    518:        grid_clear_lines(dst, dy, ny);
                    519:
                    520:        for (yy = 0; yy < ny; yy++) {
1.11      nicm      521:                srcl = &src->linedata[sy];
                    522:                dstl = &dst->linedata[dy];
1.10      nicm      523:
                    524:                memcpy(dstl, srcl, sizeof *dstl);
                    525:                if (srcl->cellsize != 0) {
                    526:                        dstl->celldata = xcalloc(
                    527:                            srcl->cellsize, sizeof *dstl->celldata);
                    528:                        memcpy(dstl->celldata, srcl->celldata,
                    529:                            srcl->cellsize * sizeof *dstl->celldata);
1.7       nicm      530:                }
1.10      nicm      531:                if (srcl->utf8size != 0) {
                    532:                        dstl->utf8data = xcalloc(
                    533:                            srcl->utf8size, sizeof *dstl->utf8data);
                    534:                        memcpy(dstl->utf8data, srcl->utf8data,
                    535:                            srcl->utf8size * sizeof *dstl->utf8data);
1.7       nicm      536:                }
                    537:
1.10      nicm      538:                sy++;
                    539:                dy++;
1.7       nicm      540:        }
1.1       nicm      541: }