Files
wlgol/grid.c
T
arek d20f4d5481 fix(input): honor pointer entry position and make clear work
Pointer enter ignored the surface position, so the pattern preview
rendered at a stale cell until the first motion event. The clear
action only printed a message and toggle_grid had no default key
binding, leaving both unreachable without a hand-written config.

Set the cursor position from the enter event, implement clear via
grid_clear plus a dirty flag, and add g/c default bindings in both
the built-in table and the generated config template. Also defer
wl_buffer destruction on resize until the compositor releases it,
route pointer motion through a handler hook instead of reading
pointer_data directly, and clean up dead code (show_lines, unused
ternary, unchecked ftruncate, unfreed grid buffers). Fixes a
wl_callback proxy leak from frame-callback overwrite. Syncs the
keyboard-bindings and display-scaling specs and archives the
fix-review-findings change.
2026-11-11 18:04:50 +01:00

67 lines
1.9 KiB
C

#include "grid.h"
#include <stdlib.h>
#include <string.h>
void grid_init(grid_t *g, int cols, int rows) {
if (!g) return;
if (cols < 1) cols = 1;
if (rows < 1) rows = 1;
g->cols = cols;
g->rows = rows;
g->cells = calloc((size_t)cols * (size_t)rows, 1);
}
void grid_free(grid_t *g) {
if (!g) return;
free(g->cells);
g->cells = NULL;
g->cols = 0;
g->rows = 0;
}
void grid_resize(grid_t *g, int new_cols, int new_rows) {
if (!g) return;
if (new_cols < 1) new_cols = 1;
if (new_rows < 1) new_rows = 1;
unsigned char *ncells = calloc((size_t)new_cols * (size_t)new_rows, 1);
if (!ncells) return;
int copy_cols = g->cols < new_cols ? g->cols : new_cols;
int copy_rows = g->rows < new_rows ? g->rows : new_rows;
for (int r = 0; r < copy_rows; r++) {
memcpy(ncells + (size_t)r * new_cols,
g->cells + (size_t)r * g->cols,
(size_t)copy_cols);
}
free(g->cells);
g->cells = ncells;
g->cols = new_cols;
g->rows = new_rows;
}
void grid_clear(grid_t *g) {
if (!g || !g->cells) return;
memset(g->cells, 0, (size_t)g->cols * (size_t)g->rows);
}
void grid_copy(grid_t *dst, const grid_t *src) {
if (!dst || !src || !src->cells) return;
if (dst->cols != src->cols || dst->rows != src->rows) {
grid_resize(dst, src->cols, src->rows);
}
if (dst->cols == src->cols && dst->rows == src->rows && dst->cells) {
memcpy(dst->cells, src->cells, (size_t)src->cols * (size_t)src->rows);
}
}
int grid_get(const grid_t *g, int c, int r) {
if (!g || !g->cells) return 0;
if (c < 0 || r < 0 || c >= g->cols || r >= g->rows) return 0;
return g->cells[(size_t)r * g->cols + c];
}
void grid_set(grid_t *g, int c, int r, int val) {
if (!g || !g->cells) return;
if (c < 0 || r < 0 || c >= g->cols || r >= g->rows) return;
g->cells[(size_t)r * g->cols + c] = val ? 1 : 0;
}