grid: add grid_snapshot()

This function deep copies a grid into a newly *allocated* grid struct.
This commit is contained in:
Daniel Eklöf 2021-02-22 10:21:58 +01:00
parent bb74fe3f7d
commit ae3ec52507
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 83 additions and 0 deletions

82
grid.c
View file

@ -8,9 +8,91 @@
#include "debug.h"
#include "macros.h"
#include "sixel.h"
#include "stride.h"
#include "util.h"
#include "xmalloc.h"
struct grid *
grid_snapshot(const struct grid *grid)
{
struct grid *clone = xmalloc(sizeof(*clone));
clone->num_rows = grid->num_rows;
clone->num_cols = grid->num_cols;
clone->offset = grid->offset;
clone->view = grid->view;
clone->cursor = grid->cursor;
clone->rows = xcalloc(grid->num_rows, sizeof(clone->rows[0]));
memset(&clone->scroll_damage, 0, sizeof(clone->scroll_damage));
memset(&clone->sixel_images, 0, sizeof(clone->sixel_images));
for (int r = 0; r < grid->num_rows; r++) {
const struct row *row = grid->rows[r];
if (row == NULL)
continue;
struct row *clone_row = xmalloc(sizeof(*row));
clone->rows[r] = clone_row;
clone_row->cells = xmalloc(grid->num_cols * sizeof(clone_row->cells[0]));
clone_row->linebreak = row->linebreak;
clone_row->dirty = row->dirty;;
for (int c = 0; c < grid->num_cols; c++) {
clone_row->cells[c] = row->cells[c];
clone_row->cells[c].attrs.clean = 0;
}
if (row->extra != NULL) {
const struct row_data *extra = row->extra;
struct row_data *new_extra = xcalloc(1, sizeof(*new_extra));
tll_foreach(extra->uri_ranges, it) {
struct row_uri_range range = {
.start = it->item.start,
.end = it->item.end,
.id = it->item.id,
.uri = xstrdup(it->item.uri),
};
tll_push_back(new_extra->uri_ranges, range);
}
clone_row->extra = new_extra;
} else
clone_row->extra = NULL;
}
tll_foreach(grid->sixel_images, it) {
int width = it->item.width;
int height = it->item.height;
pixman_image_t *pix = it->item.pix;
pixman_format_code_t pix_fmt = pixman_image_get_format(pix);
int stride = stride_for_format_and_width(pix_fmt, width);
size_t size = stride * height;
void *new_data = xmalloc(size);
memcpy(new_data, it->item.data, size);
pixman_image_t *new_pix = pixman_image_create_bits_no_clear(
pix_fmt, width, height, new_data, stride);
struct sixel six = {
.data = new_data,
.pix = new_pix,
.width = width,
.height = height,
.rows = it->item.rows,
.cols = it->item.cols,
.pos = it->item.pos,
};
tll_push_back(clone->sixel_images, six);
}
return clone;
}
void
grid_free(struct grid *grid)
{

1
grid.h
View file

@ -4,6 +4,7 @@
#include "debug.h"
#include "terminal.h"
struct grid *grid_snapshot(const struct grid *grid);
void grid_free(struct grid *grid);
void grid_swap_row(struct grid *grid, int row_a, int row_b);