/* $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, { .fg = 8 }, { .bg = 8 }, { { ' ' }, 0, 1, 1 }
};
const struct grid_cell_entry grid_default_entry = {
0, { .data = { 0, 8, 8, ' ' } }
};
int grid_check_y(struct grid *, u_int);
void grid_reflow_copy(struct grid_line *, u_int, struct grid_line *l,
u_int, u_int);
void grid_reflow_join(struct grid *, u_int *, struct grid_line *, u_int);
void grid_reflow_split(struct grid *, u_int *, struct grid_line *, u_int,
u_int);
void grid_reflow_move(struct grid *, u_int *, struct grid_line *);
size_t grid_string_cells_fg(const struct grid_cell *, int *);
size_t grid_string_cells_bg(const struct grid_cell *, int *);
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. */
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);
}
/* 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->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->linedata[yy];
if (gla->cellsize != glb->cellsize)
return (1);
for (xx = 0; xx < gla->cellsize; xx++) {
grid_get_cell(ga, xx, yy, &gca);
grid_get_cell(gb, xx, yy, &gcb);
if (memcmp(&gca, &gcb, sizeof (struct grid_cell)) != 0)
return (1);
}
}
return (0);
}
/*
* Collect lines from the history if at the limit. Free the top (oldest) 10%
* and shift up.
*/
void
grid_collect_history(struct grid *gd)
{
u_int yy;
if (gd->hsize < gd->hlimit)
return;
yy = gd->hlimit / 10;
if (yy < 1)
yy = 1;
grid_move_lines(gd, 0, yy, gd->hsize + gd->sy - yy);