#include "yasl/yasl.h" #include #include #include static bool running = true; static int mouse_x = -1; static int mouse_y = -1; static bool mouse_down = false; static int color_theme = 0; static void on_draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud) { (void)ud; int w = frame->target.size.x; int h = frame->target.size.y; uint32_t *pixels = frame->target.pixels; if (!pixels || w <= 0 || h <= 0) return; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { uint8_t r, g, b; uint8_t u = (uint8_t)((x * 255) / (w > 0 ? w : 1)); uint8_t v = (uint8_t)((y * 255) / (h > 0 ? h : 1)); switch (color_theme % 3) { case 0: r = u; g = v; b = 160; break; case 1: r = 40; g = u; b = v; break; default: r = v; g = 60; b = u; break; } /* Draw interactive pointer indicator */ if (mouse_x >= 0 && mouse_y >= 0) { int dx = x - mouse_x; int dy = y - mouse_y; int dist_sq = dx * dx + dy * dy; int radius = mouse_down ? 36 : 20; if (dist_sq < radius * radius) { r = (uint8_t)(255 - r / 2); g = (uint8_t)(255 - g / 2); b = (uint8_t)(255 - b / 2); } } pixels[y * w + x] = (255u << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; } } struct yasl_damage_list dmg = { .n = -1 }; yasl_surface_present(s, &frame->target, &dmg); } static void on_configure(struct yasl_surface *s, struct yasl_geometry geo, void *ud) { (void)ud; printf("[YASL] Resized to %dx%d (scale: %.2fx)\n", geo.size.x, geo.size.y, (double)geo.scale_120 / YASL_SCALE_UNIT); yasl_surface_schedule_redraw(s); } static void on_close(struct yasl_surface *s, void *ud) { (void)s; (void)ud; printf("[YASL] Window close requested\n"); running = false; } static void on_quit(void *ud) { (void)ud; running = false; } int main(void) { /* YASL manages its event loop internally when NULL is passed */ struct yasl_display_cbs disp_cbs = { .quit = on_quit }; struct yasl_display_spec disp_spec = { .app_id = "org.yasl.example", .cbs = &disp_cbs, }; struct yasl_display *d = yasl_display_open(NULL, &disp_spec); if (!d) { fprintf(stderr, "Failed to open display\n"); return 1; } struct yasl_surface_spec surf_spec = { .title = "YASL Interactive Window", .size = { .x = 640, .y = 480 }, }; struct yasl_surface *s = yasl_surface_create_toplevel(d, &surf_spec); if (!s) { fprintf(stderr, "Failed to create surface\n"); yasl_display_close(d); return 1; } struct yasl_surface_cbs surf_cbs = { .draw = on_draw, .configure = on_configure, .close = on_close, }; yasl_surface_set_callbacks(s, &surf_cbs, NULL); yasl_surface_schedule_redraw(s); printf("=== YASL Demo Running ===\n"); printf(" - Move or Click mouse inside window to interact\n"); printf(" - Press Space to cycle background themes\n"); printf(" - Press Escape or close window to exit\n\n"); while (running) { yasl_display_dispatch(d); struct yasl_input_event ev; while (yasl_surface_input_poll(s, &ev)) { if (ev.kind == YASL_INPUT_KEY) { if (ev.key.action == YASL_KEY_PRESS) { if (ev.key.key == 0xff1b) { /* Escape */ running = false; } else if (ev.key.key == ' ' || ev.key.key == 0xff0d) { /* Space or Return */ color_theme++; yasl_surface_schedule_redraw(s); } } } else if (ev.kind == YASL_INPUT_MOUSE) { if (ev.mouse.kind == YASL_PTR_MOTION) { mouse_x = ev.mouse.pos.x; mouse_y = ev.mouse.pos.y; yasl_surface_schedule_redraw(s); } else if (ev.mouse.kind == YASL_PTR_PRESS) { mouse_down = true; mouse_x = ev.mouse.pos.x; mouse_y = ev.mouse.pos.y; yasl_surface_schedule_redraw(s); } else if (ev.mouse.kind == YASL_PTR_RELEASE) { mouse_down = false; yasl_surface_schedule_redraw(s); } else if (ev.mouse.kind == YASL_PTR_LEAVE) { mouse_x = -1; mouse_y = -1; mouse_down = false; yasl_surface_schedule_redraw(s); } } } yasl_display_flush(d); } yasl_surface_destroy(s); yasl_display_close(d); return 0; }