/* $OpenBSD$ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include "tmux.h"
/*
* Grid data. This is the basic data structure that represents what is shown on
* screen.
*
* A grid is a grid of cells (struct grid_cell). Lines are not allocated until
* cells in that line are written to. The grid is split into history and
* viewable data with the history starting at row (line) 0 and extending to
* (hsize - 1); from hsize to hsize + (sy - 1) is the viewable data. All
* functions in this file work on absolute coordinates, grid-view.c has
* functions which work on the screen data.
*/
/* Default grid cell data. */
const struct grid_cell grid_default_cell = {
0, 0, 8, 8, { { ' ' }, 0, 1, 1 }
};
const struct grid_cell_entry grid_default_entry = {
0, { .data = { 0, 8, 8, ' ' } }
};
static void grid_reflow_copy(struct grid_line *, u_int, struct grid_line *,
u_int, u_int);
static void grid_reflow_join(struct grid *, u_int *, struct grid_line *,
u_int);
static void grid_reflow_split(struct grid *, u_int *, struct grid_line *,
u_int, u_int);
static void grid_reflow_move(struct grid *, u_int *, struct grid_line *);
static size_t grid_string_cells_fg(const struct grid_cell *, int *);
static size_t grid_string_cells_bg(const struct grid_cell *, int *);
static void grid_string_cells_code(const struct grid_cell *,
const struct grid_cell *, char *, size_t, int);
/* Copy default into a cell. */
static void
grid_clear_cell(struct grid *gd, u_int px, u_int py)
{
gd->linedata[py].celldata[px] = grid_default_entry;
}
/* Check grid y position. */
static int
grid_check_y(struct grid *gd, u_int py)
{
if ((py) >= (gd)->hsize + (gd)->sy) {
log_debug("y out of range: %u", py);
return (-1);
}
return (0);
}
/* Compare grid cells. Return 1 if equal, 0 if not. */
int
grid_cells_equal(const struct grid_cell *gca, const struct grid_cell *gcb)
{
if (gca->fg != gcb->fg || gca->bg != gcb->bg)
return (0);
if (gca->attr != gcb->attr || gca->flags != gcb->flags)
return (0);
if (gca->data.width != gcb->data.width)
return (0);
if (gca->data.size != gcb->data.size)
return (0);
return (memcmp(gca->data.data, gcb->data.data, gca->data.size) == 0);
}
/* Create a new grid. */
struct grid *
grid_create(u_int sx, u_int sy, u_int hlimit)
{
struct grid *gd;
gd = xmalloc(sizeof *gd);
gd->sx = sx;
gd->sy = sy;
gd->flags = GRID_HISTORY;
gd->hscrolled = 0;
gd->hsize = 0;
gd->hlimit = hlimit;
gd->linedata = xcalloc(gd->sy, sizeof *gd->linedata);
return (gd);
}
/* Destroy grid. */
void
grid_destroy(struct grid *gd)
{
struct grid_line *gl;
u_int yy;
for (yy = 0; yy < gd->hsize + gd->sy; yy++) {
gl = &gd->linedata[yy];
free(gl->celldata);
free(gl->extddata);
}
free(gd->linedata);
free(gd);
}
/* Compare grids. */
int
grid_compare(struct grid *ga, struct grid *gb)
{
struct grid_line *gla, *glb;
struct grid_cell gca, gcb;
u_int xx, yy;
if (ga->sx != gb->sx || ga->sy != gb->sy)
return (1);
for (yy = 0; yy < ga->sy; yy++) {
gla = &ga->linedata[yy];
glb = &gb