diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-13 15:05:06 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-13 15:05:06 +0300 |
| commit | 98f0d51157fa2a2b8ccaae54ed4850a88d223d01 (patch) | |
| tree | 90b03d0930986620d95eb0f45c07b96e1331783c /examples/tablet.c | |
| parent | 339e1ad7adb8340732ee856d0f6f0d2c7f1455d1 (diff) | |
Simplify surface state, examples, and library installation
Diffstat (limited to 'examples/tablet.c')
| -rw-r--r-- | examples/tablet.c | 156 |
1 files changed, 77 insertions, 79 deletions
diff --git a/examples/tablet.c b/examples/tablet.c index 8a1af7e..f392cdc 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -1,5 +1,5 @@ /* Pressure-sensitive drawing using only YASL's portable API. */ -#include <yasl/yasl.h> +#include <yasl/app.h> #include <math.h> #include <stdio.h> @@ -14,10 +14,15 @@ #define HEADER 104 struct app { - bool running, smoke_test, failed, tablet_supported; + bool smoke_test, failed, tablet_supported; bool down, visible, previous; + struct yasl_surface *surface; enum yasl_backend backend; - struct yasl_mouse_event pointer; + struct { + struct yasl_vector2i pos; + enum yasl_pointer_device device; + struct yasl_pointer_pen pen; + } pointer; double last_x, last_y, last_radius; uint32_t *canvas; struct yasl_geometry geometry; @@ -135,31 +140,49 @@ static void stroke(struct app *app) app->previous = true; } -static void input(struct app *app, const struct yasl_input_event *event) +static int input(void *ud, const struct yasl_event *event) { - if (event->kind == YASL_INPUT_RESET || - (event->kind == YASL_INPUT_FOCUS && !event->focus.focused)) { + struct app *app = ud; + if (event->type == YASL_EVENT_WINDOW_CLOSE_REQUEST) + return YASL_APP_QUIT; + if (event->type == YASL_EVENT_WINDOW_RESIZE) { + if (!resize_canvas(app, (struct yasl_geometry){event->resize.size, event->resize.scale_120})) { + app->failed = true; + return YASL_APP_QUIT; + } + } else if (event->type == YASL_EVENT_INPUT_RESET || event->type == YASL_EVENT_MOUSE_LEAVE || + (event->type == YASL_EVENT_WINDOW_FOCUS && !event->focus.focused)) { cancel_stroke(app); - } else if (event->kind == YASL_INPUT_KEY && event->key.action == YASL_KEY_PRESS) { - if (event->key.key == YASL_KEY_ESCAPE) app->running = false; + } else if (event->type == YASL_EVENT_KEY && event->key.action == YASL_KEY_PRESS) { + if (event->key.key == YASL_KEY_ESCAPE) return YASL_APP_QUIT; if (event->key.key == YASL_KEY_C || event->key.key == YASL_KEY_SPACE) clear_canvas(app); - } else if (event->kind == YASL_INPUT_MOUSE) { - const struct yasl_mouse_event *pointer = &event->mouse; - if (pointer->kind == YASL_PTR_SCROLL) return; - if (app->pointer.device != pointer->device) cancel_stroke(app); - app->pointer = *pointer; - app->visible = pointer->kind != YASL_PTR_LEAVE; - if (pointer->kind == YASL_PTR_LEAVE) cancel_stroke(app); - else if (pointer->kind == YASL_PTR_PRESS && pointer->button == 0) { + } else if (event->type == YASL_EVENT_MOUSE_BUTTON) { + if (app->pointer.device != event->mouse_button.device) cancel_stroke(app); + app->pointer.pos = event->mouse_button.pos; + app->pointer.device = event->mouse_button.device; + app->pointer.pen = event->mouse_button.pen; + app->visible = true; + if (event->mouse_button.pressed && event->mouse_button.button == 0) { app->down = true; app->previous = false; stroke(app); - } else if (pointer->kind == YASL_PTR_RELEASE && pointer->button == 0) { + } else if (!event->mouse_button.pressed && event->mouse_button.button == 0) { app->down = app->previous = false; app->pointer.pen.pressure = 0; - } else if (pointer->kind == YASL_PTR_MOTION && app->down) stroke(app); + } + } else if (event->type == YASL_EVENT_MOUSE_MOTION) { + if (app->pointer.device != event->mouse_motion.device) cancel_stroke(app); + app->pointer.pos = event->mouse_motion.pos; + app->pointer.device = event->mouse_motion.device; + app->pointer.pen = event->mouse_motion.pen; + app->visible = true; + if (app->down) stroke(app); + } else { + return YASL_APP_CONTINUE; } + yasl_surface_request_frame(event->surface); + return YASL_APP_CONTINUE; } /* Tiny built-in 5x7 font keeps this example independent of a text library. */ @@ -214,18 +237,19 @@ static void text(struct yasl_framebuffer *fb, double dpi, int x, int y, } } -static void draw(struct yasl_surface *surface, struct yasl_frame *frame, void *ud) +static void draw(void *ud, struct yasl_surface *surface, struct yasl_frame_context *frame) { struct app *app = ud; - struct yasl_geometry geometry = yasl_surface_geometry(surface); - geometry.size = frame->target.size; + struct yasl_geometry geometry = {frame->size, frame->scale_120}; if (!resize_canvas(app, geometry)) { fprintf(stderr, "Cannot allocate drawing canvas\n"); - app->running = false; app->failed = true; + yasl_surface_destroy(surface); + app->surface = NULL; return; } - struct yasl_framebuffer *fb = &frame->target; + struct yasl_framebuffer target = {.size = frame->size, .pixels = frame->cpu.pixels}; + struct yasl_framebuffer *fb = ⌖ memcpy(fb->pixels, app->canvas, (size_t)geometry.size.x * geometry.size.y * sizeof(uint32_t)); double dpi = (double)app->geometry.scale_120 / YASL_SCALE_UNIT; rect(fb, 0, 0, fb->size.x, (int)lround(HEADER * dpi), PANEL); @@ -260,41 +284,42 @@ static void draw(struct yasl_surface *surface, struct yasl_frame *frame, void *u for (int i = 0; i <= 64; i++) rect(fb, (int)lround(cx + tx * i / 64), (int)lround(cy + ty * i / 64), 2, 2, ACCENT); } - struct yasl_damage_list damage = {.n = -1}; - if (!yasl_surface_present(surface, fb, &damage)) { + if (!yasl_frame_present_cpu(frame, NULL, 0)) { app->failed = true; - app->running = false; } - if (app->smoke_test) app->running = false; + if (app->failed || app->smoke_test) { + yasl_surface_destroy(surface); + app->surface = NULL; + } } -static void configure(struct yasl_surface *surface, struct yasl_geometry geometry, void *ud) +static int init(void *ud, struct yasl_display *display) { struct app *app = ud; - if (!resize_canvas(app, geometry)) { + app->backend = yasl_display_backend(display); + app->tablet_supported = (yasl_display_caps(display) & YASL_CAP_TABLET) != 0; + struct yasl_window_spec spec = { + .title = "YASL Tablet - pressure and tilt", .size = {900, 640}, + }; + app->surface = yasl_surface_create_toplevel(display, &spec); + if (!app->surface || !resize_canvas(app, yasl_surface_geometry(app->surface))) { + fprintf(stderr, "Cannot create drawing surface\n"); app->failed = true; - app->running = false; + return YASL_APP_QUIT; } - yasl_surface_schedule_redraw(surface); -} - -static void close_window(struct yasl_surface *surface, void *ud) -{ - (void)surface; - ((struct app *)ud)->running = false; -} - -static void quit(void *ud) { ((struct app *)ud)->running = false; } -static void lost(void *ud) -{ - struct app *app = ud; - app->failed = true; - app->running = false; + yasl_surface_set_mouse_mode(app->surface, YASL_MOUSE_HIDDEN); + yasl_surface_request_frame(app->surface); + printf("YASL tablet on %s: pressure changes brush width; eraser removes ink.\n" + "Mouse: fixed-width drawing. C/Space: clear. Escape: quit.\n", + backend_name(app->backend)); + if (!app->tablet_supported) + printf("This display server does not expose tablet input.\n"); + return YASL_APP_CONTINUE; } int main(int argc, char **argv) { - struct app app = {.running = true}; + struct app app = {0}; enum yasl_backend backend = YASL_BACKEND_AUTO; for (int i = 1; i < argc; i++) { if (!strcmp(argv[i], "--smoke-test")) app.smoke_test = true; @@ -312,39 +337,12 @@ int main(int argc, char **argv) return help ? 0 : 1; } } - struct yasl_display_cbs display_cbs = {.quit = quit, .lost = lost}; - struct yasl_display_spec display_spec = { - .app_id = "org.yasl.tablet", .backend = backend, .cbs = &display_cbs, .ud = &app, + struct yasl_app_desc desc = { + .app_id = "org.yasl.tablet", .backend = backend, .userdata = &app, + .init = init, .event = input, .frame = draw, }; - struct yasl_display *display = yasl_display_open(NULL, &display_spec); - if (!display) { perror("Cannot open display"); return 1; } - app.backend = yasl_display_backend(display); - app.tablet_supported = (yasl_display_caps(display) & YASL_CAP_TABLET) != 0; - struct yasl_window_cbs callbacks = {.draw = draw, .configure = configure, .close = close_window}; - struct yasl_window_spec spec = { - .title = "YASL Tablet - pressure and tilt", .size = {900, 640}, - .cbs = &callbacks, .ud = &app, - }; - struct yasl_surface *surface = yasl_surface_create_toplevel(display, &spec); - if (!surface) { perror("Cannot create surface"); yasl_display_close(display); return 1; } - yasl_surface_set_cursor(surface, YASL_CURSOR_HIDDEN); - yasl_surface_schedule_redraw(surface); - printf("YASL tablet on %s: pressure changes brush width; eraser removes ink.\n" - "Mouse: fixed-width drawing. C/Space: clear. Escape: quit.\n", - backend_name(app.backend)); - if (!app.tablet_supported) - printf("This display server does not expose tablet input.\n"); - while (app.running) { - if (yasl_display_dispatch(display, -1) < 0) { app.failed = true; break; } - struct yasl_input_event event; - while (yasl_surface_input_poll(surface, &event)) { - input(&app, &event); - yasl_surface_schedule_redraw(surface); - } - yasl_display_flush(display); - } + int result = yasl_run(&desc); + if (result) perror("yasl_run"); free(app.canvas); - yasl_surface_destroy(surface); - yasl_display_close(display); - return app.failed ? 1 : 0; + return result || app.failed ? 1 : 0; } |
