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
+5 -1
View File
@@ -147,6 +147,8 @@ config_t config_defaults(void) {
{"n", ACTION_STEP},
{"+", ACTION_SPEED_UP},
{"-", ACTION_SPEED_DOWN},
{"g", ACTION_TOGGLE_GRID},
{"c", ACTION_CLEAR},
};
int ndef = sizeof(defs) / sizeof(defs[0]);
for (int i = 0; i < ndef; i++) {
@@ -180,7 +182,9 @@ static const char *default_file_content =
"\"p\" = \"pause\"\n"
"\"n\" = \"step\"\n"
"\"+\" = \"speed_up\"\n"
"\"-\" = \"speed_down\"\n";
"\"-\" = \"speed_down\"\n"
"\"g\" = \"toggle_grid\"\n"
"\"c\" = \"clear\"\n";
int config_ensure_default_file(const char *path) {
if (!path) return -1;
-1
View File
@@ -10,7 +10,6 @@ void grid_init(grid_t *g, int cols, int rows) {
g->cols = cols;
g->rows = rows;
g->cells = calloc((size_t)cols * (size_t)rows, 1);
g->show_lines = 1;
}
void grid_free(grid_t *g) {
-1
View File
@@ -5,7 +5,6 @@ typedef struct {
int cols;
int rows;
unsigned char *cells;
int show_lines;
} grid_t;
void grid_init(grid_t *g, int cols, int rows);
+4
View File
@@ -6,6 +6,7 @@
typedef void (*pointer_button_handler_t)(uint32_t button, uint32_t state, double x, double y, void *user_data);
typedef void (*pointer_motion_handler_t)(double x, double y, void *user_data);
struct pointer_data {
struct wl_cursor_theme *cursor_theme;
@@ -19,6 +20,7 @@ struct pointer_data {
double y;
pointer_button_handler_t button_handler;
pointer_motion_handler_t motion_handler;
void *user_data;
};
@@ -28,4 +30,6 @@ void pointer_init_cursor(struct pointer_data *pointer_data, struct wl_compositor
void pointer_set_button_handler(struct pointer_data *pointer_data, pointer_button_handler_t handler, void *user_data);
void pointer_set_motion_handler(struct pointer_data *pointer_data, pointer_motion_handler_t handler, void *user_data);
#endif
@@ -2,21 +2,21 @@
## 1. Input fixes
- [ ] 1.1 Set `pd->x`/`pd->y` in `pointer_enter_handler` from `wl_fixed_to_double(sx/sy)` in `tpointer.c`; verify by running the app and confirming the pattern preview renders at the entry cell when the pointer enters without further motion
- [ ] 1.2 Add `pointer_set_motion_handler()` to `tpointer.c`/`tpointer.h` mirroring the button-handler pattern, register it in `wlgol.c` to store logical cursor position, and stop reading `pointer_data.x/y` in `loop_update()`; verify with `cmake --build ./build` and `ctest --test-dir build` plus a run checking that painting still follows the cursor
- [x] 1.1 Set `pd->x`/`pd->y` in `pointer_enter_handler` from `wl_fixed_to_double(sx/sy)` in `tpointer.c`; verify by running the app and confirming the pattern preview renders at the entry cell when the pointer enters without further motion
- [x] 1.2 Add `pointer_set_motion_handler()` to `tpointer.c`/`tpointer.h` mirroring the button-handler pattern, register it in `wlgol.c` to store logical cursor position, and stop reading `pointer_data.x/y` in `loop_update()`; verify with `cmake --build ./build` and `ctest --test-dir build` plus a run checking that painting still follows the cursor
## 2. Actions and default bindings
- [ ] 2.1 Implement `ACTION_CLEAR` in `wlgol.c` as `grid_clear(&grid)` + `screen_dirty = 1`; verify by pressing the clear binding with cells on the grid and observing them disappear
- [ ] 2.2 Add `"g" = "toggle_grid"` and `"c" = "clear"` to `default_file_content` in `config.c` and to the built-in defaults; verify with `ctest --test-dir build` (config tests) and a run pressing both keys on a fresh default config
- [ ] 2.3 Extend `tests/test_config.c` to assert lookup of the new default bindings (`g` -> ACTION_TOGGLE_GRID, `c` -> ACTION_CLEAR) and verify with `ctest --test-dir build`
- [x] 2.1 Implement `ACTION_CLEAR` in `wlgol.c` as `grid_clear(&grid)` + `screen_dirty = 1`; verify by pressing the clear binding with cells on the grid and observing them disappear
- [x] 2.2 Add `"g" = "toggle_grid"` and `"c" = "clear"` to `default_file_content` in `config.c` and to the built-in defaults; verify with `ctest --test-dir build` (config tests) and a run pressing both keys on a fresh default config
- [x] 2.3 Extend `tests/test_config.c` to assert lookup of the new default bindings (`g` -> ACTION_TOGGLE_GRID, `c` -> ACTION_CLEAR) and verify with `ctest --test-dir build`
## 3. Buffer lifecycle on resize
- [ ] 3.1 In `canvas_resize`, park the outgoing `wl_buffer`/`wl_shm_pool` in `canvas.old_buffer`/`old_pool` instead of destroying them immediately (munmap of old data stays immediate), and destroy them from `wl_buffer_release_handler`; verify by resizing the window repeatedly at fractional scale and confirming no artifacts or crashes (`WAYLAND_DEBUG=1` shows no destroy-before-release)
- [x] 3.1 In `canvas_resize`, park the outgoing `wl_buffer`/`wl_shm_pool` in `canvas.old_buffer`/`old_pool` instead of destroying them immediately (munmap of old data stays immediate), and destroy them from `wl_buffer_release_handler`; verify by resizing the window repeatedly at fractional scale and confirming no artifacts or crashes (`WAYLAND_DEBUG=1` shows no destroy-before-release)
## 4. Cleanup (behavior-neutral)
- [ ] 4.1 Remove `show_lines` from `grid_t` and its uses, keeping the `show_grid_lines` flag; verify with `ctest --test-dir build` and the grid-line toggle key
- [ ] 4.2 Free `grid`, `grid_back`, `grid_rendered` after the main loop exits; verify with valgrind (or a leak-checker run of a short session) reporting no leaks from those buffers
- [ ] 4.3 Remove the dead `ok ? 220 : 220` ternary and check `ftruncate`'s return value in `canvas_resize`; verify with `cmake --build ./build` producing no new warnings
- [x] 4.1 Remove `show_lines` from `grid_t` and its uses, keeping the `show_grid_lines` flag; verify with `ctest --test-dir build` and the grid-line toggle key
- [x] 4.2 Free `grid`, `grid_back`, `grid_rendered` after the main loop exits; verify with valgrind (or a leak-checker run of a short session) reporting no leaks from those buffers
- [x] 4.3 Remove the dead `ok ? 220 : 220` ternary and check `ftruncate`'s return value in `canvas_resize`; verify with `cmake --build ./build` producing no new warnings
+4
View File
@@ -28,6 +28,10 @@ The system SHALL report mouse positions and grid cells in logical coordinates re
- **WHEN** the user clicks on a scaled display
- **THEN** the selected grid cell matches the visual location of the cursor
#### Scenario: Pointer entry without motion
- **WHEN** the pointer enters the canvas surface at a position and no motion event follows
- **THEN** the reported cursor position matches the entry point in logical coordinates
### Requirement: Rendering uses physical resolution
The system SHALL render all content at physical resolution so that lines and cells stay sharp at the active scale.
+9 -1
View File
@@ -6,13 +6,21 @@ Allows users to configure keyboard shortcuts that map key combinations to applic
## Requirements
### Requirement: Default bindings are available without configuration
The system SHALL provide built-in default bindings for all documented actions, including digits 1-4 selecting the Glider, Blinker, Block, and Toad patterns, Space rotating, Escape cancelling, and Ctrl+Q quitting.
The system SHALL provide built-in default bindings for every supported action — digits 1-0 selecting the ten built-in patterns, Space rotating, Escape cancelling, Ctrl+Q quitting, P pausing, N stepping, +/- changing simulation speed, G toggling grid lines, and C clearing the grid.
#### Scenario: No config file present
- **WHEN** the application starts and `~/.config/wlgol/keys.conf` does not exist
- **THEN** the application uses the built-in default bindings
- **AND** creates the config file with the default contents
#### Scenario: Clear without configuration
- **WHEN** the default bindings are active and the user presses C
- **THEN** the clear action is triggered
#### Scenario: Toggle grid lines without configuration
- **WHEN** the default bindings are active and the user presses G
- **THEN** the toggle_grid action is triggered
### Requirement: Configuration file overrides defaults by merge
The system SHALL read `~/.config/wlgol/keys.conf` as TOML when it exists and merge its entries over the built-in defaults, so only specified keys change.
+4
View File
@@ -28,8 +28,12 @@ int main(void) {
config_t cfg = config_defaults();
xkb_keysym_t one = xkb_keysym_from_name("1", XKB_KEYSYM_CASE_INSENSITIVE);
xkb_keysym_t space = xkb_keysym_from_name("space", XKB_KEYSYM_CASE_INSENSITIVE);
xkb_keysym_t g = xkb_keysym_from_name("g", XKB_KEYSYM_CASE_INSENSITIVE);
xkb_keysym_t c = xkb_keysym_from_name("c", XKB_KEYSYM_CASE_INSENSITIVE);
assert(config_lookup(&cfg, 0, one) == ACTION_SELECT_GLIDER);
assert(config_lookup(&cfg, 0, space) == ACTION_ROTATE);
assert(config_lookup(&cfg, 0, g) == ACTION_TOGGLE_GRID);
assert(config_lookup(&cfg, 0, c) == ACTION_CLEAR);
const char *path = "/tmp/opencode/test_keys.conf";
FILE *fp = fopen(path, "w");
+11
View File
@@ -7,6 +7,8 @@ static void pointer_enter_handler(void *data, struct wl_pointer *pointer, uint32
// not implemented
printf("pointer_enter\n");
struct pointer_data *pointer_data = data;
pointer_data->x = wl_fixed_to_double(sx);
pointer_data->y = wl_fixed_to_double(sy);
wl_pointer_set_cursor(pointer, serial, pointer_data->cursor_surface,
pointer_data->cursor_image->hotspot_x, pointer_data->cursor_image->hotspot_y);
@@ -24,6 +26,9 @@ static void pointer_motion_handler(void *data, struct wl_pointer *pointer, uint3
struct pointer_data *pd = data;
pd->x = wl_fixed_to_double(sx);
pd->y = wl_fixed_to_double(sy);
if (pd->motion_handler) {
pd->motion_handler(pd->x, pd->y, pd->user_data);
}
}
static void pointer_button_handler(void *data, struct wl_pointer *pointer, uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
@@ -77,6 +82,12 @@ void pointer_set_button_handler(struct pointer_data *pointer_data, pointer_butto
pointer_data->user_data = user_data;
}
void pointer_set_motion_handler(struct pointer_data *pointer_data, pointer_motion_handler_t handler, void *user_data)
{
pointer_data->motion_handler = handler;
pointer_data->user_data = user_data;
}
void pointer_init_cursor(struct pointer_data *pointer_data, struct wl_compositor *compositor, struct wl_shm *shm)
{
pointer_data->x = 0;
+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);