From 6eac0a5f2c552728efca420b1129fae7d4fea773 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Sun, 11 Oct 2026 23:40:57 +0300 Subject: feat: backend selection (Wayland, X11, Cocoa) --- src/wayland/surface.c | 93 ++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 45 deletions(-) (limited to 'src/wayland/surface.c') diff --git a/src/wayland/surface.c b/src/wayland/surface.c index da334e4..ced7d89 100644 --- a/src/wayland/surface.c +++ b/src/wayland/surface.c @@ -1,9 +1,9 @@ #include "internal.h" #include -static void display_free(struct yasl_display *c); +static void display_free(struct wayland_display *c); -static void display_cancel_read(struct yasl_display *c) +static void display_cancel_read(struct wayland_display *c) { if (!c->reading) return; @@ -11,7 +11,7 @@ static void display_cancel_read(struct yasl_display *c) c->reading = false; } -static void report_display_error(struct yasl_display *c) +static void report_display_error(struct wayland_display *c) { int err = wl_display_get_error(c->wl); if (!err) @@ -26,24 +26,24 @@ static void report_display_error(struct yasl_display *c) fprintf(stderr, "[yasl] display error %d\n", err); } -void display_enter(struct yasl_display *c) +void display_enter(struct wayland_display *c) { c->depth++; } -void display_leave(struct yasl_display *c) +void display_leave(struct wayland_display *c) { if (--c->depth > 0) return; keyboard_settle_focus(c); while (c->dead) { - struct yasl_surface *w = c->dead; + struct wayland_surface *w = c->dead; c->dead = w->dead_next; free(w); } } -static void display_lose(struct yasl_display *c) +static void display_lose(struct wayland_display *c) { if (c->lost) return; @@ -51,7 +51,7 @@ static void display_lose(struct yasl_display *c) report_display_error(c); display_cancel_read(c); display_enter(c); - for (struct yasl_surface *w = c->surfaces, *next; w; w = next) { + for (struct wayland_surface *w = c->surfaces, *next; w; w = next) { next = w->conn_next; surface_fail(w); } @@ -60,19 +60,19 @@ static void display_lose(struct yasl_display *c) c->cbs->lost(c->ud); } -void surface_fail(struct yasl_surface *w) +void surface_fail(struct wayland_surface *w) { if (w->closed) return; w->closed = true; if (w->cbs->close) - w->cbs->close(w, w->ud); + w->cbs->close(&w->base, w->ud); } static int display_ready(int fd, short revents, void *ud) { (void)fd; - struct yasl_display *c = ud; + struct wayland_display *c = ud; if (c->lost) return 0; display_enter(c); @@ -90,7 +90,7 @@ static int display_ready(int fd, short revents, void *ud) return 0; } -void yasl_display_dispatch(struct yasl_display *c) +void wayland_display_dispatch(struct wayland_display *c) { if (!c || c->lost || c->reading) return; @@ -100,7 +100,7 @@ void yasl_display_dispatch(struct yasl_display *c) display_leave(c); } -static void display_arm(struct yasl_display *c) +static void display_arm(struct wayland_display *c) { while (!c->reading && wl_display_prepare_read(c->wl) != 0) { if (wl_display_dispatch_pending(c->wl) < 0) { @@ -121,12 +121,12 @@ static void display_arm(struct yasl_display *c) yasl_loop_set_fd_events(c->loop, c->watch, events); } -void yasl_display_flush(struct yasl_display *c) +void wayland_display_flush(struct wayland_display *c) { if (!c || c->lost) return; display_enter(c); - for (struct yasl_surface *w = c->surfaces, *next; w; w = next) { + for (struct wayland_surface *w = c->surfaces, *next; w; w = next) { next = w->conn_next; surface_draw_frame(w); } @@ -135,7 +135,7 @@ void yasl_display_flush(struct yasl_display *c) display_leave(c); } -static void display_free(struct yasl_display *c) +static void display_free(struct wayland_display *c) { conn_drag_end(c); if (c->loop && c->watch > 0) yasl_loop_del_fd(c->loop, c->watch); @@ -167,10 +167,11 @@ static void display_free(struct yasl_display *c) free(c); } -struct yasl_display *yasl_display_open(struct yasl_loop *loop, const struct yasl_display_spec *spec) +struct wayland_display *wayland_display_open(struct yasl_loop *loop, const struct yasl_display_spec *spec) { - struct yasl_display *c = calloc(1, sizeof(*c)); + struct wayland_display *c = calloc(1, sizeof(*c)); if (!c) return NULL; + c->base.ops = &yasl_wayland_backend; c->loop = loop; if (spec) { c->cbs = spec->cbs; @@ -201,14 +202,14 @@ err: return NULL; } -void yasl_display_close(struct yasl_display *c) +void wayland_display_close(struct wayland_display *c) { if (!c) return; assert(!c->surfaces && "destroy every surface before its display"); display_free(c); } -uint32_t yasl_display_caps(const struct yasl_display *c) +uint32_t wayland_display_caps(const struct wayland_display *c) { if (!c) return 0; uint32_t caps = YASL_CAP_POPUPS | YASL_CAP_CSD; @@ -223,18 +224,19 @@ uint32_t yasl_display_caps(const struct yasl_display *c) return caps; } -static void conn_link(struct yasl_display *c, struct yasl_surface *w) +static void conn_link(struct wayland_display *c, struct wayland_surface *w) { + w->base.display = &c->base; w->conn = c; w->conn_next = c->surfaces; c->surfaces = w; } -static void conn_unlink(struct yasl_surface *w) +static void conn_unlink(struct wayland_surface *w) { - struct yasl_display *c = w->conn; + struct wayland_display *c = w->conn; if (!c) return; - for (struct yasl_surface **p = &c->surfaces; *p; p = &(*p)->conn_next) { + for (struct wayland_surface **p = &c->surfaces; *p; p = &(*p)->conn_next) { if (*p == w) { *p = w->conn_next; break; @@ -247,9 +249,9 @@ static void conn_unlink(struct yasl_surface *w) w->conn = NULL; } -struct yasl_surface *conn_surface_for(struct yasl_display *c, struct wl_surface *s) +struct wayland_surface *conn_surface_for(struct wayland_display *c, struct wl_surface *s) { - for (struct yasl_surface *w = c->surfaces; w && s; w = w->conn_next) + for (struct wayland_surface *w = c->surfaces; w && s; w = w->conn_next) if (w->surface == s) return w; return NULL; @@ -262,18 +264,18 @@ static bool spec_valid(const struct yasl_surface_spec *spec) buffer_size(spec->size.x, spec->size.y, &bytes); } -struct yasl_surface *yasl_surface_create_toplevel(struct yasl_display *c, +struct wayland_surface *wayland_surface_create_toplevel(struct wayland_display *c, const struct yasl_surface_spec *spec) { if (!c || c->lost || !spec_valid(spec)) return NULL; int width = spec->size.x, height = spec->size.y; display_cancel_read(c); - struct yasl_surface *w = calloc(1, sizeof(*w)); + struct wayland_surface *w = calloc(1, sizeof(*w)); if (!w) return NULL; display_enter(c); conn_link(c, w); - yasl_surface_set_callbacks(w, NULL, NULL); + wayland_surface_set_callbacks(w, NULL, NULL); w->req_width = width; w->req_height = height; w->logical_width = width; @@ -311,12 +313,12 @@ struct yasl_surface *yasl_surface_create_toplevel(struct yasl_display *c, return w; err: - yasl_surface_destroy(w); + wayland_surface_destroy(w); display_leave(c); return NULL; } -void yasl_surface_set_callbacks(struct yasl_surface *w, const struct yasl_surface_cbs *cbs, void *ud) +void wayland_surface_set_callbacks(struct wayland_surface *w, const struct yasl_surface_cbs *cbs, void *ud) { if (w && !w->closed) { static const struct yasl_surface_cbs no_callbacks = {0}; @@ -325,10 +327,10 @@ void yasl_surface_set_callbacks(struct yasl_surface *w, const struct yasl_surfac } } -void yasl_surface_destroy(struct yasl_surface *w) +void wayland_surface_destroy(struct wayland_surface *w) { if (!w || w->destroyed) return; - for (struct yasl_surface *o = w->conn ? w->conn->surfaces : NULL; o; o = o->conn_next) + for (struct wayland_surface *o = w->conn ? w->conn->surfaces : NULL; o; o = o->conn_next) assert(o->parent != w && "destroy a popup before its parent"); input_queue_clear(&w->input); free(w->ime_preedit); @@ -347,10 +349,11 @@ void yasl_surface_destroy(struct yasl_surface *w) if (w->popup) xdg_popup_destroy(w->popup); if (w->xdg_surface) xdg_surface_destroy(w->xdg_surface); if (w->surface) wl_surface_destroy(w->surface); - struct yasl_display *c = w->conn; + struct wayland_display *c = w->conn; conn_unlink(w); static const struct yasl_surface_cbs no_callbacks = {0}; - *w = (struct yasl_surface){ + *w = (struct wayland_surface){ + .base = w->base, .conn_next = w->conn_next, .cbs = &no_callbacks, .closed = true, @@ -364,32 +367,32 @@ void yasl_surface_destroy(struct yasl_surface *w) } } -struct yasl_display *yasl_surface_display(const struct yasl_surface *w) +struct wayland_display *wayland_surface_display(const struct wayland_surface *w) { return w ? w->conn : NULL; } -void yasl_surface_schedule_redraw(struct yasl_surface *w) +void wayland_surface_schedule_redraw(struct wayland_surface *w) { if (w) w->want_draw = true; } -struct yasl_vector2i yasl_surface_size(const struct yasl_surface *w) +struct yasl_vector2i wayland_surface_size(const struct wayland_surface *w) { return w ? (struct yasl_vector2i){ .x = w->width, .y = w->height } : (struct yasl_vector2i){0}; } -uint32_t yasl_surface_scale_120(const struct yasl_surface *w) +uint32_t wayland_surface_scale_120(const struct wayland_surface *w) { return w ? w->scale_120 : YASL_SCALE_UNIT; } -void yasl_surface_set_background_color(struct yasl_surface *w, uint32_t bg) +void wayland_surface_set_background_color(struct wayland_surface *w, uint32_t bg) { (void)w; (void)bg; } -bool yasl_surface_start_interactive_move(struct yasl_surface *s) +bool wayland_surface_start_interactive_move(struct wayland_surface *s) { if (!s || !s->toplevel || !s->conn) return false; struct seat *seat = conn_active_seat(s->conn); @@ -398,7 +401,7 @@ bool yasl_surface_start_interactive_move(struct yasl_surface *s) return true; } -bool yasl_surface_start_interactive_resize(struct yasl_surface *s, enum yasl_resize_edge edge) +bool wayland_surface_start_interactive_resize(struct wayland_surface *s, enum yasl_resize_edge edge) { if (!s || !s->toplevel || !s->conn || edge == YASL_EDGE_NONE) return false; struct seat *seat = conn_active_seat(s->conn); @@ -407,7 +410,7 @@ bool yasl_surface_start_interactive_resize(struct yasl_surface *s, enum yasl_res return true; } -bool yasl_surface_show_window_menu(struct yasl_surface *s, struct yasl_vector2i pos) +bool wayland_surface_show_window_menu(struct wayland_surface *s, struct yasl_vector2i pos) { if (!s || !s->toplevel || !s->conn) return false; struct seat *seat = conn_active_seat(s->conn); @@ -419,12 +422,12 @@ bool yasl_surface_show_window_menu(struct yasl_surface *s, struct yasl_vector2i return true; } -void *yasl_display_get_native_handle(struct yasl_display *d) +void *wayland_display_get_native_handle(struct wayland_display *d) { return d ? d->wl : NULL; } -void *yasl_surface_get_native_handle(struct yasl_surface *s) +void *wayland_surface_get_native_handle(struct wayland_surface *s) { return s ? s->surface : NULL; } -- cgit v1.2.3