From 87279f6903343e621c0446b20d1e7d6a495f1125 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Wed, 14 Oct 2026 13:56:48 +0300 Subject: Simplify the public API into one dense header yasl.h now holds everything except Vulkan: 45 functions instead of 62, and three installed headers instead of ten. - One constructor: yasl_surface_create takes CPU, GL or NATIVE, with zero-initialized GL options meaning 3.2 core, RGBA8, D24S8. - Events carry action, mods, x, y and text at the top level; pen positions are now logical like pointer positions. - yasl_cpu_pixels/yasl_cpu_present draw into one persistent canvas, cutting three full-frame copies per frame. - yasl_surface_get_info, yasl_surface_request, YASL_CURSOR_LOCKED, yasl_surface_set_text_input, yasl_surface_begin_move_resize and yasl_surface_get_native replace groups of narrower calls. - Interactive calls drop the decorative trigger event argument. - yasl_wait_event returns 0 only on timeout or yasl_wake, the refresh callback runs only in native modal loops, focus loss sends INPUT_RESET, and a lost event no longer fails every later wait. - Unsupported operations and bad arguments report ENOTSUP and EINVAL instead of EIO; the keycodes header no longer needs C23. - The shared library gets a versioned SONAME and version macros. Co-Authored-By: Claude Opus 5.5 --- examples/tablet.c | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'examples/tablet.c') diff --git a/examples/tablet.c b/examples/tablet.c index ce796a4..8c62fc0 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -1,6 +1,5 @@ /* Pen canvas: pressure, eraser and coherent strokes drawn straight into the CPU surface. */ #include -#include #include #include @@ -24,11 +23,10 @@ static void stamp(uint32_t *pixels, int w, int h, double x, double y, double rad } /* Paints paper over the area a resize added, or everything when clear is set. */ -static uint32_t *canvas(yasl_surface *s, int *w, int *h, int *painted_w, int *painted_h, bool clear, struct yasl_cpu_buffer *buffer) +static uint32_t *canvas(yasl_surface *s, int *w, int *h, int *painted_w, int *painted_h, bool clear) { - if (yasl_cpu_acquire(s, buffer) < 0) return NULL; - *w = buffer->width; *h = buffer->height; - uint32_t *pixels = buffer->pixels; + uint32_t *pixels = yasl_cpu_pixels(s, w, h); + if (!pixels) return NULL; if (clear) *painted_w = *painted_h = 0; for (int y = 0; y < *h; y++) for (int x = y < *painted_h ? *painted_w : 0; x < *w; x++) pixels[(size_t)y * *w + x] = PAPER; @@ -44,11 +42,11 @@ int main(int argc, char **argv) if (!strcmp(argv[i], "--smoke-test")) smoke = true; else if (!strcmp(argv[i], "--backend") && i + 1 < argc) { const char *b = argv[++i]; - if (!(yasl_init(&(struct yasl_options){.backend=!strcmp(b, "wayland") ? YASL_BACKEND_WAYLAND : - !strcmp(b, "x11") ? YASL_BACKEND_X11 : YASL_BACKEND_COCOA}) == 0)) return 1; + if (yasl_init(!strcmp(b, "wayland") ? YASL_BACKEND_WAYLAND : + !strcmp(b, "x11") ? YASL_BACKEND_X11 : YASL_BACKEND_COCOA, NULL) < 0) return 1; } else return 1; } - yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){.title="YASL pen canvas — C clears, Escape exits", .width=800, .height=600, .type=YASL_SURFACE_CPU, .flags=0}); + yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){.title="YASL pen canvas — C clears, Escape exits", .width=800, .height=600}); if (!s) return 1; int painted_w = 0, painted_h = 0, frames = 0, result = 1; bool contact = false; @@ -58,26 +56,26 @@ int main(int argc, char **argv) int n = yasl_wait_event(&e, smoke ? 20 : -1); if (n < 0) break; if (smoke && frames && n == 0) { result = 0; break; } - bool escape = e.type == YASL_EVENT_KEY && e.key.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_ESCAPE; + bool escape = e.type == YASL_EVENT_KEY && e.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_ESCAPE; if (e.type == YASL_EVENT_CLOSE || e.type == YASL_EVENT_QUIT || escape) { result = 0; break; } bool clear = false, draw = false, erase = false, down = false, present = e.type == YASL_EVENT_REFRESH; double x = 0, y = 0, p = 0.5; switch (e.type) { case YASL_EVENT_KEY: - if (e.key.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_C) clear = present = true; + if (e.action == YASL_KEY_PRESS && e.key.key == YASL_KEY_C) clear = present = true; break; case YASL_EVENT_PEN: - x = e.pen.x; y = e.pen.y; - p = (e.pen.axes_valid & YASL_PEN_PRESSURE) ? e.pen.pressure : 0.5; + x = e.x; y = e.y; + p = (e.pen.axes & YASL_PEN_PRESSURE) ? e.pen.pressure : 0.5; erase = e.pen.tool == YASL_PEN_ERASER; down = e.pen.in_contact; - draw = down && (e.pen.action == YASL_PEN_DOWN || e.pen.action == YASL_PEN_MOVE); - if (e.pen.action == YASL_PEN_LEAVE || e.pen.action == YASL_PEN_CANCEL) contact = false; + draw = down && (e.action == YASL_PEN_DOWN || e.action == YASL_PEN_MOVE); + if (e.action == YASL_PEN_LEAVE || e.action == YASL_PEN_CANCEL) contact = false; break; case YASL_EVENT_POINTER: - x = e.pointer.x; y = e.pointer.y; + x = e.x; y = e.y; down = (e.pointer.buttons & YASL_BUTTON_BIT(YASL_BUTTON_PRIMARY)) != 0; - draw = down && (e.pointer.action == YASL_POINTER_DOWN || e.pointer.action == YASL_POINTER_MOVE); + draw = down && (e.action == YASL_POINTER_DOWN || e.action == YASL_POINTER_MOVE); break; case YASL_EVENT_INPUT_RESET: contact = false; @@ -88,12 +86,11 @@ int main(int argc, char **argv) if (draw || present) { int w, h; int old_w = painted_w, old_h = painted_h; - struct yasl_cpu_buffer buffer; - uint32_t *pixels = canvas(s, &w, &h, &painted_w, &painted_h, clear, &buffer); + uint32_t *pixels = canvas(s, &w, &h, &painted_w, &painted_h, clear); if (!pixels) break; if (w != old_w || h != old_h) contact = false; if (draw) { - double scale = yasl_surface_get_scale(s); + double scale = yasl_surface_get_info(s).scale; x *= scale; y *= scale; double radius = (1.5 + 12 * p) * scale, dx = contact ? x - last_x : 0, dy = contact ? y - last_y : 0; int steps = (int)ceil(hypot(dx, dy) / (radius * 0.4)); @@ -101,7 +98,7 @@ int main(int argc, char **argv) for (int i = 1; i <= steps; i++) stamp(pixels, w, h, x - dx + dx * i / steps, y - dy + dy * i / steps, radius, erase); last_x = x; last_y = y; } - if ((yasl_cpu_present(s, &buffer) < 0)) break; + if (yasl_cpu_present(s) < 0) break; frames++; } if (e.type == YASL_EVENT_PEN || e.type == YASL_EVENT_POINTER) contact = down; -- cgit v1.2.3