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.
This commit is contained in:
2026-11-11 18:04:50 +01:00
parent 2bd0b29bf1
commit d20f4d5481
15 changed files with 102 additions and 29 deletions
+56 -16
View File
@@ -76,6 +76,8 @@ struct canvas
struct wl_buffer *buffer;
unsigned char *data;
struct wl_shm_pool *pool;
struct wl_buffer *old_buffer; /* parked until the compositor releases it */
struct wl_shm_pool *old_pool;
int fd;
struct wl_surface *surface;
struct wl_callback *frame;
@@ -237,7 +239,15 @@ const struct xdg_wm_base_listener xdg_shell_listener = {
void wl_buffer_release_handler(void *data, struct wl_buffer *wl_buffer)
{
// printf("wl_buffer_release\n");
if (canvas.old_buffer && wl_buffer == canvas.old_buffer) {
wl_buffer_destroy(canvas.old_buffer);
canvas.old_buffer = NULL;
if (canvas.old_pool) {
wl_shm_pool_destroy(canvas.old_pool);
canvas.old_pool = NULL;
}
return;
}
released = 1;
}
@@ -261,9 +271,7 @@ static void loop_update(void)
full_redraw = 1;
}
/* update cursor from pointer */
cursor_x = pointer_data.x;
cursor_y = pointer_data.y;
/* cursor position arrives via the pointer motion handler */
/* pattern preview follows the cursor */
if (active_tool >= 0 && (cursor_x != preview_cursor_x || cursor_y != preview_cursor_y)) {
@@ -340,8 +348,10 @@ static void loop_update(void)
} else {
/* nothing new (or buffer still busy): keep the frame callback
* alive without presenting new content */
canvas.frame = wl_surface_frame(canvas.surface);
wl_callback_add_listener(canvas.frame, &wl_surface_frame_listener, NULL);
if (!canvas.frame) {
canvas.frame = wl_surface_frame(canvas.surface);
wl_callback_add_listener(canvas.frame, &wl_surface_frame_listener, NULL);
}
wl_surface_commit(canvas.surface);
}
}
@@ -393,6 +403,8 @@ static void handle_key(uint32_t key, uint32_t state, xkb_keysym_t sym, uint32_t
case ACTION_SELECT_ACORN:
active_tool = 9; active_rotation = 0; screen_dirty = 1; printf("selected acorn\n"); break;
case ACTION_CLEAR:
grid_clear(&grid);
screen_dirty = 1;
printf("clear requested\n");
break;
case ACTION_PAUSE:
@@ -426,6 +438,13 @@ static void handle_key(uint32_t key, uint32_t state, xkb_keysym_t sym, uint32_t
}
}
static void store_cursor(double x, double y, void *user_data)
{
(void)user_data;
cursor_x = x;
cursor_y = y;
}
static void handle_button(uint32_t button, uint32_t state, double x, double y, void *user_data)
{
(void)user_data;
@@ -470,8 +489,20 @@ static void canvas_resize(int log_w, int log_h, double scale) {
if (phys_w < 1) phys_w = 1;
if (phys_h < 1) phys_h = 1;
if (canvas.buffer) { wl_buffer_destroy(canvas.buffer); canvas.buffer = NULL; }
if (canvas.pool) { wl_shm_pool_destroy(canvas.pool); canvas.pool = NULL; }
if (canvas.buffer) {
/* do not destroy while the compositor may still be reading it;
* park it and destroy on release - unless the compositor already
* released it, then there is nothing to wait for */
if (released) {
wl_buffer_destroy(canvas.buffer);
if (canvas.pool) wl_shm_pool_destroy(canvas.pool);
} else {
canvas.old_buffer = canvas.buffer;
canvas.old_pool = canvas.pool;
}
canvas.buffer = NULL;
canvas.pool = NULL;
}
if (canvas.data && canvas.size > 0) { munmap(canvas.data, canvas.size); canvas.data = NULL; }
if (canvas.fd >= 0) { close(canvas.fd); canvas.fd = -1; }
@@ -481,7 +512,9 @@ static void canvas_resize(int log_w, int log_h, double scale) {
canvas.size = canvas.stride * phys_h;
canvas.fd = syscall(SYS_memfd_create, "buffer", 0);
ftruncate(canvas.fd, canvas.size);
if (ftruncate(canvas.fd, canvas.size) != 0) {
printf("ftruncate failed\n");
}
canvas.data = mmap(NULL, canvas.size, PROT_READ | PROT_WRITE, MAP_SHARED, canvas.fd, 0);
canvas.pool = wl_shm_create_pool(shm, canvas.fd, canvas.size);
close(canvas.fd);
@@ -542,6 +575,7 @@ int main(void)
pointer_init_cursor(&pointer_data, compositor, shm);
pointer_set_button_handler(&pointer_data, handle_button, NULL);
pointer_set_motion_handler(&pointer_data, store_cursor, NULL);
pointer_data.pointer = wl_seat_get_pointer(wl_seat);
wl_pointer_add_listener(pointer_data.pointer, &pointer_listener, &pointer_data);
@@ -549,7 +583,6 @@ int main(void)
logical_width = 1024;
logical_height = 576;
grid_init(&grid, logical_width / 8, logical_height / 8);
grid.show_lines = 1;
// wait for the surface to be configured
wl_display_roundtrip(display);
@@ -586,6 +619,11 @@ int main(void)
wl_surface_destroy(canvas.surface);
if (canvas.pool) wl_shm_pool_destroy(canvas.pool);
if (canvas.buffer) wl_buffer_destroy(canvas.buffer);
if (canvas.old_pool) wl_shm_pool_destroy(canvas.old_pool);
if (canvas.old_buffer) wl_buffer_destroy(canvas.old_buffer);
grid_free(&grid);
grid_free(&grid_back);
grid_free(&grid_rendered);
wl_keyboard_destroy(keyboard_data.keyboard);
if (keyboard_data.xkb_state != NULL)
{
@@ -679,7 +717,7 @@ static void repaint_cell(int c, int r, int cell_px)
fill_rect(x, y, w, h, 20, 20, 24, 255);
if (show_grid_lines && grid.show_lines) {
if (show_grid_lines) {
fill_rect(x, y, 1, h, 40, 40, 48, 255);
fill_rect(x, y, w, 1, 40, 40, 48, 255);
if (c == grid.cols - 1 && (c + 1) * cell_px < canvas.width)
@@ -708,8 +746,8 @@ void redraw()
fill_rect(0, 0, canvas.width, canvas.height, 20, 20, 24, 255);
/* grid lines */
if (show_grid_lines && grid.show_lines) {
for (int c = 0; c <= grid.cols; c++) {
if (show_grid_lines) {
for (int c = 0; c <= grid.cols; c++) {
int x = c * cell_px;
fill_rect(x, 0, 1, canvas.height, 40, 40, 48, 255);
}
@@ -771,7 +809,7 @@ void redraw()
int r = cy + dy[i];
if (c < 0 || r < 0 || c >= grid.cols || r >= grid.rows) { ok = 0; break; }
}
unsigned char pr = ok ? 220 : 220;
unsigned char pr = 220;
unsigned char pg = ok ? 200 : 80;
unsigned char pb = ok ? 80 : 80;
for (int i = 0; i < n; i++) {
@@ -794,8 +832,10 @@ void redraw()
grid_copy(&grid_rendered, &grid);
// callback
canvas.frame = wl_surface_frame(canvas.surface);
wl_callback_add_listener(canvas.frame, &wl_surface_frame_listener, NULL);
if (!canvas.frame) {
canvas.frame = wl_surface_frame(canvas.surface);
wl_callback_add_listener(canvas.frame, &wl_surface_frame_listener, NULL);
}
wl_surface_attach(canvas.surface, canvas.buffer, 0, 0);
if (full_redraw) {
wl_surface_damage(canvas.surface, 0, 0, logical_width, logical_height);