/* $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 }
};
static const struct grid_cell_entry grid_default_entry = {
0, { .data = { 0, 8, 8, ' ' } }
};
static void grid_expand_line(struct grid *, u_int, u_int, u_int);
static void grid_empty_line(struct grid *, u_int, u_int);
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);
/* Store cell in entry. */
static void
grid_store_cell(struct grid_cell_entry *gce, const struct grid_cell *gc,
u_char c)
{
gce->flags = gc->flags;
gce->data.fg = gc->fg & 0xff;
if (gc->fg & COLOUR_FLAG_256)
gce->flags |= GRID_FLAG_FG256;
gce->data.bg = gc->bg & 0xff;
if (gc->bg & COLOUR_FLAG_256)
gce->flags |= GRID_FLAG_BG256;
gce->data.attr = gc->attr;
gce->data.data = c;
}
/* Check if a cell should be extended. */
static int
grid_need_extended_cell(const struct grid_cell_entry *gce,
const struct grid_cell *gc)
{
if (gce->flags & GRID_FLAG_EXTENDED)
return (1);
if (gc->data.size != 1 || gc->data.width != 1)
return (1);
if ((gc->fg & COLOUR_FLAG_RGB) ||(gc->bg & COLOUR_FLAG_RGB))
return (1);
return (0);
}
/* Set cell as extended. */
static struct grid_cell *
grid_extended_cell(struct grid_line *gl, struct grid_cell_entry *gce,
const struct grid_cell *gc)
{
struct grid_cell *gcp;
gl->flags |= GRID_LINE_EXTENDED;
if (~gce->flags & GRID_FLAG_EXTENDED) {
gl->extddata = xreallocarray(gl->extddata, gl->extdsize + 1,
sizeof *gl->extddata);
gce->offset = gl->extdsize++;
gce->flags = gc->flags | GRID_FLAG_EXTENDED;
}
if (gce->offset >= gl->extdsize)
fatalx("offset too big");
gcp = &gl->extddata[gce->offset];
memcpy(gcp, gc, sizeof *gcp);
return (gcp);
}
/* Copy default into a cell. */
static void
grid_clear_cell(struct grid *gd, u_int px,