feat: implement keyboard and mouse input, pattern placement, and HiDPI scaling

Add configurable key bindings (TOML), modifier-aware dispatch, mouse-driven
pattern placement with preview and rotation, cell grid storage/rendering,
and fractional/integer HiDPI scaling. Archive the input-and-placement change
and sync its specs to main specs.

Tests: Hashtable, Config, Grid, Pattern (4/4 pass).
This commit is contained in:
2026-11-08 14:46:04 +01:00
parent 5e7abfb56b
commit 7874a5f7c6
31 changed files with 4081 additions and 96 deletions
+298 -72
View File
@@ -1,46 +1,109 @@
#include <bits/time.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <sys/mman.h>
#include <syscall.h>
#include <unistd.h>
#include <linux/input-event-codes.h>
#include <xdg-shell.h>
#include <viewporter.h>
#include <fractional-scale-v1.h>
#include <wayland-client.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include <tkeyboard.h>
#include <tpointer.h>
#include <config.h>
#include <grid.h>
#include <pattern.h>
struct wl_compositor *compositor;
struct wl_shm *shm;
struct xdg_wm_base *xdg_shell;
struct wl_seat *wl_seat;
struct wl_output *wl_output = NULL;
int output_scale_factor = 1;
struct keyboard_data keyboard_data;
struct pointer_data pointer_data;
int running = 1;
int released = 0;
config_t g_config;
int active_tool = -1; /* -1 none, 0..3 patterns */
int active_rotation = 0;
int logical_width = 400;
int logical_height = 400;
double current_scale = 1.0;
int need_resize = 1;
grid_t grid;
double cursor_x = 0, cursor_y = 0;
int click_pending = 0;
double click_x = 0, click_y = 0;
int show_grid_lines = 1;
int sim_paused = 1;
struct timespec mytime;
struct canvas
{
int width;
int height;
int width; /* physical */
int height; /* physical */
int stride;
int size;
struct wl_buffer *buffer;
unsigned char *data;
struct wl_shm_pool *pool;
int fd;
struct wl_surface *surface;
struct wl_callback *frame;
} canvas;
struct wp_viewporter *viewporter = NULL;
struct wp_viewport *canvas_viewport = NULL;
struct wp_fractional_scale_manager_v1 *fractional_manager = NULL;
struct wp_fractional_scale_v1 *fractional_scale = NULL;
// output listener
static void output_geometry_handler(void *data, struct wl_output *output, int32_t x, int32_t y, int32_t phys_w, int32_t phys_h, int32_t subpixel, const char *make, const char *model, int32_t transform) { (void)data;(void)output;(void)x;(void)y;(void)phys_w;(void)phys_h;(void)subpixel;(void)make;(void)model;(void)transform; }
static void output_mode_handler(void *data, struct wl_output *output, uint32_t flags, int32_t width, int32_t height, int32_t refresh) { (void)data;(void)output;(void)flags;(void)width;(void)height;(void)refresh; }
static void output_done_handler(void *data, struct wl_output *output) { (void)data;(void)output; }
static void output_scale_handler(void *data, struct wl_output *output, int32_t factor) {
(void)data;(void)output;
output_scale_factor = factor;
if (!fractional_manager) {
if (factor > 0) current_scale = factor;
need_resize = 1;
}
}
const struct wl_output_listener output_listener = {
.geometry = output_geometry_handler,
.mode = output_mode_handler,
.done = output_done_handler,
.scale = output_scale_handler,
};
// fractional scale listener
static void fractional_preferred_scale_handler(void *data, struct wp_fractional_scale_v1 *scale_obj, uint32_t scale) {
(void)data;(void)scale_obj;
double s = (double)scale / 120.0;
if (s > 0) current_scale = s;
need_resize = 1;
}
const struct wp_fractional_scale_v1_listener fractional_scale_listener = {
.preferred_scale = fractional_preferred_scale_handler,
};
// registry
void registry_global_handler(
void *data, struct wl_registry *registry,
@@ -68,6 +131,25 @@ void registry_global_handler(
wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 5);
printf("Got wl_seat (%d)\n", version);
}
else if (strcmp(interface, wl_output_interface.name) == 0)
{
if (!wl_output)
{
wl_output = wl_registry_bind(registry, name, &wl_output_interface, 2);
wl_output_add_listener(wl_output, &output_listener, NULL);
printf("Got wl_output (%d)\n", version);
}
}
else if (strcmp(interface, wp_viewporter_interface.name) == 0)
{
viewporter = wl_registry_bind(registry, name, &wp_viewporter_interface, 1);
printf("Got wp_viewporter (%d)\n", version);
}
else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0)
{
fractional_manager = wl_registry_bind(registry, name, &wp_fractional_scale_manager_v1_interface, 1);
printf("Got fractional_scale_manager (%d)\n", version);
}
}
void registry_global_remove_handler(
@@ -89,6 +171,9 @@ void xdg_toplevel_configure_handler(
struct wl_array *states)
{
printf("xdg_toplevel_configure: %dx%d\n", width, height);
if (width > 0) logical_width = width;
if (height > 0) logical_height = height;
need_resize = 1;
}
void xdg_toplevel_close_handler(void *data, struct xdg_toplevel *xdg_toplevel)
@@ -144,18 +229,68 @@ const struct wl_buffer_listener wl_buffer_listener = {
void redraw(); // forward declaration
static void handle_key(uint32_t key, uint32_t state, xkb_keysym_t sym, void *user_data)
static void handle_key(uint32_t key, uint32_t state, xkb_keysym_t sym, uint32_t mods, void *user_data)
{
(void)key;
(void)user_data;
if (state == WL_KEYBOARD_KEY_STATE_PRESSED && sym == XKB_KEY_Escape)
{
printf("Escape pressed, exiting.\n");
if (state != WL_KEYBOARD_KEY_STATE_PRESSED) return;
action_t a = config_lookup(&g_config, mods, sym);
printf("action=%d\n", a);
switch (a) {
case ACTION_QUIT:
running = 0;
break;
case ACTION_CANCEL:
active_tool = -1;
printf("tool cancelled\n");
break;
case ACTION_ROTATE:
if (active_tool >= 0) {
active_rotation = (active_rotation + 1) % 4;
printf("rotation=%d\n", active_rotation);
}
break;
case ACTION_SELECT_GLIDER:
active_tool = 0; active_rotation = 0; printf("selected glider\n"); break;
case ACTION_SELECT_BLINKER:
active_tool = 1; active_rotation = 0; printf("selected blinker\n"); break;
case ACTION_SELECT_BLOCK:
active_tool = 2; active_rotation = 0; printf("selected block\n"); break;
case ACTION_SELECT_TOAD:
active_tool = 3; active_rotation = 0; printf("selected toad\n"); break;
case ACTION_CLEAR:
printf("clear requested\n");
break;
case ACTION_PAUSE:
sim_paused = !sim_paused;
break;
case ACTION_STEP:
printf("step requested\n");
break;
case ACTION_SPEED_UP:
case ACTION_SPEED_DOWN:
case ACTION_TOGGLE_GRID:
show_grid_lines = !show_grid_lines;
break;
default:
break;
}
}
static void handle_button(uint32_t button, uint32_t state, double x, double y, void *user_data)
{
(void)user_data;
if (state != WL_POINTER_BUTTON_STATE_PRESSED) return;
if (button != BTN_LEFT) return;
click_x = x;
click_y = y;
click_pending = 1;
printf("click at %.1f %.1f\n", x, y);
}
void wl_callack_done(void *data, struct wl_callback *callback, uint32_t time)
{
if (canvas.frame)
@@ -173,6 +308,44 @@ void wl_callack_done(void *data, struct wl_callback *callback, uint32_t time)
const struct wl_callback_listener wl_surface_frame_listener = {
.done = wl_callack_done};
static void canvas_resize(int log_w, int log_h, double scale) {
if (log_w < 1) log_w = 1;
if (log_h < 1) log_h = 1;
if (scale <= 0) scale = 1.0;
int phys_w = (int)ceil((double)log_w * scale);
int phys_h = (int)ceil((double)log_h * 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.data && canvas.size > 0) { munmap(canvas.data, canvas.size); canvas.data = NULL; }
if (canvas.fd >= 0) { close(canvas.fd); canvas.fd = -1; }
canvas.width = phys_w;
canvas.height = phys_h;
canvas.stride = phys_w * 4;
canvas.size = canvas.stride * phys_h;
canvas.fd = syscall(SYS_memfd_create, "buffer", 0);
ftruncate(canvas.fd, canvas.size);
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);
canvas.fd = -1;
canvas.buffer = wl_shm_pool_create_buffer(canvas.pool, 0, phys_w, phys_h, canvas.stride, WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener(canvas.buffer, &wl_buffer_listener, NULL);
if (canvas_viewport) {
wp_viewport_set_destination(canvas_viewport, log_w, log_h);
}
grid_resize(&grid, log_w / 8, log_h / 8);
need_resize = 0;
printf("canvas resized logical=%dx%d phys=%dx%d scale=%g grid=%dx%d\n", log_w, log_h, phys_w, phys_h, scale, grid.cols, grid.rows);
}
int main(void)
{
clock_gettime(CLOCK_MONOTONIC, &mytime);
@@ -188,6 +361,14 @@ int main(void)
canvas.surface = wl_compositor_create_surface(compositor);
if (viewporter) {
canvas_viewport = wp_viewporter_get_viewport(viewporter, canvas.surface);
}
if (fractional_manager) {
fractional_scale = wp_fractional_scale_manager_v1_get_fractional_scale(fractional_manager, canvas.surface);
wp_fractional_scale_v1_add_listener(fractional_scale, &fractional_scale_listener, NULL);
}
struct xdg_surface *xdg_surface = xdg_wm_base_get_xdg_surface(xdg_shell, canvas.surface);
struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
@@ -205,35 +386,29 @@ int main(void)
wl_keyboard_add_listener(keyboard_data.keyboard, &keyboard_listener, &keyboard_data);
pointer_init_cursor(&pointer_data, compositor, shm);
pointer_set_button_handler(&pointer_data, handle_button, NULL);
pointer_data.pointer = wl_seat_get_pointer(wl_seat);
wl_pointer_add_listener(pointer_data.pointer, &pointer_listener, &pointer_data);
canvas.width = 400;
canvas.height = 400;
canvas.stride = canvas.width * 4;
canvas.size = canvas.stride * canvas.height; // bytes
canvas.fd = -1;
grid_init(&grid, logical_width / 8, logical_height / 8);
grid.show_lines = 1;
// open an anonymous file and write some zero bytes to it
int fd = syscall(SYS_memfd_create, "buffer", 0);
ftruncate(fd, canvas.size);
// map it to the memory
canvas.data =
mmap(NULL, canvas.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
// turn it into a shared memory pool
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, canvas.size);
close(fd); // mapped, so can be closed
// allocate the buffer in that pool
canvas.buffer = wl_shm_pool_create_buffer(
pool, 0, canvas.width, canvas.height, canvas.stride, WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener(canvas.buffer, &wl_buffer_listener, NULL);
// wait for the surface to be configured
wl_display_roundtrip(display);
/* initial scale may arrive via fractional/output after roundtrip */
if (fractional_manager && !fractional_scale) {
/* already created above */
}
canvas_resize(logical_width, logical_height, current_scale);
const char *home = getenv("HOME");
char config_path[1024];
if (home) snprintf(config_path, sizeof(config_path), "%s/.config/wlgol/keys.conf", home);
else snprintf(config_path, sizeof(config_path), ".config/wlgol/keys.conf");
g_config = config_load(config_path);
redraw();
while (wl_display_dispatch(display) != -1 && running)
@@ -244,7 +419,8 @@ int main(void)
xdg_toplevel_destroy(xdg_toplevel);
xdg_surface_destroy(xdg_surface);
wl_surface_destroy(canvas.surface);
wl_shm_pool_destroy(pool);
if (canvas.pool) wl_shm_pool_destroy(canvas.pool);
if (canvas.buffer) wl_buffer_destroy(canvas.buffer);
wl_keyboard_destroy(keyboard_data.keyboard);
if (keyboard_data.xkb_state != NULL)
{
@@ -266,59 +442,109 @@ unsigned char mycolour = 0;
char direction = 1;
int framecounter = 0;
void redraw()
{
clock_t start = clock();
mycolour += direction;
switch (mycolour)
{
case 0: // lower limit, start to increase
direction = 1;
break;
case 255: // upper limit, start to decrease
direction = -1;
break;
}
// printf("dred=%d\n", dred);
// draw into buffer
for (int x = 0; x < canvas.width; x++)
{
for (int y = 0; y < canvas.height; y++)
{
struct pixel
{
// little-endian ARGB
static void fill_rect(int x0, int y0, int w, int h, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
if (w <= 0 || h <= 0) return;
for (int y = y0; y < y0 + h && y < canvas.height; y++) {
if (y < 0) continue;
for (int x = x0; x < x0 + w && x < canvas.width; x++) {
if (x < 0) continue;
struct pixel {
unsigned char blue;
unsigned char green;
unsigned char red;
unsigned char alpha;
} *px = (struct pixel *)(canvas.data + y * canvas.stride + x * 4);
px->red = r;
px->green = g;
px->blue = b;
px->alpha = a;
}
}
}
// draw a stripes pattern
if ((x + y) % 30 < 10)
{
// transparent
px->alpha = 0;
void redraw()
{
if (need_resize) {
canvas_resize(logical_width, logical_height, current_scale);
}
clock_t start = clock();
/* update cursor from pointer */
cursor_x = pointer_data.x;
cursor_y = pointer_data.y;
/* handle pending click placement */
if (click_pending) {
click_pending = 0;
if (active_tool >= 0) {
int cx = (int)(click_x / 8.0);
int cy = (int)(click_y / 8.0);
int dx[16], dy[16];
int n = pattern_rotated_offsets(active_tool, active_rotation, dx, dy);
int ok = 1;
for (int i = 0; i < n; i++) {
int c = cx + dx[i];
int r = cy + dy[i];
if (c < 0 || r < 0 || c >= grid.cols || r >= grid.rows) { ok = 0; break; }
}
else if ((x + y) % 30 < 20)
{
// yellow
px->alpha = 255;
px->red = 255;
px->green = 255;
px->blue = 0;
}
else
{
// semitransparent red
px->alpha = 128;
px->red = mycolour;
px->green = 0;
px->blue = (255 - mycolour);
if (ok) {
for (int i = 0; i < n; i++) {
grid_set(&grid, cx + dx[i], cy + dy[i], 1);
}
}
}
}
int cell_px = (grid.cols > 0) ? (canvas.width / grid.cols) : 8;
if (cell_px < 1) cell_px = 1;
/* clear background */
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++) {
int x = c * cell_px;
fill_rect(x, 0, 1, canvas.height, 40, 40, 48, 255);
}
for (int r = 0; r <= grid.rows; r++) {
int y = r * cell_px;
fill_rect(0, y, canvas.width, 1, 40, 40, 48, 255);
}
}
/* alive cells */
for (int r = 0; r < grid.rows; r++) {
for (int c = 0; c < grid.cols; c++) {
if (grid_get(&grid, c, r)) {
fill_rect(c * cell_px + 1, r * cell_px + 1, cell_px - 2, cell_px - 2, 80, 200, 120, 255);
}
}
}
/* preview */
if (active_tool >= 0) {
int cx = (int)(cursor_x / 8.0);
int cy = (int)(cursor_y / 8.0);
int dx[16], dy[16];
int n = pattern_rotated_offsets(active_tool, active_rotation, dx, dy);
int ok = 1;
for (int i = 0; i < n; i++) {
int c = cx + dx[i];
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 pg = ok ? 200 : 80;
unsigned char pb = ok ? 80 : 80;
for (int i = 0; i < n; i++) {
int c = cx + dx[i];
int r = cy + dy[i];
fill_rect(c * cell_px + 2, r * cell_px + 2, cell_px - 4, cell_px - 4, pr, pg, pb, 160);
}
}
// callback
canvas.frame = wl_surface_frame(canvas.surface);
wl_callback_add_listener(canvas.frame, &wl_surface_frame_listener, NULL);