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
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-11
@@ -0,0 +1,49 @@
# Design
## Context
Review findings span three modules: `tpointer.c` (enter handler ignores surface position), `wlgol.c` (ACTION_CLEAR no-op, buffer destroyed during resize while the compositor may hold it, minor cleanups) and `config.c` (default keymap lacks toggle_grid and clear). The renderer is now dirty-driven; a cleared grid propagates through the existing per-cell diff, so no special render path is needed for clear.
## Goals / Non-Goals
**Goals:**
- Preview appears at the true entry cell when the pointer enters the surface.
- Clear works with the default bindings; every supported action is reachable without a config file.
- No buffer destruction while the compositor may still be reading it.
**Non-Goals:**
- New input features (drag with tool, middle button), cursor theme changes.
- Double buffering or a general buffer-age pipeline.
## Decisions
### 1. Pointer enter sets `pd->x`/`pd->y` from `sx`/`sy`
Two lines: convert with `wl_fixed_to_double`. Alternative — query position elsewhere or force a synthetic motion: both more code for the same result. Wayland guarantees `enter` carries the entry position, so motion events are not needed to know where the pointer is.
### 2. Clear = `grid_clear()` + `screen_dirty = 1`
The on-demand renderer diffs against `grid_rendered`, so a wholesale clear repaints exactly the cells that were alive. No `full_redraw` needed. The incremental path is already exercised for large cell counts; nothing new to build.
### 3. Default keys: `"g" = toggle_grid`, `"c" = clear`
Both are free in the default keymap and mnemonic. Recorded as an assumption — if these keys are wanted for something else later, they are remappable like any other binding.
### 4. Resize defers buffer/pool destruction to the release event
`canvas_resize` stops destroying `canvas.buffer`/`canvas.pool` immediately; it parks them in `canvas.old_buffer`/`canvas.old_pool` and munmaps the old data right away (the compositor keeps its own mapping, so unmapping ours is safe). The existing `wl_buffer_release_handler` destroys parked buffer and pool when the compositor lets go. Rationale: destroying a `wl_buffer` still referenced by a pending frame is tolerated by compositors today but is protocol-untidy and can glitch; deferring costs one pointer pair. Alternative — double-buffering to avoid reuse conflicts entirely: out of scope per proposal.
### 5. Pointer position reaches the loop via a motion handler hook
Add `pointer_set_motion_handler()` mirroring the existing `pointer_set_button_handler()` pattern; `wlgol.c` registers a callback that stores logical `x`/`y` into module-local cursor variables. `loop_update()` stops reading `pointer_data.x` directly. Keeps input state owned by `tpointer.c`.
### 6. Cleanup items ride along
Remove `grid.show_lines` (single `show_grid_lines` flag remains), free the three grid buffers after the event loop exits, replace the dead `ok ? 220 : 220` ternary, and check `ftruncate`'s return. All behavior-neutral; tested by build + existing tests.
## Risks / Trade-offs
- [Deferring buffer destruction keeps an old SHM mapping alive one extra frame on rapid resizes] → Bounded: each release event drains the queue; worst case is one parked buffer per resize.
- [New default keys g/c could clash with a user's existing `keys.conf`] → No: user config merges over defaults, and existing files simply keep working; only fresh default files gain the keys.
## Migration Plan
Single commit; no data migration. Users with an existing `~/.config/wlgol/keys.conf` keep their bindings (merge semantics) and can gain g/c by deleting the file or adding the entries. Rollback is `git revert`.
## Open Questions
None.
@@ -0,0 +1,30 @@
# Proposal
## Why
A code review of the renderer/input path found two functional defects (pattern preview lands at a wrong cell when the pointer enters without motion; the clear action does nothing and toggle_grid is unreachable with default bindings) plus implementation-only cleanup that reduces duplicated state and protocol sloppiness in the canvas resize path.
## What Changes
- Set the cursor position from the pointer `enter` event (surface-local, logical), so the pattern preview renders at the true entry cell instead of the last stale position.
- Implement the `clear` action: set all cells dead and mark the screen dirty (the `cell-grid` spec already requires this; the code only printed a message).
- Add missing default key bindings: `"g" = toggle_grid`, `"c" = clear` (assumption: these keys; adjust in review).
- Destroy the SHM buffer on resize only after its release event, or otherwise make `canvas_resize` not destroy a buffer the compositor may still be reading.
- Cleanup, no behavior change: drop the redundant `grid.show_lines` field (keep the single `show_grid_lines` flag), pass pointer position to the main loop through the existing handler pattern instead of reading `pointer_data` directly, free the three grid buffers on exit, remove the dead preview color ternary, check `ftruncate`'s return value.
## Capabilities
### New Capabilities
### Modified Capabilities
- `keyboard-bindings`: the default-bindings requirement now explicitly covers every supported action, including clear and toggle_grid, with the new default keys.
- `display-scaling`: the input-coordinates requirement gains a scenario — the cursor position is correct at pointer entry, not only after motion events.
## Impact
- `wlgol.c`: ACTION_CLEAR handler, canvas_resize buffer lifecycle, exit cleanup, preview colors.
- `tpointer.c`: pointer enter handler sets `x`/`y` from `sx`/`sy`.
- `config.c`: two added entries in the default keymap file content.
- `include/grid.h` / `grid.c`: removal of `show_lines` field (call sites in `wlgol.c` updated).
- No new dependencies; no protocol changes.
@@ -0,0 +1,14 @@
# Spec Delta: fix-review-findings
## MODIFIED Requirements
### Requirement: Input coordinates remain logical
The system SHALL report mouse positions and grid cells in logical coordinates regardless of the active scale.
#### Scenario: Scaled click
- **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
@@ -0,0 +1,19 @@
# Spec Delta: fix-review-findings
## MODIFIED Requirements
### Requirement: Default bindings are available without configuration
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
@@ -0,0 +1,22 @@
# Tasks
## 1. Input fixes
- [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
- [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
- [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)
- [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