diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-14 13:56:48 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-14 13:56:48 +0300 |
| commit | 87279f6903343e621c0446b20d1e7d6a495f1125 (patch) | |
| tree | 999b5fb5fb76702efb02f856f082147304f0859b /examples | |
| parent | 1ab3e5a123109704789c2768e11a4f9af515821d (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/game.c | 44 | ||||
| -rw-r--r-- | examples/hello.c | 48 | ||||
| -rw-r--r-- | examples/notepad.c | 111 | ||||
| -rw-r--r-- | examples/simple_window.c | 52 | ||||
| -rw-r--r-- | examples/tablet.c | 37 |
5 files changed, 141 insertions, 151 deletions
diff --git a/examples/game.c b/examples/game.c index 850b39b..89b9f3a 100644 --- a/examples/game.c +++ b/examples/game.c @@ -1,8 +1,8 @@ /* GPU example: the application owns context binding, drawing and swap pacing. */ #include <yasl/yasl.h> -#include <yasl/gl.h> -#include <stdio.h> +#include <errno.h> #include <math.h> +#include <stdio.h> #include <string.h> typedef void (*clear_color_fn)(float, float, float, float); @@ -12,11 +12,10 @@ typedef void (*viewport_fn)(int, int, int, int); int main(int argc, char **argv) { bool smoke = argc == 2 && !strcmp(argv[1], "--smoke-test"); - yasl_surface *s = yasl_gl_surface_create(&(struct yasl_surface_options){.title="YASL OpenGL", .width=800, .height=600, .type=YASL_SURFACE_NATIVE, .flags=0}, NULL); - if (!s || (yasl_gl_make_current(s) < 0)) { - const char *error; - error = strerror(errno); - fprintf(stderr, "%s\n", error); + yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){ + .title = "YASL OpenGL", .width = 800, .height = 600, .type = YASL_SURFACE_GL}); + if (!s || yasl_gl_make_current(s) < 0) { + fprintf(stderr, "%s\n", strerror(errno)); yasl_shutdown(); return 1; } @@ -34,23 +33,34 @@ int main(int argc, char **argv) /* Drain events before each frame; while the display is gone, block until it returns. */ yasl_event e; int n = yasl_wait_event(&e, online ? 0 : -1); - if (n < 0) { result = 1; break; } + if (n < 0) { + result = 1; + break; + } if (e.type == YASL_EVENT_CLOSE || e.type == YASL_EVENT_QUIT || - (e.type == YASL_EVENT_KEY && e.key.key == YASL_KEY_ESCAPE)) running = false; - if (e.type == YASL_EVENT_DISPLAY_LOST) online = false; + (e.type == YASL_EVENT_KEY && e.key.key == YASL_KEY_ESCAPE)) + running = false; + if (e.type == YASL_EVENT_DISPLAY_LOST) { + yasl_gl_make_current(NULL); + online = false; + } if (e.type == YASL_EVENT_DISPLAY_RESTORED) { /* The context is new: bind it again and restore GL state here. */ - online = (yasl_gl_make_current(s) == 0) && (yasl_gl_set_swap_interval(s, 1) == 0); + online = yasl_gl_make_current(s) == 0 && yasl_gl_set_swap_interval(s, 1) == 0; } - if (n > 0 || !online || !running) continue; - int w, h; - yasl_surface_get_framebuffer_size(s, &w, &h); - viewport(0, 0, w, h); + if (n > 0 || !online || !running) + continue; + struct yasl_surface_info info = yasl_surface_get_info(s); + viewport(0, 0, info.pixel_width, info.pixel_height); double t = yasl_time_ns() / 1e9; clear_color((float)(0.3 + 0.2 * sin(t)), 0.2f, 0.4f, 1); clear(0x00004000u); - if ((yasl_gl_present(s) < 0)) { result = 1; break; } - if (smoke && ++frames == 3) break; + if (yasl_gl_present(s) < 0) { + result = 1; + break; + } + if (smoke && ++frames == 3) + break; } yasl_gl_make_current(NULL); yasl_shutdown(); diff --git a/examples/hello.c b/examples/hello.c index 054051d..d17dd39 100644 --- a/examples/hello.c +++ b/examples/hello.c @@ -1,50 +1,30 @@ /* A CPU-rendered window. */ #include <yasl/yasl.h> -#include <yasl/cpu.h> int main(void) { - struct yasl_surface_options options = { - .title = "Hello YASL", - .width = 640, - .height = 480, - .type = YASL_SURFACE_CPU, - }; - yasl_surface *surface = yasl_surface_create(&options); + yasl_surface *surface = yasl_surface_create( + &(struct yasl_surface_options){.title = "Hello YASL", .width = 640, .height = 480}); if (!surface) return 1; - - int result = 0; - for (;;) { - yasl_event event; - if (yasl_wait_event(&event, -1) < 0) { - result = 1; - break; - } + yasl_event event; + while (yasl_wait_event(&event, -1) >= 0) { if (event.type == YASL_EVENT_CLOSE || event.type == YASL_EVENT_QUIT) break; if (event.type != YASL_EVENT_REFRESH) continue; - - struct yasl_cpu_buffer buffer; - if (yasl_cpu_acquire(surface, &buffer) < 0) { - result = 1; + int w, h; + uint32_t *pixels = yasl_cpu_pixels(surface, &w, &h); + if (!pixels) break; - } - for (int y = 0; y < buffer.height; y++) { - uint32_t *row = (uint32_t *)((unsigned char *)buffer.pixels + y * buffer.stride_bytes); - for (int x = 0; x < buffer.width; x++) { + for (int y = 0; y < h; y++) + for (int x = 0; x < w; x++) { /* Opaque premultiplied ARGB: red grows rightward, green downward. */ - uint32_t r = (uint32_t)(x * 255 / (buffer.width > 1 ? buffer.width - 1 : 1)); - uint32_t g = (uint32_t)(y * 255 / (buffer.height > 1 ? buffer.height - 1 : 1)); - row[x] = 0xff000000 | r << 16 | g << 8 | (255 - r); + uint32_t r = (uint32_t)(x * 255 / (w > 1 ? w - 1 : 1)); + uint32_t g = (uint32_t)(y * 255 / (h > 1 ? h - 1 : 1)); + pixels[(size_t)y * w + x] = 0xff000000 | r << 16 | g << 8 | (255 - r); } - } - if (yasl_cpu_present(surface, &buffer) < 0) { - result = 1; - break; - } + yasl_cpu_present(surface); } - yasl_shutdown(); - return result; + return yasl_shutdown() < 0; } diff --git a/examples/notepad.c b/examples/notepad.c index ff12038..d0b38b9 100644 --- a/examples/notepad.c +++ b/examples/notepad.c @@ -19,7 +19,6 @@ #define _DEFAULT_SOURCE #include <yasl/yasl.h> -#include <yasl/cpu.h> #include <ctype.h> #include <math.h> @@ -988,10 +987,9 @@ static void prompt_execute(struct editor *ed) static bool editor_init(struct editor *ed) { - ed->surface = yasl_surface_create(&(struct yasl_surface_options){.title="Untitled - YASL Notepad", .width=960, .height=640, .type=YASL_SURFACE_CPU, .flags=0}); + ed->surface = yasl_surface_create(&(struct yasl_surface_options){.title="Untitled - YASL Notepad", .width=960, .height=640}); if (!ed->surface) return false; - ed->surface_scale = yasl_surface_get_scale(ed->surface); - yasl_surface_set_text_input(ed->surface, true, YASL_TEXT_NORMAL); + ed->surface_scale = yasl_surface_get_info(ed->surface).scale; ed->worker_running = true; if (pthread_mutex_init(&ed->worker_mutex, NULL)) return false; @@ -1034,19 +1032,21 @@ static void editor_update_ime(struct editor *ed) ed->ime_snapshot_ready = true; ed->text_revision++; } - if (snapshot_changed) - yasl_surface_set_surrounding_text(ed->surface, ed->ime_snapshot, strlen(ed->ime_snapshot), - ed->ime_begin, ed->ime_end, ed->text_revision); - double scale = yasl_surface_get_scale(ed->surface); + double scale = yasl_surface_get_info(ed->surface).scale; int zoom = (scale >= 2.0 ? 2 : 1) + ed->zoom_offset; if (zoom < 1) zoom = 1; double caret_x = (48.0 * zoom + 8.0 * zoom + ed->cursor_col * 8.0 * zoom) / scale; double caret_y = (24.0 * zoom + (ed->cursor_row - ed->scroll_y) * 16.0 * zoom) / scale; - if (!ed->ime_caret_ready || caret_x != ed->ime_caret_x || caret_y != ed->ime_caret_y) { + if (snapshot_changed || !ed->ime_caret_ready || caret_x != ed->ime_caret_x || + caret_y != ed->ime_caret_y) { ed->ime_caret_x = caret_x; ed->ime_caret_y = caret_y; ed->ime_caret_ready = true; - yasl_surface_set_ime_caret(ed->surface, caret_x, caret_y, 2.0, 16.0); + yasl_surface_set_text_input(ed->surface, &(struct yasl_text_input){ + .text = ed->ime_snapshot, .length = strlen(ed->ime_snapshot), + .selection_begin = ed->ime_begin, .selection_end = ed->ime_end, + .revision = ed->text_revision, .caret_x = caret_x, .caret_y = caret_y, + .caret_width = 2.0, .caret_height = 16.0}); } } @@ -1055,49 +1055,49 @@ static void on_event(struct editor *ed, const yasl_event *ev) switch (ev->type) { case YASL_EVENT_TEXT: if (ed->prompt != PROMPT_NONE) { - for (size_t i = 0; i < ev->text.length && ed->prompt_len < (int)sizeof(ed->prompt_buf) - 1; i++) { - if (ev->text.data[i] >= 32 && ev->text.data[i] <= 126) { - ed->prompt_buf[ed->prompt_len++] = ev->text.data[i]; + for (size_t i = 0; i < ev->length && ed->prompt_len < (int)sizeof(ed->prompt_buf) - 1; i++) { + if (ev->text[i] >= 32 && ev->text[i] <= 126) { + ed->prompt_buf[ed->prompt_len++] = ev->text[i]; ed->prompt_buf[ed->prompt_len] = '\0'; } } } else { - insert_text(ed, ev->text.data, ev->text.length); + insert_text(ed, ev->text, ev->length); } editor_request_render(ed); break; case YASL_EVENT_CLIPBOARD: - if (ev->clipboard.status != YASL_TRANSFER_OK) { - editor_set_status(ed, ev->clipboard.status == YASL_TRANSFER_UNAVAILABLE + if (ev->action != YASL_TRANSFER_OK) { + editor_set_status(ed, ev->action == YASL_TRANSFER_UNAVAILABLE ? "Clipboard has no text" : "Clipboard transfer failed"); editor_request_render(ed); break; } - if (ev->clipboard.data && ev->clipboard.length > 0) { - insert_text(ed, ev->clipboard.data, ev->clipboard.length); + if (ev->text && ev->length > 0) { + insert_text(ed, ev->text, ev->length); editor_request_render(ed); } break; case YASL_EVENT_TEXT_EDIT: - if (ed->prompt == PROMPT_NONE && ev->text_edit.data && - ev->text_edit.revision == ed->text_revision && - ev->text_edit.begin <= ev->text_edit.end && - ev->text_edit.end <= strlen(ed->ime_snapshot)) { - ed->cursor_col = (int)ev->text_edit.begin; + if (ed->prompt == PROMPT_NONE && ev->text && + ev->ime.revision == ed->text_revision && + ev->ime.begin <= ev->ime.end && + ev->ime.end <= strlen(ed->ime_snapshot)) { + ed->cursor_col = (int)ev->ime.begin; ed->sel_anchor_row = ed->cursor_row; - ed->sel_anchor_col = (int)ev->text_edit.begin; - ed->cursor_col = (int)ev->text_edit.end; - ed->has_selection = ev->text_edit.begin != ev->text_edit.end; - insert_text(ed, ev->text_edit.data, ev->text_edit.length); + ed->sel_anchor_col = (int)ev->ime.begin; + ed->cursor_col = (int)ev->ime.end; + ed->has_selection = ev->ime.begin != ev->ime.end; + insert_text(ed, ev->text, ev->length); editor_request_render(ed); } break; case YASL_EVENT_DROP: - if (ev->drop.action == YASL_DROP_DATA) { - const char *cursor = ev->drop.data; + if (ev->action == YASL_DROP_RELEASE) { + const char *cursor = ev->text; char path[512]; size_t length; while ((length = yasl_drop_next_path(&cursor, path, sizeof(path))) > 0) { @@ -1111,9 +1111,9 @@ static void on_event(struct editor *ed, const yasl_event *ev) break; case YASL_EVENT_KEY: - if (ev->key.action == YASL_KEY_PRESS || ev->key.action == YASL_KEY_REPEAT) { - bool ctrl = (ev->key.mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) != 0; - bool shift = (ev->key.mods & YASL_MOD_SHIFT) != 0; + if (ev->action == YASL_KEY_PRESS || ev->action == YASL_KEY_REPEAT) { + bool ctrl = (ev->mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) != 0; + bool shift = (ev->mods & YASL_MOD_SHIFT) != 0; /* Interactive Prompt Mode */ if (ed->prompt != PROMPT_NONE) { @@ -1219,7 +1219,7 @@ static void on_event(struct editor *ed, const yasl_event *ev) editor_request_render(ed); return; case YASL_KEY_V: - if (!yasl_clipboard_request(ed->surface, YASL_CLIPBOARD)) + if (yasl_clipboard_request(ed->surface, YASL_CLIPBOARD) < 0) editor_set_status(ed, "Clipboard paste failed"); return; case YASL_KEY_Z: @@ -1341,10 +1341,10 @@ static void on_event(struct editor *ed, const yasl_event *ev) break; case YASL_EVENT_POINTER: { - double device_scale = yasl_surface_get_scale(ed->surface); - int px = (int)lround(ev->pointer.x * device_scale); - int py = (int)lround(ev->pointer.y * device_scale); - if (ev->pointer.action == YASL_POINTER_DOWN && ev->pointer.button == YASL_BUTTON_PRIMARY) { + double device_scale = yasl_surface_get_info(ed->surface).scale; + int px = (int)lround(ev->x * device_scale); + int py = (int)lround(ev->y * device_scale); + if (ev->action == YASL_POINTER_DOWN && ev->pointer.button == YASL_BUTTON_PRIMARY) { int scale = (device_scale >= 2.0 ? 2 : 1) + ed->zoom_offset; if (scale < 1) scale = 1; int char_w = 8 * scale; @@ -1395,11 +1395,11 @@ static void on_event(struct editor *ed, const yasl_event *ev) } ed->caret_visible = true; editor_request_render(ed); - } else if (ev->pointer.action == YASL_POINTER_UP && ev->pointer.button == YASL_BUTTON_PRIMARY) { + } else if (ev->action == YASL_POINTER_UP && ev->pointer.button == YASL_BUTTON_PRIMARY) { ed->mouse_dragging = false; - } else if (ev->pointer.action == YASL_POINTER_LEAVE) { + } else if (ev->action == YASL_POINTER_LEAVE) { ed->mouse_dragging = false; - } else if (ev->pointer.action == YASL_POINTER_MOVE && ed->mouse_dragging) { + } else if (ev->action == YASL_POINTER_MOVE && ed->mouse_dragging) { int scale = (device_scale >= 2.0 ? 2 : 1) + ed->zoom_offset; if (scale < 1) scale = 1; int char_w = 8 * scale; @@ -1427,27 +1427,26 @@ static void on_event(struct editor *ed, const yasl_event *ev) yasl_event pointer = {0}; pointer.type = YASL_EVENT_POINTER; pointer.surface = ev->surface; - pointer.pointer.x = ev->pen.x; - pointer.pointer.y = ev->pen.y; + pointer.x = ev->x; + pointer.y = ev->y; pointer.pointer.button = YASL_BUTTON_PRIMARY; - pointer.pointer.mods = 0; - switch (ev->pen.action) { - case YASL_PEN_DOWN: pointer.pointer.action = YASL_POINTER_DOWN; break; - case YASL_PEN_UP: pointer.pointer.action = YASL_POINTER_UP; break; + switch (ev->action) { + case YASL_PEN_DOWN: pointer.action = YASL_POINTER_DOWN; break; + case YASL_PEN_UP: pointer.action = YASL_POINTER_UP; break; case YASL_PEN_LEAVE: - case YASL_PEN_CANCEL: pointer.pointer.action = YASL_POINTER_LEAVE; break; - default: pointer.pointer.action = YASL_POINTER_MOVE; break; + case YASL_PEN_CANCEL: pointer.action = YASL_POINTER_LEAVE; break; + default: pointer.action = YASL_POINTER_MOVE; break; } on_event(ed, &pointer); break; } case YASL_EVENT_SCROLL: - if (ev->scroll.mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) { + if (ev->mods & (YASL_MOD_CTRL | YASL_MOD_SUPER)) { /* Ctrl + Wheel = Zoom */ if (ev->scroll.lines_y > 0 && ed->zoom_offset < 2) ed->zoom_offset++; else if (ev->scroll.lines_y < 0 && ed->zoom_offset > -1) ed->zoom_offset--; - } else if (ev->scroll.mods & YASL_MOD_SHIFT) { + } else if (ev->mods & YASL_MOD_SHIFT) { /* Shift + Wheel = Horizontal scroll */ ed->scroll_x -= (int)(ev->scroll.lines_y * 3.0); if (ed->scroll_x < 0) ed->scroll_x = 0; @@ -1483,13 +1482,11 @@ static void on_event(struct editor *ed, const yasl_event *ev) static void editor_render(struct editor *ed) { - struct yasl_cpu_buffer buffer; - int acquired = yasl_cpu_acquire(ed->surface, &buffer); - int w = buffer.width, h = buffer.height; - uint32_t *pix = acquired == 0 ? buffer.pixels : NULL; + int w, h; + uint32_t *pix = yasl_cpu_pixels(ed->surface, &w, &h); if (!pix) return; - double display_scale = yasl_surface_get_scale(ed->surface); + double display_scale = yasl_surface_get_info(ed->surface).scale; int scale = (display_scale >= 2.0 ? 2 : 1) + ed->zoom_offset; if (scale < 1) scale = 1; int char_w = 8 * scale; @@ -1626,7 +1623,7 @@ static void editor_render(struct editor *ed) ed->is_modified ? 0xfffab387 : status_accent, scale); } - if ((yasl_cpu_present(ed->surface, &buffer) < 0)) { + if (yasl_cpu_present(ed->surface) < 0) { fprintf(stderr, "Could not present CPU image\n"); ed->failed = true; ed->running = false; @@ -1699,7 +1696,7 @@ int main(int argc, char **argv) } } - if ((yasl_init(&(struct yasl_options){.app_id="org.yasl.notepad"}) < 0) || !editor_init(&ed)) { + if (yasl_init(YASL_BACKEND_AUTO, "org.yasl.notepad") < 0 || !editor_init(&ed)) { fprintf(stderr, "Could not initialize YASL Notepad\n"); editor_shutdown(&ed); yasl_shutdown(); diff --git a/examples/simple_window.c b/examples/simple_window.c index 4b4b552..f85abd4 100644 --- a/examples/simple_window.c +++ b/examples/simple_window.c @@ -1,51 +1,57 @@ /* Mouse and pen input on a CPU surface. */ #include <yasl/yasl.h> -#include <yasl/cpu.h> #include <string.h> int main(int argc, char **argv) { bool smoke = argc == 2 && !strcmp(argv[1], "--smoke-test"); - yasl_surface *s = yasl_surface_create(&(struct yasl_surface_options){.title="YASL input", .width=640, .height=480, .type=YASL_SURFACE_CPU, .flags=0}); - if (!s) return 1; + yasl_surface *s = yasl_surface_create( + &(struct yasl_surface_options){.title = "YASL input", .width = 640, .height = 480}); + if (!s) + return 1; double px = -100, py = -100, pressure = 0; bool dirty = false; int frames = 0, result = 1; yasl_event e; for (;;) { int n = yasl_wait_event(&e, smoke ? 20 : -1); - if (n < 0) break; + if (n < 0) + break; if (e.type == YASL_EVENT_CLOSE || e.type == YASL_EVENT_QUIT || - (e.type == YASL_EVENT_KEY && e.key.key == YASL_KEY_ESCAPE)) { result = 0; break; } - if (e.type == YASL_EVENT_POINTER) { - px = e.pointer.x; py = e.pointer.y; - pressure = (e.pointer.buttons & YASL_BUTTON_BIT(YASL_BUTTON_PRIMARY)) ? 0.5 : 0; - dirty = true; + (e.type == YASL_EVENT_KEY && e.key.key == YASL_KEY_ESCAPE)) { + result = 0; + break; } - if (e.type == YASL_EVENT_PEN) { - px = e.pen.x; py = e.pen.y; - pressure = (e.pen.axes_valid & YASL_PEN_PRESSURE) ? e.pen.pressure : 0; + if (e.type == YASL_EVENT_POINTER || e.type == YASL_EVENT_PEN) { + px = e.x; + py = e.y; + pressure = e.type == YASL_EVENT_PEN ? ((e.pen.axes & YASL_PEN_PRESSURE) ? e.pen.pressure : 0) + : (e.pointer.buttons & YASL_BUTTON_BIT(YASL_BUTTON_PRIMARY)) ? 0.5 + : 0; dirty = true; } - if (e.type == YASL_EVENT_REFRESH) dirty = true; + if (e.type == YASL_EVENT_REFRESH) + dirty = true; if (dirty) { - struct yasl_cpu_buffer buffer; - int acquired = yasl_cpu_acquire(s, &buffer); - int w = buffer.width, h = buffer.height; - uint32_t *pixels = acquired == 0 ? buffer.pixels : NULL; - if (!pixels) break; - double scale = yasl_surface_get_scale(s), radius = (8 + pressure * 48) * scale; - for (int y = 0; y < h; y++) { + int w, h; + uint32_t *pixels = yasl_cpu_pixels(s, &w, &h); + if (!pixels) + break; + double scale = yasl_surface_get_info(s).scale, radius = (8 + pressure * 48) * scale; + for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) { double dx = x - px * scale, dy = y - py * scale; pixels[(size_t)y * w + x] = dx * dx + dy * dy < radius * radius ? 0xffffc060u : 0xff203040u; } - } - if ((yasl_cpu_present(s, &buffer) < 0)) break; + if (yasl_cpu_present(s) < 0) + break; dirty = false; frames++; } - if (smoke && frames && n == 0) { result = 0; break; } + if (smoke && frames && n == 0) { + result = 0; + break; + } } yasl_shutdown(); return result; 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 <yasl/yasl.h> -#include <yasl/cpu.h> #include <string.h> #include <math.h> @@ -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; |
