summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-10-14 13:13:52 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-10-14 13:13:52 +0300
commit1ab3e5a123109704789c2768e11a4f9af515821d (patch)
tree31ee03e58093a58100c6eb58681d5d29648a5ddd /src
parentd86fa6215d40caa7852dbbd6a8cf4aa8e89dd879 (diff)
Redefine public and rendering APIs with explicit contracts
Diffstat (limited to 'src')
-rw-r--r--src/common/backend.h5
-rw-r--r--src/common/desktop.c106
-rw-r--r--src/common/native.c119
-rw-r--r--src/common/public_internal.h3
-rw-r--r--src/common/surface.c1429
-rw-r--r--src/common/vulkan.c223
-rw-r--r--src/platform/wayland/backend.c5
7 files changed, 1397 insertions, 493 deletions
diff --git a/src/common/backend.h b/src/common/backend.h
index 6eefbb3..246a9bc 100644
--- a/src/common/backend.h
+++ b/src/common/backend.h
@@ -25,9 +25,14 @@ struct yasl_private_display {
};
struct yasl_surface {
+ struct yasl_vulkan_binding *vulkan;
unsigned creation_flags;
enum yasl_private_presenter presenter;
struct yasl_surface *parent;
+ uint32_t *cpu_pixels;
+ int cpu_width, cpu_height;
+ uint64_t cpu_token;
+ bool cpu_acquired;
uint32_t *committed_pixels;
int committed_width, committed_height;
/* Window values last reported through public WINDOW events. */
diff --git a/src/common/desktop.c b/src/common/desktop.c
index 5045604..a9f72e3 100644
--- a/src/common/desktop.c
+++ b/src/common/desktop.c
@@ -11,17 +11,20 @@
static bool uri_valid(const char *uri)
{
unsigned char c = (unsigned char)uri[0];
- if ((unsigned)((c | 32) - 'a') >= 26) return false;
+ if ((unsigned)((c | 32) - 'a') >= 26)
+ return false;
const char *p = uri + 1;
for (; *p != ':'; p++) {
c = (unsigned char)*p;
bool alpha = (unsigned)((c | 32) - 'a') < 26;
bool digit = (unsigned)(c - '0') < 10;
- if (!alpha && !digit && c != '+' && c != '-' && c != '.') return false;
+ if (!alpha && !digit && c != '+' && c != '-' && c != '.')
+ return false;
}
for (; *p; p++) {
c = (unsigned char)*p;
- if (c < 0x20 || c == 0x7f) return false;
+ if (c < 0x20 || c == 0x7f)
+ return false;
}
return true;
}
@@ -38,13 +41,16 @@ static size_t copy_out(char *out, size_t cap, const char *s, size_t len)
bool yasl_private_display_open_uri(struct yasl_private_display *d, const char *uri)
{
- if (!d || !uri || !uri_valid(uri)) return false;
+ if (!d || !uri || !uri_valid(uri))
+ return false;
return desktop_open_uri(uri);
}
-bool yasl_private_display_notify(struct yasl_private_display *d, const char *title, const char *body)
+bool yasl_private_display_notify(struct yasl_private_display *d, const char *title,
+ const char *body)
{
- if (!d || !title) return false;
+ if (!d || !title)
+ return false;
return desktop_notify(d->app_id, title, body ? body : "");
}
@@ -55,27 +61,31 @@ uint32_t yasl_private_display_appearance(struct yasl_private_display *d)
bool yasl_private_surface_bell(struct yasl_surface *s)
{
- if (!s || !s->display->ops->surface_bell) return false;
+ if (!s || !s->display->ops->surface_bell)
+ return false;
return s->display->ops->surface_bell(s);
}
size_t yasl_private_executable_path(char *out, size_t cap)
{
- if (!out && cap) return 0;
+ if (!out && cap)
+ return 0;
char buf[DESKTOP_STR_MAX];
return copy_out(out, cap, buf, desktop_executable_path(buf, sizeof(buf)));
}
size_t yasl_private_resource_dir(char *out, size_t cap)
{
- if (!out && cap) return 0;
+ if (!out && cap)
+ return 0;
char buf[DESKTOP_STR_MAX];
return copy_out(out, cap, buf, desktop_resource_dir(buf, sizeof(buf)));
}
size_t yasl_private_default_locale(char *out, size_t cap)
{
- if (!out && cap) return 0;
+ if (!out && cap)
+ return 0;
char buf[DESKTOP_STR_MAX];
return copy_out(out, cap, buf, desktop_default_locale(buf, sizeof(buf)));
}
@@ -83,13 +93,69 @@ size_t yasl_private_default_locale(char *out, size_t cap)
#include "public_internal.h"
#include "yasl/desktop.h"
#include <errno.h>
-bool yasl_open_uri(const char *uri)
-{ if(!uri||!uri_valid(uri))return yasl_public_fail(EINVAL);errno=0;return desktop_open_uri(uri)||yasl_public_fail(errno); }
-bool yasl_notify(const char *title,const char *body)
-{ if(!yasl_public_owner())return false;errno=0;return yasl_private_display_notify(yasl_public_display(),title,body)||yasl_public_fail(errno); }
-unsigned yasl_appearance(void) { return yasl_private_display_appearance(yasl_public_display()); }
-bool yasl_surface_bell(yasl_surface *s)
-{ if(!yasl_public_surface(s,false))return false;errno=0;return yasl_private_surface_bell(s)||yasl_public_fail(errno); }
-size_t yasl_executable_path(char *out,size_t cap) { return yasl_private_executable_path(out,cap); }
-size_t yasl_resource_dir(char *out,size_t cap) { return yasl_private_resource_dir(out,cap); }
-size_t yasl_default_locale(char *out,size_t cap) { return yasl_private_default_locale(out,cap); }
+int yasl_open_uri(const char *uri)
+{
+ if (!uri || !uri_valid(uri))
+ return yasl_public_error(EINVAL);
+ errno = 0;
+ return yasl_public_status(desktop_open_uri(uri) || yasl_public_fail(errno));
+}
+int yasl_notify(const char *title, const char *body)
+{
+ if (!yasl_public_owner())
+ return -errno;
+ if (!title)
+ return yasl_public_error(EINVAL);
+ errno = 0;
+ return yasl_public_status(yasl_private_display_notify(yasl_public_display(), title, body) ||
+ yasl_public_fail(errno));
+}
+unsigned yasl_get_appearance(void)
+{
+ return yasl_public_owner() ? yasl_private_display_appearance(yasl_public_display()) : 0;
+}
+int yasl_surface_bell(yasl_surface *s)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if (!s->display->ops->surface_bell)
+ return yasl_public_error(ENOTSUP);
+ errno = 0;
+ return yasl_public_status(yasl_private_surface_bell(s) || yasl_public_fail(errno));
+}
+size_t yasl_get_executable_path(char *out, size_t cap)
+{
+ if (!out && cap) {
+ yasl_public_fail(EINVAL);
+ return 0;
+ }
+ errno = 0;
+ size_t n = yasl_private_executable_path(out, cap);
+ if (!n)
+ yasl_public_fail(errno);
+ return n;
+}
+size_t yasl_get_resource_dir(char *out, size_t cap)
+{
+ if (!out && cap) {
+ yasl_public_fail(EINVAL);
+ return 0;
+ }
+ errno = 0;
+ size_t n = yasl_private_resource_dir(out, cap);
+ if (!n)
+ yasl_public_fail(errno);
+ return n;
+}
+size_t yasl_get_default_locale(char *out, size_t cap)
+{
+ if (!out && cap) {
+ yasl_public_fail(EINVAL);
+ return 0;
+ }
+ errno = 0;
+ size_t n = yasl_private_default_locale(out, cap);
+ if (!n)
+ yasl_public_fail(errno);
+ return n;
+}
diff --git a/src/common/native.c b/src/common/native.c
index 88604c9..1b32094 100644
--- a/src/common/native.c
+++ b/src/common/native.c
@@ -4,22 +4,28 @@
void *yasl_private_display_get_native_handle(struct yasl_private_display *d)
{
- if (!d) return NULL;
- if (d->native_handle) return d->native_handle;
- return d->ops && d->ops->display_get_native_handle ?
- d->ops->display_get_native_handle(d) : NULL;
+ if (!d)
+ return NULL;
+ if (d->native_handle)
+ return d->native_handle;
+ return d->ops && d->ops->display_get_native_handle ? d->ops->display_get_native_handle(d)
+ : NULL;
}
void *yasl_private_surface_get_native_handle(struct yasl_surface *s)
{
- if (!s) return NULL;
- if (s->native_handle) return s->native_handle;
- return s->display && s->display->ops && s->display->ops->surface_get_native_handle ?
- s->display->ops->surface_get_native_handle(s) : NULL;
+ if (!s)
+ return NULL;
+ if (s->native_handle)
+ return s->native_handle;
+ return s->display && s->display->ops && s->display->ops->surface_get_native_handle
+ ? s->display->ops->surface_get_native_handle(s)
+ : NULL;
}
struct yasl_surface *yasl_private_surface_create_native(struct yasl_private_display *d,
- const struct yasl_private_window_spec *spec, void *graphics_device)
+ const struct yasl_private_window_spec *spec,
+ void *graphics_device)
{
if (!graphics_device) {
errno = EINVAL;
@@ -29,10 +35,10 @@ struct yasl_surface *yasl_private_surface_create_native(struct yasl_private_disp
}
bool yasl_private_surface_get_native_frame(struct yasl_surface *s,
- const struct yasl_private_framebuffer *target, struct yasl_private_native_frame *out)
+ const struct yasl_private_framebuffer *target,
+ struct yasl_private_native_frame *out)
{
- if (!s || !target || !out || !target->generation ||
- target->generation != s->frame_generation ||
+ if (!s || !target || !out || !target->generation || target->generation != s->frame_generation ||
!s->display->ops->surface_get_native_frame)
return false;
return s->display->ops->surface_get_native_frame(s, target, out);
@@ -40,36 +46,83 @@ bool yasl_private_surface_get_native_frame(struct yasl_surface *s,
#include "public_internal.h"
#include "yasl/metal.h"
+#if YASL_HAS_WAYLAND || YASL_HAS_X11 || YASL_HAS_COCOA
+static int native_validate(const yasl_surface *s, enum yasl_private_backend backend)
+{
+ if (!yasl_public_surface((yasl_surface *)s, false))
+ return -errno;
+ if (s->display->ops->type != backend)
+ return yasl_public_error(ENOTSUP);
+ if (s->display->offline)
+ return yasl_public_error(ENOTCONN);
+ return 0;
+}
+#endif
#if YASL_HAS_WAYLAND
#include "yasl/native/wayland.h"
-struct wl_display *yasl_wayland_display(const yasl_surface *s)
-{ return s && s->display->ops->type==YASL_PRIVATE_BACKEND_WAYLAND ? yasl_private_display_get_native_handle(s->display):NULL; }
-struct wl_surface *yasl_wayland_surface(const yasl_surface *s)
-{ return s && s->display->ops->type==YASL_PRIVATE_BACKEND_WAYLAND ? yasl_private_surface_get_native_handle((yasl_surface*)s):NULL; }
+int yasl_wayland_get_native(const yasl_surface *s, struct yasl_wayland_handles *out)
+{
+ if (!out)
+ return yasl_public_error(EINVAL);
+ *out = (struct yasl_wayland_handles){0};
+ int error = native_validate(s, YASL_PRIVATE_BACKEND_WAYLAND);
+ if (error)
+ return error;
+ out->display = yasl_private_display_get_native_handle(s->display);
+ out->surface = yasl_private_surface_get_native_handle((yasl_surface *)s);
+ return 0;
+}
#endif
#if YASL_HAS_X11
#include "yasl/native/x11.h"
-Display *yasl_x11_display(const yasl_surface *s)
-{ return s && s->display->ops->type==YASL_PRIVATE_BACKEND_X11 ? yasl_private_display_get_native_handle(s->display):NULL; }
-uintptr_t yasl_x11_window(const yasl_surface *s)
-{ return s && s->display->ops->type==YASL_PRIVATE_BACKEND_X11 ? (uintptr_t)yasl_private_surface_get_native_handle((yasl_surface*)s):0; }
+int yasl_x11_get_native(const yasl_surface *s, struct yasl_x11_handles *out)
+{
+ if (!out)
+ return yasl_public_error(EINVAL);
+ *out = (struct yasl_x11_handles){0};
+ int error = native_validate(s, YASL_PRIVATE_BACKEND_X11);
+ if (error)
+ return error;
+ out->display = yasl_private_display_get_native_handle(s->display);
+ out->window = (uintptr_t)yasl_private_surface_get_native_handle((yasl_surface *)s);
+ return 0;
+}
#endif
#if YASL_HAS_COCOA
#include "yasl/native/cocoa.h"
#include "common/private_native_cocoa.h"
-NSApplication *yasl_cocoa_application(void)
-{ struct yasl_private_display *d=yasl_public_display();return d ? yasl_private_cocoa_display_get_application(d):NULL; }
-NSWindow *yasl_cocoa_window(const yasl_surface *s)
-{ return yasl_private_cocoa_surface_get_window((yasl_surface*)s); }
-NSView *yasl_cocoa_view(const yasl_surface *s)
-{ return yasl_private_cocoa_surface_get_view((yasl_surface*)s); }
+int yasl_cocoa_get_native(const yasl_surface *s, struct yasl_cocoa_handles *out)
+{
+ if (!out)
+ return yasl_public_error(EINVAL);
+ *out = (struct yasl_cocoa_handles){0};
+ int error = native_validate(s, YASL_PRIVATE_BACKEND_COCOA);
+ if (error)
+ return error;
+ out->application = yasl_private_cocoa_display_get_application(s->display);
+ out->window = yasl_private_cocoa_surface_get_window((yasl_surface *)s);
+ out->view = yasl_private_cocoa_surface_get_view((yasl_surface *)s);
+ return 0;
+}
#endif
-CAMetalLayer *yasl_metal_layer(yasl_surface *s)
+int yasl_metal_get_layer(yasl_surface *s, CAMetalLayer **out)
{
- if(!yasl_public_surface(s,false))return NULL;
- if(s->presenter!=YASL_PRIVATE_PRESENTER_NONE && s->presenter!=YASL_PRIVATE_PRESENTER_METAL) {yasl_public_fail(EBUSY);return NULL;}
- if(!s->display->ops->surface_get_metal_layer){yasl_public_fail(ENOTSUP);return NULL;}
- CAMetalLayer *layer=s->display->ops->surface_get_metal_layer(s);
- if(!layer){yasl_public_fail(ENOTSUP);return NULL;}
- s->presenter=YASL_PRIVATE_PRESENTER_METAL;return layer;
+ if (!out)
+ return yasl_public_error(EINVAL);
+ *out = NULL;
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if (s->display->offline)
+ return yasl_public_error(ENOTCONN);
+ if (s->vulkan || (s->presenter != YASL_PRIVATE_PRESENTER_NONE &&
+ s->presenter != YASL_PRIVATE_PRESENTER_METAL))
+ return yasl_public_error(EBUSY);
+ if (!s->display->ops->surface_get_metal_layer)
+ return yasl_public_error(ENOTSUP);
+ CAMetalLayer *layer = s->display->ops->surface_get_metal_layer(s);
+ if (!layer)
+ return yasl_public_error(ENOTSUP);
+ s->presenter = YASL_PRIVATE_PRESENTER_METAL;
+ *out = layer;
+ return 0;
}
diff --git a/src/common/public_internal.h b/src/common/public_internal.h
index 210b222..95ecb6d 100644
--- a/src/common/public_internal.h
+++ b/src/common/public_internal.h
@@ -6,7 +6,8 @@ struct yasl_private_display *yasl_public_display(void);
bool yasl_public_owner(void);
bool yasl_public_surface(yasl_surface *, bool mutation);
bool yasl_public_fail(int error);
-void yasl_public_set_error(enum yasl_error, int native_error);
+int yasl_public_status(bool success);
+int yasl_public_error(int error);
void yasl_public_register_surface(yasl_surface *, unsigned flags, yasl_surface *parent);
void yasl_private_surface_notify_refresh(struct yasl_surface *);
#endif
diff --git a/src/common/surface.c b/src/common/surface.c
index b47fea7..de9e7b3 100644
--- a/src/common/surface.c
+++ b/src/common/surface.c
@@ -11,514 +11,1155 @@
#include <string.h>
static struct yasl_private_display *display;
-static enum yasl_backend requested_backend;
-static char *application_id;
static pthread_t owner;
static pthread_mutex_t lifecycle_mutex = PTHREAD_MUTEX_INITIALIZER;
static bool pumping;
static unsigned refresh_depth;
static char *delivered_text; /* Strings of the last returned event; freed by the next poll/wait. */
-static _Thread_local enum yasl_error last_error;
-static _Thread_local int native_error;
-
-void yasl_public_set_error(enum yasl_error error, int native)
-{ last_error=error; native_error=native; }
bool yasl_public_fail(int error)
{
- if (!error) error=EIO;
- enum yasl_error code=YASL_ERROR_IO;
- if (error==EINVAL || error==EOVERFLOW || error==EILSEQ) code=YASL_ERROR_INVALID;
- else if (error==ENOTSUP || error==ENOSYS) code=YASL_ERROR_UNSUPPORTED;
- else if (error==ENOMEM) code=YASL_ERROR_NO_MEMORY;
- else if (error==EBUSY) code=YASL_ERROR_BUSY;
- else if (error==EPERM) code=YASL_ERROR_WRONG_THREAD;
- else if (error==ENOTCONN) code=YASL_ERROR_DISCONNECTED;
- yasl_public_set_error(code,error); errno=error; return false;
-}
-enum yasl_error yasl_get_error(const char **description)
-{
- static const char *const messages[]={"No error","Invalid argument","Unsupported operation",
- "Out of memory","Library not initialized","Operation is busy","Wrong thread",
- "Input context expired","Native operation failed","Display disconnected"};
- if(description) *description=messages[last_error];
- return last_error;
-}
-int yasl_get_native_error(void) { return native_error; }
-struct yasl_private_display *yasl_public_display(void) { return display; }
+ errno = error > 0 ? error : EIO;
+ return false;
+}
+int yasl_public_error(int error)
+{
+ yasl_public_fail(error);
+ return -errno;
+}
+int yasl_public_status(bool success)
+{
+ return success ? 0 : yasl_public_error(errno);
+}
+struct yasl_private_display *yasl_public_display(void)
+{
+ return display;
+}
bool yasl_public_owner(void)
{
- if(!display) { yasl_public_set_error(YASL_ERROR_NOT_INITIALIZED,0); return false; }
- if(!pthread_equal(owner,pthread_self())) return yasl_public_fail(EPERM);
+ if (!display)
+ return yasl_public_fail(ENODEV);
+ if (!pthread_equal(owner, pthread_self()))
+ return yasl_public_fail(EPERM);
return true;
}
-bool yasl_public_surface(yasl_surface *s,bool mutation)
+bool yasl_public_surface(yasl_surface *s, bool mutation)
{
- if(!yasl_public_owner()) return false;
- if(!s || s->display!=display) return yasl_public_fail(EINVAL);
- if(mutation && refresh_depth) return yasl_public_fail(EBUSY);
+ if (!yasl_public_owner())
+ return false;
+ if (!s || s->display != display)
+ return yasl_public_fail(EINVAL);
+ if (mutation && refresh_depth)
+ return yasl_public_fail(EBUSY);
return true;
}
-static bool initialize(void)
+int yasl_init(const struct yasl_options *options)
{
- if(display) return yasl_public_owner();
- struct yasl_private_display_spec spec={.app_id=application_id,.backend=(enum yasl_private_backend)requested_backend};
- struct yasl_private_display *d=yasl_private_display_open(NULL,&spec);
- if(!d) return yasl_public_fail(errno);
+ if (display) {
+ if (!yasl_public_owner())
+ return -errno;
+ return yasl_public_error(EALREADY);
+ }
+ struct yasl_options defaults = {0};
+ if (!options)
+ options = &defaults;
+ if ((unsigned)options->backend > YASL_BACKEND_COCOA)
+ return yasl_public_error(EINVAL);
+ struct yasl_private_display_spec spec = {
+ .app_id = options->app_id,
+ .backend = (enum yasl_private_backend)options->backend,
+ };
+ errno = 0;
+ struct yasl_private_display *d = yasl_private_display_open(NULL, &spec);
+ if (!d)
+ return yasl_public_error(errno);
pthread_mutex_lock(&lifecycle_mutex);
- owner=pthread_self(); display=d;
+ owner = pthread_self();
+ display = d;
pthread_mutex_unlock(&lifecycle_mutex);
- return true;
-}
-bool yasl_set_app_id(const char *id)
-{
- if(display) return yasl_public_fail(EBUSY);
- char *copy=id ? strdup(id) : NULL;
- if(id&&!copy) return yasl_public_fail(ENOMEM);
- free(application_id); application_id=copy; return true;
+ return 0;
}
-bool yasl_set_backend(enum yasl_backend backend)
+static bool initialize(void)
{
- if(display) return yasl_public_fail(EBUSY);
- if(backend<YASL_BACKEND_AUTO || backend>YASL_BACKEND_COCOA) return yasl_public_fail(EINVAL);
- requested_backend=backend; return true;
+ return display ? yasl_public_owner() : yasl_init(NULL) == 0;
}
-static bool creation_valid(int width,int height,unsigned flags)
+static bool creation_valid(int width, int height, unsigned flags)
{
- if(width<=0 || height<=0 || width>32767 || height>32767 ||
- (unsigned long long)width*(unsigned)height>INT32_MAX/4 || (flags & ~15u))
+ if (width <= 0 || height <= 0 || width > 32767 || height > 32767 ||
+ (unsigned long long)width * (unsigned)height > INT32_MAX / 4 || (flags & ~15u))
return yasl_public_fail(EINVAL);
- if(pumping || refresh_depth) return yasl_public_fail(EBUSY);
+ if (pumping || refresh_depth)
+ return yasl_public_fail(EBUSY);
return true;
}
-static void draw_committed(yasl_surface *s,struct yasl_private_frame *frame,void *ud)
+static void draw_committed(yasl_surface *s, struct yasl_private_frame *frame, void *ud)
{
(void)ud;
- if(s->refresh_callback && pumping && !refresh_depth) {
- ++refresh_depth; s->refresh_callback(s,s->refresh_userdata); --refresh_depth;
- }
- if(s->presenter!=YASL_PRIVATE_PRESENTER_CPU || !frame->target.pixels) return;
- int w=frame->target.size.x,h=frame->target.size.y;
- if(w==s->committed_width && h==s->committed_height && s->committed_pixels)
- memcpy(frame->target.pixels,s->committed_pixels,(size_t)w*h*4);
- else memset(frame->target.pixels,0,(size_t)w*h*4);
- struct yasl_private_damage_list damage={.n=-1};
- yasl_private_surface_present(s,&frame->target,&damage);
-}
-void yasl_public_register_surface(yasl_surface *s,unsigned flags,yasl_surface *parent)
-{
- s->creation_flags=flags; s->parent=parent;
- if(flags&YASL_SURFACE_CPU) s->presenter=YASL_PRIVATE_PRESENTER_CPU;
- struct yasl_private_window_cbs cb={.draw=draw_committed};
- yasl_private_surface_set_callbacks(s,&cb,NULL);
- s->reported_state=s->state;
- struct yasl_private_geometry geometry=yasl_private_surface_geometry(s);
- yasl_private_surface_notify_configure(s,geometry);
-}
-yasl_surface *yasl_surface_create(const char *title,int width,int height,unsigned flags)
-{
- if(!creation_valid(width,height,flags)) return NULL;
- bool fresh=!display;
- if(!initialize()) return NULL;
- struct yasl_private_window_spec spec={.title=title,.size={width,height},
- .flags=flags,.external_gpu=!(flags&YASL_SURFACE_CPU)};
- yasl_surface *s=yasl_private_surface_create_toplevel(display,&spec);
- if(!s) {
- int error=errno;
- if(fresh) { pthread_mutex_lock(&lifecycle_mutex); struct yasl_private_display *d=display; display=NULL;
- pthread_mutex_unlock(&lifecycle_mutex); yasl_private_display_close(d); }
- yasl_public_fail(error); return NULL;
- }
- yasl_public_register_surface(s,flags,NULL); return s;
-}
-void yasl_surface_destroy(yasl_surface *s)
-{
- if(!s) return;
- if(!yasl_public_surface(s,true)) return;
- for(;;) {
- yasl_surface *child=NULL;
- for(yasl_surface *p=display->surfaces;p;p=p->event_next) if(p->parent==s) { child=p; break; }
- if(!child) break;
+ if (s->refresh_callback && pumping && !refresh_depth) {
+ ++refresh_depth;
+ s->refresh_callback(s, s->refresh_userdata);
+ --refresh_depth;
+ }
+ if (s->presenter != YASL_PRIVATE_PRESENTER_CPU || !frame->target.pixels)
+ return;
+ int w = frame->target.size.x, h = frame->target.size.y;
+ if (w == s->committed_width && h == s->committed_height && s->committed_pixels)
+ memcpy(frame->target.pixels, s->committed_pixels, (size_t)w * h * 4);
+ else
+ memset(frame->target.pixels, 0, (size_t)w * h * 4);
+ struct yasl_private_damage_list damage = {.n = -1};
+ yasl_private_surface_present(s, &frame->target, &damage);
+}
+void yasl_public_register_surface(yasl_surface *s, unsigned flags, yasl_surface *parent)
+{
+ s->creation_flags = flags;
+ s->parent = parent;
+ if (flags & YASL_PRIVATE_SURFACE_CPU)
+ s->presenter = YASL_PRIVATE_PRESENTER_CPU;
+ struct yasl_private_window_cbs cb = {.draw = draw_committed};
+ yasl_private_surface_set_callbacks(s, &cb, NULL);
+ s->reported_state = s->state;
+ struct yasl_private_geometry geometry = yasl_private_surface_geometry(s);
+ yasl_private_surface_notify_configure(s, geometry);
+}
+yasl_surface *yasl_surface_create(const struct yasl_surface_options *options)
+{
+ if (!options || (unsigned)options->type > YASL_SURFACE_CPU || (options->flags & ~14u)) {
+ yasl_public_fail(EINVAL);
+ return NULL;
+ }
+ const char *title = options->title;
+ int width = options->width, height = options->height;
+ unsigned flags =
+ options->flags | (options->type == YASL_SURFACE_CPU ? YASL_PRIVATE_SURFACE_CPU : 0u);
+ if (!creation_valid(width, height, flags))
+ return NULL;
+ bool fresh = !display;
+ if (!initialize())
+ return NULL;
+ struct yasl_private_window_spec spec = {.title = title,
+ .size = {width, height},
+ .flags = flags,
+ .external_gpu = !(flags & YASL_PRIVATE_SURFACE_CPU)};
+ yasl_surface *s = yasl_private_surface_create_toplevel(display, &spec);
+ if (!s) {
+ int error = errno;
+ if (fresh) {
+ pthread_mutex_lock(&lifecycle_mutex);
+ struct yasl_private_display *d = display;
+ display = NULL;
+ pthread_mutex_unlock(&lifecycle_mutex);
+ yasl_private_display_close(d);
+ }
+ yasl_public_fail(error);
+ return NULL;
+ }
+ yasl_public_register_surface(s, flags, NULL);
+ s->userdata = options->userdata;
+ return s;
+}
+int yasl_surface_destroy(yasl_surface *s)
+{
+ if (!s)
+ return 0;
+ if (!yasl_public_surface(s, true))
+ return -errno;
+ for (yasl_surface *p = display->surfaces; p; p = p->event_next) {
+ for (yasl_surface *ancestor = p; ancestor; ancestor = ancestor->parent)
+ if (ancestor == s && p->vulkan)
+ return yasl_public_error(EBUSY);
+ }
+ for (;;) {
+ yasl_surface *child = NULL;
+ for (yasl_surface *p = display->surfaces; p; p = p->event_next)
+ if (p->parent == s) {
+ child = p;
+ break;
+ }
+ if (!child)
+ break;
yasl_surface_destroy(child);
}
- free(s->committed_pixels); s->committed_pixels=NULL;
- free(s->text_surrounding); s->text_surrounding=NULL;
+ free(s->cpu_pixels);
+ s->cpu_pixels = NULL;
+ free(s->committed_pixels);
+ s->committed_pixels = NULL;
+ free(s->text_surrounding);
+ s->text_surrounding = NULL;
yasl_private_surface_destroy(s);
+ return 0;
}
-void yasl_shutdown(void)
+int yasl_shutdown(void)
{
- if(display && !yasl_public_owner()) return;
- if(pumping || refresh_depth) { yasl_public_fail(EBUSY); return; }
- if(display) {
- while(display->surfaces) yasl_surface_destroy(display->surfaces);
- pthread_mutex_lock(&lifecycle_mutex); struct yasl_private_display *d=display; display=NULL;
- pthread_mutex_unlock(&lifecycle_mutex); yasl_private_display_close(d);
+ if (display && !yasl_public_owner())
+ return -errno;
+ if (pumping || refresh_depth) {
+ yasl_public_fail(EBUSY);
+ return -errno;
+ }
+ if (display) {
+ for (yasl_surface *s = display->surfaces; s; s = s->event_next)
+ if (s->vulkan)
+ return yasl_public_error(EBUSY);
+ while (display->surfaces)
+ yasl_surface_destroy(display->surfaces);
+ pthread_mutex_lock(&lifecycle_mutex);
+ struct yasl_private_display *d = display;
+ display = NULL;
+ pthread_mutex_unlock(&lifecycle_mutex);
+ yasl_private_display_close(d);
}
- free(delivered_text); delivered_text=NULL;
- free(application_id); application_id=NULL; requested_backend=YASL_BACKEND_AUTO;
+ free(delivered_text);
+ delivered_text = NULL;
#if YASL_HAS_VULKAN
extern void yasl_private_vulkan_reset(void);
yasl_private_vulkan_reset();
#endif
+ return 0;
}
void yasl_wake(void)
{
pthread_mutex_lock(&lifecycle_mutex);
- if(display && display->loop) yasl_private_loop_kick(display->loop);
+ if (display && display->loop)
+ yasl_private_loop_kick(display->loop);
pthread_mutex_unlock(&lifecycle_mutex);
}
-uint64_t yasl_time_ns(void) { return yasl_private_loop_now_ns(); }
+uint64_t yasl_time_ns(void)
+{
+ return yasl_private_loop_now_ns();
+}
static double scale_of(const yasl_surface *s)
-{ return s && s->scale_120 ? (double)s->scale_120/120.0 : 1.0; }
-static int logical(int pixels,double scale) { return (int)lround(pixels/scale); }
-enum yasl_backend yasl_surface_backend(const yasl_surface *s)
-{ return s ? (enum yasl_backend)s->display->ops->type : YASL_BACKEND_AUTO; }
-unsigned yasl_surface_features(const yasl_surface *s)
-{
- if(!s) return 0;
- unsigned caps=yasl_private_display_caps(s->display);
- if(s->display->ops->surface_create_gl) caps|=YASL_CAP_OPENGL;
+{
+ return s && s->scale_120 ? (double)s->scale_120 / 120.0 : 1.0;
+}
+static int logical(int pixels, double scale)
+{
+ return (int)lround(pixels / scale);
+}
+enum yasl_backend yasl_surface_get_backend(const yasl_surface *s)
+{
+ return s && yasl_public_surface((yasl_surface *)s, false)
+ ? (enum yasl_backend)s->display->ops->type
+ : YASL_BACKEND_AUTO;
+}
+unsigned yasl_surface_get_features(const yasl_surface *s)
+{
+ if (!s || !yasl_public_surface((yasl_surface *)s, false))
+ return 0;
+ unsigned caps = yasl_private_display_caps(s->display);
+ if (s->display->ops->surface_create_gl)
+ caps |= YASL_CAP_OPENGL;
#if YASL_HAS_VULKAN
- caps|=YASL_CAP_VULKAN;
+ caps |= YASL_CAP_VULKAN;
#endif
return caps;
}
-void yasl_surface_get_framebuffer_size(const yasl_surface *s,int *w,int *h)
-{ if(w) *w=s?s->size.x:0; if(h) *h=s?s->size.y:0; }
-void yasl_surface_get_size(const yasl_surface *s,int *w,int *h)
-{ double scale=scale_of(s); if(w) *w=s?logical(s->size.x,scale):0; if(h) *h=s?logical(s->size.y,scale):0; }
-double yasl_surface_get_scale(const yasl_surface *s) { return scale_of(s); }
-unsigned yasl_surface_state(const yasl_surface *s) { return s?s->state:0; }
-void *yasl_surface_userdata(const yasl_surface *s) { return s?s->userdata:NULL; }
-void yasl_surface_set_userdata(yasl_surface *s,void *ud) { if(yasl_public_surface(s,false)) s->userdata=ud; }
-bool yasl_surface_set_title(yasl_surface *s,const char *title)
-{
- if(!yasl_public_surface(s,false)) return false;
- if(!title) return yasl_public_fail(EINVAL);
- if(!s->display->ops->surface_set_title) return yasl_public_fail(ENOTSUP);
- s->display->ops->surface_set_title(s,title); return true;
-}
-#define REQUEST(name,priv,args,call) bool name args { if(!yasl_public_surface(s,false)) return false; errno=0; return priv call || yasl_public_fail(errno); }
-REQUEST(yasl_surface_request_size,yasl_private_surface_set_size,(yasl_surface *s,int w,int h),(s,((struct yasl_private_vector2i){w,h})))
-REQUEST(yasl_surface_request_fullscreen,yasl_private_surface_set_fullscreen,(yasl_surface *s,bool v),(s,v))
-REQUEST(yasl_surface_request_maximized,yasl_private_surface_set_maximized,(yasl_surface *s,bool v),(s,v))
-REQUEST(yasl_surface_request_minimize,yasl_private_surface_minimize,(yasl_surface *s),(s))
-REQUEST(yasl_surface_set_pointer_mode,yasl_private_surface_set_pointer_mode,(yasl_surface *s,enum yasl_pointer_mode m),(s,(enum yasl_private_pointer_mode)m))
-bool yasl_surface_set_cursor(yasl_surface *s,enum yasl_cursor cursor)
-{
- if(!yasl_public_surface(s,false)) return false;
- if(cursor<YASL_CURSOR_ARROW || cursor>YASL_CURSOR_HIDDEN) return yasl_public_fail(EINVAL);
- if(!s->display->ops->surface_set_cursor) return yasl_public_fail(ENOTSUP);
- yasl_private_surface_set_cursor(s,(enum yasl_private_cursor)cursor); return true;
-}
-void yasl_surface_set_refresh_callback(yasl_surface *s,void(*fn)(yasl_surface *,void *),void *ud)
-{ if(yasl_public_surface(s,false)) { s->refresh_callback=fn; s->refresh_userdata=ud; } }
+void yasl_surface_get_framebuffer_size(const yasl_surface *s, int *w, int *h)
+{
+ if (s && !yasl_public_surface((yasl_surface *)s, false))
+ s = NULL;
+ if (w)
+ *w = s ? s->size.x : 0;
+ if (h)
+ *h = s ? s->size.y : 0;
+}
+void yasl_surface_get_size(const yasl_surface *s, int *w, int *h)
+{
+ if (s && !yasl_public_surface((yasl_surface *)s, false))
+ s = NULL;
+ double scale = scale_of(s);
+ if (w)
+ *w = s ? logical(s->size.x, scale) : 0;
+ if (h)
+ *h = s ? logical(s->size.y, scale) : 0;
+}
+double yasl_surface_get_scale(const yasl_surface *s)
+{
+ return s && yasl_public_surface((yasl_surface *)s, false) ? scale_of(s) : 1.0;
+}
+unsigned yasl_surface_get_state(const yasl_surface *s)
+{
+ return s && yasl_public_surface((yasl_surface *)s, false) ? s->state : 0;
+}
+void *yasl_surface_get_userdata(const yasl_surface *s)
+{
+ return s && yasl_public_surface((yasl_surface *)s, false) ? s->userdata : NULL;
+}
+int yasl_surface_set_userdata(yasl_surface *s, void *ud)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ s->userdata = ud;
+ return 0;
+}
+int yasl_surface_set_title(yasl_surface *s, const char *title)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if (!title)
+ return yasl_public_error(EINVAL);
+ if (!s->display->ops->surface_set_title)
+ return yasl_public_error(ENOTSUP);
+ s->display->ops->surface_set_title(s, title);
+ return 0;
+}
+#define REQUEST(name, priv, args, call) \
+ int name args \
+ { \
+ if (!yasl_public_surface(s, false)) \
+ return -errno; \
+ errno = 0; \
+ return yasl_public_status(priv call || yasl_public_fail(errno)); \
+ }
+REQUEST(yasl_surface_request_size, yasl_private_surface_set_size, (yasl_surface * s, int w, int h),
+ (s, ((struct yasl_private_vector2i){w, h})))
+REQUEST(yasl_surface_request_fullscreen, yasl_private_surface_set_fullscreen,
+ (yasl_surface * s, bool v), (s, v))
+REQUEST(yasl_surface_request_maximized, yasl_private_surface_set_maximized,
+ (yasl_surface * s, bool v), (s, v))
+REQUEST(yasl_surface_request_minimize, yasl_private_surface_minimize, (yasl_surface * s), (s))
+REQUEST(yasl_surface_set_pointer_mode, yasl_private_surface_set_pointer_mode,
+ (yasl_surface * s, enum yasl_pointer_mode m), (s, (enum yasl_private_pointer_mode)m))
+int yasl_surface_set_cursor(yasl_surface *s, enum yasl_cursor cursor)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if (cursor < YASL_CURSOR_ARROW || cursor > YASL_CURSOR_HIDDEN)
+ return yasl_public_error(EINVAL);
+ if (!s->display->ops->surface_set_cursor)
+ return yasl_public_error(ENOTSUP);
+ yasl_private_surface_set_cursor(s, (enum yasl_private_cursor)cursor);
+ return 0;
+}
+int yasl_surface_set_refresh_callback(yasl_surface *s, void (*fn)(yasl_surface *, void *), void *ud)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ s->refresh_callback = fn;
+ s->refresh_userdata = ud;
+ return 0;
+}
void yasl_private_surface_notify_refresh(yasl_surface *s)
{
- if(!s || !s->display) return;
- struct yasl_private_event ev={.type=YASL_PRIVATE_EVENT_REFRESH,.surface=s};
- yasl_private_events_push(s->display,s,&ev);
- if(s->refresh_callback && pumping && !refresh_depth) {
- ++refresh_depth; s->refresh_callback(s,s->refresh_userdata); --refresh_depth;
+ if (!s || !s->display)
+ return;
+ struct yasl_private_event ev = {.type = YASL_PRIVATE_EVENT_REFRESH, .surface = s};
+ yasl_private_events_push(s->display, s, &ev);
+ if (s->refresh_callback && pumping && !refresh_depth) {
+ ++refresh_depth;
+ s->refresh_callback(s, s->refresh_userdata);
+ --refresh_depth;
}
}
-static char *copy_text(const char *data,size_t length)
+static char *copy_text(const char *data, size_t length)
{
- if(length==SIZE_MAX || (!data&&length)) { errno=EINVAL; return NULL; }
- char *p=malloc(length+1); if(!p) return NULL;
- if(length) memcpy(p,data,length);
- p[length]=0; return p;
+ if (length == SIZE_MAX || (!data && length)) {
+ errno = EINVAL;
+ return NULL;
+ }
+ char *p = malloc(length + 1);
+ if (!p)
+ return NULL;
+ if (length)
+ memcpy(p, data, length);
+ p[length] = 0;
+ return p;
}
-static const char *deliver_text(const char *data,size_t length)
+static const char *deliver_text(const char *data, size_t length)
{
- free(delivered_text); delivered_text=copy_text(data,length); return delivered_text;
+ free(delivered_text);
+ delivered_text = copy_text(data, length);
+ return delivered_text;
}
-/* Folds native size, state and focus changes into one WINDOW snapshot; unchanged values report nothing. */
-static bool window_event(yasl_surface *s,struct yasl_private_vector2i size,uint32_t scale_120,uint32_t state,yasl_event *e)
+/* Folds native size, state and focus changes into one WINDOW snapshot; unchanged values report
+ * nothing. */
+static bool window_event(yasl_surface *s, struct yasl_private_vector2i size, uint32_t scale_120,
+ uint32_t state, yasl_event *e)
{
- bool resized=size.x!=s->reported_size.x || size.y!=s->reported_size.y || scale_120!=s->reported_scale_120;
- if(!resized && state==s->reported_state) return false;
- s->reported_size=size; s->reported_scale_120=scale_120; s->reported_state=state;
- if(resized) s->refresh_owed=true;
- double scale=scale_120 ? scale_120/120.0 : 1.0;
- e->type=YASL_EVENT_WINDOW; e->window.pixel_width=size.x; e->window.pixel_height=size.y; e->window.scale=scale;
- e->window.width=logical(size.x,scale); e->window.height=logical(size.y,scale);
- e->window.state=state;
+ bool resized = size.x != s->reported_size.x || size.y != s->reported_size.y ||
+ scale_120 != s->reported_scale_120;
+ if (!resized && state == s->reported_state)
+ return false;
+ s->reported_size = size;
+ s->reported_scale_120 = scale_120;
+ s->reported_state = state;
+ if (resized)
+ s->refresh_owed = true;
+ double scale = scale_120 ? scale_120 / 120.0 : 1.0;
+ e->type = YASL_EVENT_WINDOW;
+ e->window.pixel_width = size.x;
+ e->window.pixel_height = size.y;
+ e->window.scale = scale;
+ e->window.width = logical(size.x, scale);
+ e->window.height = logical(size.y, scale);
+ e->window.state = state;
return true;
}
-static bool refresh_ready(yasl_surface *s) { return s->reported_size.x>0 && s->reported_size.y>0; }
-static bool convert_event(const struct yasl_private_event *p,yasl_event *e)
-{
- *e=(yasl_event){.surface=p->surface,.time_ns=p->time_ns};
- double scale=scale_of(p->surface);
- switch(p->type) {
- case YASL_PRIVATE_EVENT_WINDOW_CLOSE:e->type=YASL_EVENT_CLOSE;break;
- case YASL_PRIVATE_EVENT_DISPLAY_QUIT:e->type=YASL_EVENT_QUIT;break;
- case YASL_PRIVATE_EVENT_DISPLAY_REOPEN:e->type=YASL_EVENT_REOPEN;break;
- case YASL_PRIVATE_EVENT_DISPLAY_LOST:e->type=YASL_EVENT_DISPLAY_LOST;break;
- case YASL_PRIVATE_EVENT_DISPLAY_RESTORED:e->type=YASL_EVENT_DISPLAY_RESTORED;break;
+static bool refresh_ready(yasl_surface *s)
+{
+ return s->reported_size.x > 0 && s->reported_size.y > 0;
+}
+static bool convert_event(const struct yasl_private_event *p, yasl_event *e)
+{
+ *e = (yasl_event){.surface = p->surface, .time_ns = p->time_ns};
+ double scale = scale_of(p->surface);
+ switch (p->type) {
+ case YASL_PRIVATE_EVENT_WINDOW_CLOSE:
+ e->type = YASL_EVENT_CLOSE;
+ break;
+ case YASL_PRIVATE_EVENT_DISPLAY_QUIT:
+ e->type = YASL_EVENT_QUIT;
+ break;
+ case YASL_PRIVATE_EVENT_DISPLAY_REOPEN:
+ e->type = YASL_EVENT_REOPEN;
+ break;
+ case YASL_PRIVATE_EVENT_DISPLAY_LOST:
+ e->type = YASL_EVENT_DISPLAY_LOST;
+ break;
+ case YASL_PRIVATE_EVENT_DISPLAY_RESTORED:
+ e->type = YASL_EVENT_DISPLAY_RESTORED;
+ break;
case YASL_PRIVATE_EVENT_REFRESH:
- if(!p->surface) return false;
- if(!refresh_ready(p->surface)) { p->surface->refresh_owed=true; return false; }
- p->surface->refresh_owed=false; e->type=YASL_EVENT_REFRESH; break;
- case YASL_PRIVATE_EVENT_INPUT_RESET:e->type=YASL_EVENT_INPUT_RESET;break;
+ if (!p->surface)
+ return false;
+ if (!refresh_ready(p->surface)) {
+ p->surface->refresh_owed = true;
+ return false;
+ }
+ p->surface->refresh_owed = false;
+ e->type = YASL_EVENT_REFRESH;
+ break;
+ case YASL_PRIVATE_EVENT_INPUT_RESET:
+ e->type = YASL_EVENT_INPUT_RESET;
+ break;
case YASL_PRIVATE_EVENT_FOCUS: {
- yasl_surface *s=p->surface; if(!s) return false;
- uint32_t state=p->focus.focused ? s->reported_state|YASL_STATE_ACTIVATED : s->reported_state&~(uint32_t)YASL_STATE_ACTIVATED;
- return window_event(s,s->reported_size,s->reported_scale_120,state,e);
+ yasl_surface *s = p->surface;
+ if (!s)
+ return false;
+ uint32_t state = p->focus.focused ? s->reported_state | YASL_STATE_ACTIVATED
+ : s->reported_state & ~(uint32_t)YASL_STATE_ACTIVATED;
+ return window_event(s, s->reported_size, s->reported_scale_120, state, e);
}
case YASL_PRIVATE_EVENT_WINDOW_STATE:
- if(!p->surface) return false;
- return window_event(p->surface,p->surface->reported_size,p->surface->reported_scale_120,p->state.flags,e);
+ if (!p->surface)
+ return false;
+ return window_event(p->surface, p->surface->reported_size, p->surface->reported_scale_120,
+ p->state.flags, e);
case YASL_PRIVATE_EVENT_WINDOW_RESIZE:
- if(!p->surface) return false;
- return window_event(p->surface,p->resize.geometry.size,p->resize.geometry.scale_120,p->surface->reported_state,e);
+ if (!p->surface)
+ return false;
+ return window_event(p->surface, p->resize.geometry.size, p->resize.geometry.scale_120,
+ p->surface->reported_state, e);
case YASL_PRIVATE_EVENT_KEY:
- e->type=YASL_EVENT_KEY;e->key.key=p->key.key;e->key.action=(enum yasl_key_action)p->key.action;
- e->key.mods=p->key.mods;e->key.codepoint=p->key.codepoint;e->key.shifted=p->key.shifted;e->key.native_code=p->key.native_code;break;
+ e->type = YASL_EVENT_KEY;
+ e->key.key = p->key.key;
+ e->key.action = (enum yasl_key_action)p->key.action;
+ e->key.mods = p->key.mods;
+ e->key.codepoint = p->key.codepoint;
+ e->key.shifted = p->key.shifted;
+ e->key.native_code = p->key.native_code;
+ break;
case YASL_PRIVATE_EVENT_TEXT:
- e->type=YASL_EVENT_TEXT;e->text.length=p->text.len;e->text.data=deliver_text(p->text.data,p->text.len);return e->text.data!=NULL;
+ e->type = YASL_EVENT_TEXT;
+ e->text.length = p->text.len;
+ e->text.data = deliver_text(p->text.data, p->text.len);
+ return e->text.data != NULL;
case YASL_PRIVATE_EVENT_DROP:
- e->type=YASL_EVENT_DROP;e->drop.action=YASL_DROP_DATA;e->drop.length=p->text.len;
- e->drop.data=deliver_text(p->text.data,p->text.len);return e->drop.data!=NULL;
+ e->type = YASL_EVENT_DROP;
+ e->drop.action = YASL_DROP_DATA;
+ e->drop.length = p->text.len;
+ e->drop.data = deliver_text(p->text.data, p->text.len);
+ return e->drop.data != NULL;
case YASL_PRIVATE_EVENT_PASTE:
- e->type=YASL_EVENT_CLIPBOARD;e->clipboard.length=p->paste.len;e->clipboard.request_id=p->paste.request_id;
- e->clipboard.status=(enum yasl_transfer_status)p->paste.status;
- e->clipboard.data=deliver_text(p->paste.data,p->paste.len);return e->clipboard.data!=NULL;
+ e->type = YASL_EVENT_CLIPBOARD;
+ e->clipboard.length = p->paste.len;
+ e->clipboard.request_id = p->paste.request_id;
+ e->clipboard.status = (enum yasl_transfer_status)p->paste.status;
+ e->clipboard.data = deliver_text(p->paste.data, p->paste.len);
+ return e->clipboard.data != NULL;
case YASL_PRIVATE_EVENT_COMPOSITION:
- e->type=YASL_EVENT_COMPOSITION;e->composition.length=p->composition.len;e->composition.begin=p->composition.begin;
- e->composition.end=p->composition.end;e->composition.active=p->composition.active;
- e->composition.revision=p->composition.revision; e->composition.generation=p->composition.generation;
- e->composition.data=deliver_text(p->composition.data,p->composition.len);return e->composition.data!=NULL;
+ e->type = YASL_EVENT_COMPOSITION;
+ e->composition.length = p->composition.len;
+ e->composition.begin = p->composition.begin;
+ e->composition.end = p->composition.end;
+ e->composition.active = p->composition.active;
+ e->composition.revision = p->composition.revision;
+ e->composition.generation = p->composition.generation;
+ e->composition.data = deliver_text(p->composition.data, p->composition.len);
+ return e->composition.data != NULL;
case YASL_PRIVATE_EVENT_TEXT_EDIT:
- e->type=YASL_EVENT_TEXT_EDIT;e->text_edit.length=p->text_edit.len;e->text_edit.begin=p->text_edit.begin;e->text_edit.end=p->text_edit.end;
- e->text_edit.revision=p->text_edit.revision;e->text_edit.generation=p->text_edit.generation;
- e->text_edit.data=deliver_text(p->text_edit.data,p->text_edit.len);return e->text_edit.data!=NULL;
+ e->type = YASL_EVENT_TEXT_EDIT;
+ e->text_edit.length = p->text_edit.len;
+ e->text_edit.begin = p->text_edit.begin;
+ e->text_edit.end = p->text_edit.end;
+ e->text_edit.revision = p->text_edit.revision;
+ e->text_edit.generation = p->text_edit.generation;
+ e->text_edit.data = deliver_text(p->text_edit.data, p->text_edit.len);
+ return e->text_edit.data != NULL;
case YASL_PRIVATE_EVENT_PEN:
- e->type=YASL_EVENT_PEN;
-#define PEN(field) e->pen.field=p->pen.field
- PEN(device_id);PEN(tool_id);PEN(x);PEN(y);PEN(pressure);PEN(tilt_x);PEN(tilt_y);PEN(rotation);PEN(distance);PEN(tangential);PEN(wheel_degrees);
- PEN(axes_valid);PEN(axes_changed);PEN(buttons);PEN(changed_button);PEN(tool);PEN(action);PEN(in_contact);
+ e->type = YASL_EVENT_PEN;
+#define PEN(field) e->pen.field = p->pen.field
+ PEN(device_id);
+ PEN(tool_id);
+ PEN(x);
+ PEN(y);
+ PEN(pressure);
+ PEN(tilt_x);
+ PEN(tilt_y);
+ PEN(rotation);
+ PEN(distance);
+ PEN(tangential);
+ PEN(wheel_degrees);
+ PEN(axes_valid);
+ PEN(axes_changed);
+ PEN(buttons);
+ PEN(changed_button);
+ PEN(tool);
+ PEN(action);
+ PEN(in_contact);
#undef PEN
break;
case YASL_PRIVATE_EVENT_POINTER_SCROLL:
- e->type=YASL_EVENT_SCROLL;e->scroll.x=p->pointer.pos.x/scale;e->scroll.y=p->pointer.pos.y/scale;
- e->scroll.lines_x=p->pointer.scroll.x;e->scroll.lines_y=p->pointer.scroll.y;
- e->scroll.pixels_x=p->pointer.pixel_scroll.x/scale;e->scroll.pixels_y=p->pointer.pixel_scroll.y/scale;
- e->scroll.valid=p->pointer.scroll_valid?p->pointer.scroll_valid:YASL_SCROLL_LINES;
- e->scroll.source=p->pointer.scroll_source;e->scroll.phase=p->pointer.scroll_phase;e->scroll.momentum=p->pointer.scroll_momentum;e->scroll.mods=p->pointer.mods;break;
- case YASL_PRIVATE_EVENT_POINTER_DOWN:case YASL_PRIVATE_EVENT_POINTER_UP:case YASL_PRIVATE_EVENT_POINTER_MOTION:case YASL_PRIVATE_EVENT_POINTER_LEAVE:
- e->type=YASL_EVENT_POINTER;e->pointer.x=(p->pointer.precise_position?p->pointer.precise_x:p->pointer.pos.x)/scale;
- e->pointer.y=(p->pointer.precise_position?p->pointer.precise_y:p->pointer.pos.y)/scale;
- e->pointer.dx=p->pointer.delta.x/scale;e->pointer.dy=p->pointer.delta.y/scale;
- e->pointer.action=p->type==YASL_PRIVATE_EVENT_POINTER_DOWN?YASL_POINTER_DOWN:p->type==YASL_PRIVATE_EVENT_POINTER_UP?YASL_POINTER_UP:
- p->type==YASL_PRIVATE_EVENT_POINTER_LEAVE?YASL_POINTER_LEAVE:YASL_POINTER_MOVE;
- e->pointer.button=p->pointer.button;e->pointer.buttons=p->pointer.buttons;e->pointer.mods=p->pointer.mods;break;
+ e->type = YASL_EVENT_SCROLL;
+ e->scroll.x = p->pointer.pos.x / scale;
+ e->scroll.y = p->pointer.pos.y / scale;
+ e->scroll.lines_x = p->pointer.scroll.x;
+ e->scroll.lines_y = p->pointer.scroll.y;
+ e->scroll.pixels_x = p->pointer.pixel_scroll.x / scale;
+ e->scroll.pixels_y = p->pointer.pixel_scroll.y / scale;
+ e->scroll.valid = p->pointer.scroll_valid ? p->pointer.scroll_valid : YASL_SCROLL_LINES;
+ e->scroll.source = p->pointer.scroll_source;
+ e->scroll.phase = p->pointer.scroll_phase;
+ e->scroll.momentum = p->pointer.scroll_momentum;
+ e->scroll.mods = p->pointer.mods;
+ break;
+ case YASL_PRIVATE_EVENT_POINTER_DOWN:
+ case YASL_PRIVATE_EVENT_POINTER_UP:
+ case YASL_PRIVATE_EVENT_POINTER_MOTION:
+ case YASL_PRIVATE_EVENT_POINTER_LEAVE:
+ e->type = YASL_EVENT_POINTER;
+ e->pointer.x =
+ (p->pointer.precise_position ? p->pointer.precise_x : p->pointer.pos.x) / scale;
+ e->pointer.y =
+ (p->pointer.precise_position ? p->pointer.precise_y : p->pointer.pos.y) / scale;
+ e->pointer.dx = p->pointer.delta.x / scale;
+ e->pointer.dy = p->pointer.delta.y / scale;
+ e->pointer.action = p->type == YASL_PRIVATE_EVENT_POINTER_DOWN ? YASL_POINTER_DOWN
+ : p->type == YASL_PRIVATE_EVENT_POINTER_UP ? YASL_POINTER_UP
+ : p->type == YASL_PRIVATE_EVENT_POINTER_LEAVE ? YASL_POINTER_LEAVE
+ : YASL_POINTER_MOVE;
+ e->pointer.button = p->pointer.button;
+ e->pointer.buttons = p->pointer.buttons;
+ e->pointer.mods = p->pointer.mods;
+ break;
case YASL_PRIVATE_EVENT_DRAG:
- e->type=YASL_EVENT_DRAG;e->drag.action=(enum yasl_drag_action)p->drag.action;
- e->drag.x=p->drag.pos.x/scale;e->drag.y=p->drag.pos.y/scale;break;
+ e->type = YASL_EVENT_DRAG;
+ e->drag.action = (enum yasl_drag_action)p->drag.action;
+ e->drag.x = p->drag.pos.x / scale;
+ e->drag.y = p->drag.pos.y / scale;
+ break;
case YASL_PRIVATE_EVENT_DROP_HOVER:
- e->type=YASL_EVENT_DROP;e->drop.action=p->drop.action==YASL_PRIVATE_DROP_LEAVE?YASL_DROP_LEAVE:YASL_DROP_OVER;
- e->drop.x=p->drop.pos.x/scale;e->drop.y=p->drop.pos.y/scale;break;
- default:return false;
+ e->type = YASL_EVENT_DROP;
+ e->drop.action =
+ p->drop.action == YASL_PRIVATE_DROP_LEAVE ? YASL_DROP_LEAVE : YASL_DROP_OVER;
+ e->drop.x = p->drop.pos.x / scale;
+ e->drop.y = p->drop.pos.y / scale;
+ break;
+ default:
+ return false;
}
return true;
}
static int take_event(yasl_event *out)
{
- for(yasl_surface *s=display->surfaces;s;s=s->event_next) if(s->refresh_owed && refresh_ready(s)) {
- s->refresh_owed=false;
- *out=(yasl_event){.type=YASL_EVENT_REFRESH,.surface=s,.time_ns=yasl_time_ns()};
- return 1;
- }
+ for (yasl_surface *s = display->surfaces; s; s = s->event_next)
+ if (s->refresh_owed && refresh_ready(s)) {
+ s->refresh_owed = false;
+ *out =
+ (yasl_event){.type = YASL_EVENT_REFRESH, .surface = s, .time_ns = yasl_time_ns()};
+ return 1;
+ }
struct yasl_private_event p;
- while(yasl_private_display_poll_event(display,&p)) {
- if(p.surface && ((p.type==YASL_PRIVATE_EVENT_COMPOSITION && p.composition.generation!=p.surface->text_generation) ||
- (p.type==YASL_PRIVATE_EVENT_TEXT_EDIT && p.text_edit.generation!=p.surface->text_generation))) continue;
+ while (yasl_private_display_poll_event(display, &p)) {
+ if (p.surface && ((p.type == YASL_PRIVATE_EVENT_COMPOSITION &&
+ p.composition.generation != p.surface->text_generation) ||
+ (p.type == YASL_PRIVATE_EVENT_TEXT_EDIT &&
+ p.text_edit.generation != p.surface->text_generation)))
+ continue;
yasl_event event;
- errno=0;
- if(convert_event(&p,&event)) { *out=event;return 1; }
- if(errno) { yasl_public_fail(errno);return -1; }
+ errno = 0;
+ if (convert_event(&p, &event)) {
+ *out = event;
+ return 1;
+ }
+ if (errno) {
+ yasl_public_fail(errno);
+ return -errno;
+ }
}
return 0;
}
-int yasl_wait_event(yasl_event *out,int timeout)
+int yasl_wait_event(yasl_event *out, int timeout)
{
- if(!out) { yasl_public_fail(EINVAL);return -1; }
- *out=(yasl_event){.type=YASL_EVENT_NONE};
- if(!yasl_public_owner()) return -1;
- if(pumping || refresh_depth) { yasl_public_fail(EBUSY);return -1; }
- free(delivered_text); delivered_text=NULL;
- int n=take_event(out); if(n) return n;
- pumping=true;
+ if (!out) {
+ yasl_public_fail(EINVAL);
+ return -errno;
+ }
+ *out = (yasl_event){.type = YASL_EVENT_NONE};
+ if (!yasl_public_owner())
+ return -errno;
+ if (pumping || refresh_depth) {
+ yasl_public_fail(EBUSY);
+ return -errno;
+ }
+ for (yasl_surface *s = display->surfaces; s; s = s->event_next)
+ s->cpu_acquired = false;
+ free(delivered_text);
+ delivered_text = NULL;
+ int n = take_event(out);
+ if (n)
+ return n;
+ pumping = true;
yasl_private_display_flush(display);
- n=take_event(out);
- if(!n) {
- int work=yasl_private_display_dispatch(display,timeout);
- if(work<0) { yasl_public_fail(errno);n=-1; }
+ n = take_event(out);
+ if (!n) {
+ int work = yasl_private_display_dispatch(display, timeout);
+ if (work < 0) {
+ yasl_public_fail(errno);
+ n = -errno;
+ }
yasl_private_display_flush(display);
- if(n>=0) n=take_event(out);
- }
- pumping=false;return n;
-}
-uint32_t *yasl_cpu_pixels(yasl_surface *s,int *w,int *h)
-{
- if(w) *w=0;
- if(h) *h=0;
- if(!yasl_public_surface(s,false)) return NULL;
- if(s->presenter!=YASL_PRIVATE_PRESENTER_CPU) { yasl_public_fail(ENOTSUP); return NULL; }
- int width=s->size.x,height=s->size.y;
- if(width<=0 || height<=0) { yasl_public_fail(EBUSY); return NULL; }
- if(width!=s->committed_width || height!=s->committed_height || !s->committed_pixels) {
- uint32_t *next=calloc((size_t)width*height,4);
- if(!next) { yasl_public_fail(ENOMEM); return NULL; }
- int keep_w=width<s->committed_width?width:s->committed_width,keep_h=height<s->committed_height?height:s->committed_height;
- if(s->committed_pixels) for(int y=0;y<keep_h;y++)
- memcpy(next+(size_t)y*width,s->committed_pixels+(size_t)y*s->committed_width,(size_t)keep_w*4);
- free(s->committed_pixels); s->committed_pixels=next; s->committed_width=width; s->committed_height=height;
- }
- if(w) *w=width;
- if(h) *h=height;
- return s->committed_pixels;
-}
-bool yasl_cpu_present(yasl_surface *s)
-{
- if(!yasl_public_surface(s,false)) return false;
- if(s->presenter!=YASL_PRIVATE_PRESENTER_CPU) return yasl_public_fail(ENOTSUP);
- if(!s->committed_pixels || s->committed_width!=s->size.x || s->committed_height!=s->size.y) return yasl_public_fail(EINVAL);
- yasl_private_surface_request_frame(s); return true;
-}
-int yasl_double_click_ms(void) { return display?yasl_private_display_double_click_ms(display):400; }
-bool yasl_clipboard_set(enum yasl_selection sel,const char *text)
-{ if(!yasl_public_owner())return false;errno=0;return yasl_private_clipboard_set(display,(enum yasl_private_selection)sel,text)||yasl_public_fail(errno); }
-uint64_t yasl_clipboard_request(yasl_surface *s,enum yasl_selection sel)
-{ if(!yasl_public_surface(s,false))return 0;errno=0;uint64_t id=yasl_private_clipboard_request(s,(enum yasl_private_selection)sel);if(!id)yasl_public_fail(errno);return id; }
+ if (n >= 0)
+ n = take_event(out);
+ }
+ pumping = false;
+ return n;
+}
+static uint64_t cpu_sequence;
+int yasl_cpu_acquire(yasl_surface *s, struct yasl_cpu_buffer *out)
+{
+ if (!out)
+ return yasl_public_error(EINVAL);
+ *out = (struct yasl_cpu_buffer){0};
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if (s->presenter != YASL_PRIVATE_PRESENTER_CPU)
+ return yasl_public_error(ENOTSUP);
+ if (s->display->offline)
+ return yasl_public_error(ENOTCONN);
+ if (s->cpu_acquired)
+ return yasl_public_error(EBUSY);
+ int w = s->size.x, h = s->size.y;
+ if (w <= 0 || h <= 0)
+ return yasl_public_error(EAGAIN);
+ if ((size_t)w > SIZE_MAX / sizeof(uint32_t) / (size_t)h)
+ return yasl_public_error(EOVERFLOW);
+ if (s->cpu_width != w || s->cpu_height != h || !s->cpu_pixels) {
+ uint32_t *pixels = malloc((size_t)w * h * sizeof(*pixels));
+ if (!pixels)
+ return yasl_public_error(ENOMEM);
+ free(s->cpu_pixels);
+ s->cpu_pixels = pixels;
+ s->cpu_width = w;
+ s->cpu_height = h;
+ }
+ memset(s->cpu_pixels, 0, (size_t)w * h * sizeof(uint32_t));
+ int keep_w = w < s->committed_width ? w : s->committed_width;
+ int keep_h = h < s->committed_height ? h : s->committed_height;
+ if (s->committed_pixels)
+ for (int y = 0; y < keep_h; y++)
+ memcpy(s->cpu_pixels + (size_t)y * w,
+ s->committed_pixels + (size_t)y * s->committed_width,
+ (size_t)keep_w * sizeof(uint32_t));
+ if (++cpu_sequence == 0)
+ ++cpu_sequence;
+ s->cpu_token = cpu_sequence;
+ s->cpu_acquired = true;
+ *out =
+ (struct yasl_cpu_buffer){s->cpu_pixels, w, h, (size_t)w * sizeof(uint32_t), s->cpu_token};
+ return 0;
+}
+static int cpu_validate(yasl_surface *s, const struct yasl_cpu_buffer *buffer)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if (!buffer)
+ return yasl_public_error(EINVAL);
+ if (s->presenter != YASL_PRIVATE_PRESENTER_CPU)
+ return yasl_public_error(ENOTSUP);
+ if (!s->cpu_acquired || !buffer->token || buffer->token != s->cpu_token ||
+ s->cpu_width != s->size.x || s->cpu_height != s->size.y)
+ return yasl_public_error(ESTALE);
+ if (buffer->pixels != s->cpu_pixels || buffer->width != s->cpu_width ||
+ buffer->height != s->cpu_height ||
+ buffer->stride_bytes != (size_t)s->cpu_width * sizeof(uint32_t))
+ return yasl_public_error(EINVAL);
+ return 0;
+}
+int yasl_cpu_present(yasl_surface *s, const struct yasl_cpu_buffer *buffer)
+{
+ int error = cpu_validate(s, buffer);
+ if (error)
+ return error;
+ if (s->display->offline)
+ return yasl_public_error(ENOTCONN);
+ size_t bytes = (size_t)s->cpu_width * s->cpu_height * sizeof(uint32_t);
+ if (s->committed_width != s->cpu_width || s->committed_height != s->cpu_height ||
+ !s->committed_pixels) {
+ uint32_t *pixels = malloc(bytes);
+ if (!pixels)
+ return yasl_public_error(ENOMEM);
+ free(s->committed_pixels);
+ s->committed_pixels = pixels;
+ s->committed_width = s->cpu_width;
+ s->committed_height = s->cpu_height;
+ }
+ memcpy(s->committed_pixels, s->cpu_pixels, bytes);
+ s->cpu_acquired = false;
+ yasl_private_surface_request_frame(s);
+ return 0;
+}
+int yasl_cpu_discard(yasl_surface *s, const struct yasl_cpu_buffer *buffer)
+{
+ int error = cpu_validate(s, buffer);
+ if (error)
+ return error;
+ s->cpu_acquired = false;
+ return 0;
+}
+int yasl_get_double_click_ms(void)
+{
+ return display && yasl_public_owner() ? yasl_private_display_double_click_ms(display) : 400;
+}
+int yasl_clipboard_set(enum yasl_selection sel, const char *text)
+{
+ if (!yasl_public_owner())
+ return -errno;
+ errno = 0;
+ return yasl_public_status(
+ yasl_private_clipboard_set(display, (enum yasl_private_selection)sel, text) ||
+ yasl_public_fail(errno));
+}
+uint64_t yasl_clipboard_request(yasl_surface *s, enum yasl_selection sel)
+{
+ if (!yasl_public_surface(s, false))
+ return 0;
+ errno = 0;
+ uint64_t id = yasl_private_clipboard_request(s, (enum yasl_private_selection)sel);
+ if (!id)
+ yasl_public_fail(errno);
+ return id;
+}
static bool text_update(yasl_surface *s)
-{ if(!s->display->ops->surface_update_text_input)return true;errno=0;return s->display->ops->surface_update_text_input(s)||yasl_public_fail(errno); }
-bool yasl_surface_set_text_input(yasl_surface *s,bool active,enum yasl_text_purpose purpose)
{
- if(!yasl_public_surface(s,false))return false;
- if(purpose<YASL_TEXT_NORMAL||purpose>YASL_TEXT_TERMINAL)return yasl_public_fail(EINVAL);
- bool old=s->text_input_active;unsigned old_purpose=s->text_purpose;uint64_t generation=s->text_generation;
- if(old!=active || old_purpose!=(unsigned)purpose)++s->text_generation;
- s->text_input_active=active;s->text_purpose=purpose;
- if(!text_update(s)) { s->text_input_active=old;s->text_purpose=old_purpose;s->text_generation=generation;return false; }return true;
+ if (!s->display->ops->surface_update_text_input)
+ return true;
+ errno = 0;
+ return s->display->ops->surface_update_text_input(s) || yasl_public_fail(errno);
}
-bool yasl_surface_set_surrounding_text(yasl_surface *s,const char *text,size_t len,size_t begin,size_t end,uint64_t revision)
+int yasl_surface_set_text_input(yasl_surface *s, bool active, enum yasl_text_purpose purpose)
{
- if(!yasl_public_surface(s,false))return false;
- if((!text&&len)||begin>end||end>len||len>INT32_MAX || (len&&memchr(text,0,len)))return yasl_public_fail(EINVAL);
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if (purpose < YASL_TEXT_NORMAL || purpose > YASL_TEXT_TERMINAL)
+ return yasl_public_error(EINVAL);
+ bool old = s->text_input_active;
+ unsigned old_purpose = s->text_purpose;
+ uint64_t generation = s->text_generation;
+ if (old != active || old_purpose != (unsigned)purpose)
+ ++s->text_generation;
+ s->text_input_active = active;
+ s->text_purpose = purpose;
+ if (!text_update(s)) {
+ s->text_input_active = old;
+ s->text_purpose = old_purpose;
+ s->text_generation = generation;
+ return -errno;
+ }
+ return 0;
+}
+int yasl_surface_set_surrounding_text(yasl_surface *s, const char *text, size_t len, size_t begin,
+ size_t end, uint64_t revision)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ if ((!text && len) || begin > end || end > len || len > INT32_MAX ||
+ (len && memchr(text, 0, len)))
+ return yasl_public_error(EINVAL);
/* Validate UTF-8 and require selection boundaries between code points. */
- if((begin<len&&((unsigned char)text[begin]&0xc0)==0x80)||(end<len&&((unsigned char)text[end]&0xc0)==0x80))return yasl_public_fail(EINVAL);
- for(size_t i=0;i<len;) { unsigned char c=(unsigned char)text[i];size_t n=c<0x80?1:c>=0xc2&&c<=0xdf?2:c>=0xe0&&c<=0xef?3:c>=0xf0&&c<=0xf4?4:0;
- if(!n||i+n>len)return yasl_public_fail(EILSEQ);
- for(size_t j=1;j<n;j++)if(((unsigned char)text[i+j]&0xc0)!=0x80)return yasl_public_fail(EILSEQ);
- if(n>=3 && ((c==0xe0&&(unsigned char)text[i+1]<0xa0)||(c==0xed&&(unsigned char)text[i+1]>=0xa0)||
- (c==0xf0&&(unsigned char)text[i+1]<0x90)||(c==0xf4&&(unsigned char)text[i+1]>=0x90)))return yasl_public_fail(EILSEQ);
- i+=n;
+ if ((begin < len && ((unsigned char)text[begin] & 0xc0) == 0x80) ||
+ (end < len && ((unsigned char)text[end] & 0xc0) == 0x80))
+ return yasl_public_error(EINVAL);
+ for (size_t i = 0; i < len;) {
+ unsigned char c = (unsigned char)text[i];
+ size_t n = c < 0x80 ? 1
+ : c >= 0xc2 && c <= 0xdf ? 2
+ : c >= 0xe0 && c <= 0xef ? 3
+ : c >= 0xf0 && c <= 0xf4 ? 4
+ : 0;
+ if (!n || i + n > len)
+ return yasl_public_error(EILSEQ);
+ for (size_t j = 1; j < n; j++)
+ if (((unsigned char)text[i + j] & 0xc0) != 0x80)
+ return yasl_public_error(EILSEQ);
+ if (n >= 3 && ((c == 0xe0 && (unsigned char)text[i + 1] < 0xa0) ||
+ (c == 0xed && (unsigned char)text[i + 1] >= 0xa0) ||
+ (c == 0xf0 && (unsigned char)text[i + 1] < 0x90) ||
+ (c == 0xf4 && (unsigned char)text[i + 1] >= 0x90)))
+ return yasl_public_error(EILSEQ);
+ i += n;
+ }
+ char *copy = copy_text(text, len);
+ if (!copy)
+ return yasl_public_error(errno);
+ char *old = s->text_surrounding;
+ size_t old_len = s->text_length, old_begin = s->text_begin, old_end = s->text_end;
+ uint64_t old_rev = s->text_revision;
+ s->text_surrounding = copy;
+ s->text_length = len;
+ s->text_begin = begin;
+ s->text_end = end;
+ s->text_revision = revision;
+ if (!text_update(s)) {
+ free(copy);
+ s->text_surrounding = old;
+ s->text_length = old_len;
+ s->text_begin = old_begin;
+ s->text_end = old_end;
+ s->text_revision = old_rev;
+ return -errno;
}
- char *copy=copy_text(text,len);if(!copy)return yasl_public_fail(errno);
- char *old=s->text_surrounding;size_t old_len=s->text_length,old_begin=s->text_begin,old_end=s->text_end;uint64_t old_rev=s->text_revision;
- s->text_surrounding=copy;s->text_length=len;s->text_begin=begin;s->text_end=end;s->text_revision=revision;
- if(!text_update(s)){free(copy);s->text_surrounding=old;s->text_length=old_len;s->text_begin=old_begin;s->text_end=old_end;s->text_revision=old_rev;return false;}
- free(old);return true;
+ free(old);
+ return 0;
}
-static bool logical_rect(yasl_surface *s,double x,double y,double w,double h,struct yasl_private_rect2i *r)
+static bool logical_rect(yasl_surface *s, double x, double y, double w, double h,
+ struct yasl_private_rect2i *r)
{
- if(!isfinite(x)||!isfinite(y)||!isfinite(w)||!isfinite(h)||w<0||h<0)return yasl_public_fail(EINVAL);
- r->x=yasl_private_scale_to_device(x,s->scale_120);r->y=yasl_private_scale_to_device(y,s->scale_120);
- r->width=yasl_private_scale_to_device(w,s->scale_120);r->height=yasl_private_scale_to_device(h,s->scale_120);return true;
+ if (!isfinite(x) || !isfinite(y) || !isfinite(w) || !isfinite(h) || w < 0 || h < 0)
+ return yasl_public_fail(EINVAL);
+ double scale = scale_of(s);
+ if (fabs(x) * scale > INT_MAX || fabs(y) * scale > INT_MAX || w * scale > INT_MAX ||
+ h * scale > INT_MAX)
+ return yasl_public_fail(EOVERFLOW);
+ r->x = yasl_private_scale_to_device(x, s->scale_120);
+ r->y = yasl_private_scale_to_device(y, s->scale_120);
+ r->width = yasl_private_scale_to_device(w, s->scale_120);
+ r->height = yasl_private_scale_to_device(h, s->scale_120);
+ return true;
}
-bool yasl_surface_set_ime_caret(yasl_surface *s,double x,double y,double w,double h)
+int yasl_surface_set_ime_caret(yasl_surface *s, double x, double y, double w, double h)
{
- if(!yasl_public_surface(s,false))return false;
+ if (!yasl_public_surface(s, false))
+ return -errno;
struct yasl_private_rect2i r;
- if(!logical_rect(s,x,y,w,h,&r))return false;
- if(!s->display->ops->surface_set_ime_caret)return yasl_public_fail(ENOTSUP);
- yasl_private_surface_set_ime_caret(s,r);return true;
+ if (!logical_rect(s, x, y, w, h, &r))
+ return -errno;
+ if (!s->display->ops->surface_set_ime_caret)
+ return yasl_public_error(ENOTSUP);
+ yasl_private_surface_set_ime_caret(s, r);
+ return 0;
}
-static bool trigger_valid(yasl_surface *s,const yasl_event *e)
+static bool trigger_valid(yasl_surface *s, const yasl_event *e)
{
- if(!e||e->surface!=s||!e->time_ns||yasl_time_ns()-e->time_ns>5000000000ull ||
- !((e->type==YASL_EVENT_POINTER&&e->pointer.action==YASL_POINTER_DOWN)||(e->type==YASL_EVENT_PEN&&e->pen.action==YASL_PEN_DOWN))) {
- yasl_public_set_error(YASL_ERROR_CONTEXT_EXPIRED,0);return false;
- }return true;
+ if (!e || e->surface != s || !e->time_ns || yasl_time_ns() - e->time_ns > 5000000000ull ||
+ !((e->type == YASL_EVENT_POINTER && e->pointer.action == YASL_POINTER_DOWN) ||
+ (e->type == YASL_EVENT_PEN && e->pen.action == YASL_PEN_DOWN))) {
+ return yasl_public_fail(ESTALE);
+ }
+ return true;
}
-static bool popup_spec(yasl_surface *s,int w,int h,double x,double y,double aw,double ah,enum yasl_popup_edge anchor,enum yasl_popup_edge gravity,unsigned constraints,struct yasl_private_popup_spec *out)
+static bool popup_spec(yasl_surface *s, int w, int h, double x, double y, double aw, double ah,
+ enum yasl_popup_edge anchor, enum yasl_popup_edge gravity,
+ unsigned constraints, struct yasl_private_popup_spec *out)
{
- if(w<=0||h<=0 || (unsigned)anchor>8 || (unsigned)gravity>8 || (constraints&~63u))return yasl_public_fail(EINVAL);
- *out=(struct yasl_private_popup_spec){.size={yasl_private_scale_to_device(w,s->scale_120),yasl_private_scale_to_device(h,s->scale_120)},.anchor_edges=anchor,.gravity=gravity,.constraints=constraints};
- return logical_rect(s,x,y,aw,ah,&out->anchor);
+ if (w <= 0 || h <= 0 || w > 32767 || h > 32767 || (unsigned)anchor > 8 ||
+ (unsigned)gravity > 8 || (constraints & ~63u))
+ return yasl_public_fail(EINVAL);
+ if (w * scale_of(s) > 32767 || h * scale_of(s) > 32767)
+ return yasl_public_fail(EOVERFLOW);
+ *out = (struct yasl_private_popup_spec){.size = {yasl_private_scale_to_device(w, s->scale_120),
+ yasl_private_scale_to_device(h, s->scale_120)},
+ .anchor_edges = anchor,
+ .gravity = gravity,
+ .constraints = constraints};
+ return logical_rect(s, x, y, aw, ah, &out->anchor);
}
-yasl_surface *yasl_popup_create(yasl_surface *parent,int w,int h,double x,double y,double aw,double ah,enum yasl_popup_edge anchor,enum yasl_popup_edge gravity,unsigned constraints,const yasl_event *trigger)
+yasl_surface *yasl_popup_create(yasl_surface *parent, const struct yasl_popup_options *options,
+ const yasl_event *trigger)
{
- if(!yasl_public_surface(parent,true)||!trigger_valid(parent,trigger))return NULL;
- struct yasl_private_popup_spec spec;if(!popup_spec(parent,w,h,x,y,aw,ah,anchor,gravity,constraints,&spec))return NULL;
- yasl_surface *s=yasl_private_surface_create_popup(parent,&spec);if(!s){yasl_public_fail(errno);return NULL;}
- yasl_public_register_surface(s,parent->creation_flags,parent);return s;
+ if (!options) {
+ yasl_public_fail(EINVAL);
+ return NULL;
+ }
+ int w = options->width, h = options->height;
+ double x = options->x, y = options->y, aw = options->anchor_width, ah = options->anchor_height;
+ enum yasl_popup_edge anchor = options->anchor, gravity = options->gravity;
+ unsigned constraints = options->constraints;
+ if (!yasl_public_surface(parent, true) || !trigger_valid(parent, trigger))
+ return NULL;
+ struct yasl_private_popup_spec spec;
+ if (!popup_spec(parent, w, h, x, y, aw, ah, anchor, gravity, constraints, &spec))
+ return NULL;
+ yasl_surface *s = yasl_private_surface_create_popup(parent, &spec);
+ if (!s) {
+ yasl_public_fail(errno);
+ return NULL;
+ }
+ yasl_public_register_surface(s, parent->creation_flags, parent);
+ return s;
}
-bool yasl_popup_reposition(yasl_surface *s,int w,int h,double x,double y,double aw,double ah,enum yasl_popup_edge anchor,enum yasl_popup_edge gravity,unsigned constraints)
+int yasl_popup_reposition(yasl_surface *s, const struct yasl_popup_options *options)
{
- if(!yasl_public_surface(s,false))return false;
+ if (!options)
+ return yasl_public_error(EINVAL);
+ int w = options->width, h = options->height;
+ double x = options->x, y = options->y, aw = options->anchor_width, ah = options->anchor_height;
+ enum yasl_popup_edge anchor = options->anchor, gravity = options->gravity;
+ unsigned constraints = options->constraints;
+ if (!yasl_public_surface(s, false))
+ return -errno;
struct yasl_private_popup_spec spec;
- if(!s->parent)return yasl_public_fail(EINVAL);
- if(!popup_spec(s->parent,w,h,x,y,aw,ah,anchor,gravity,constraints,&spec))return false;
- errno=0;return yasl_private_surface_popup_reposition(s,&spec)||yasl_public_fail(errno);
-}
-bool yasl_surface_start_interactive_move(yasl_surface *s,const yasl_event *e)
-{ if(!yasl_public_surface(s,false)||!trigger_valid(s,e))return false;errno=0;return yasl_private_surface_start_interactive_move(s)||yasl_public_fail(errno); }
-bool yasl_surface_start_interactive_resize(yasl_surface *s,enum yasl_resize_edge edge,const yasl_event *e)
-{ if(!yasl_public_surface(s,false)||!trigger_valid(s,e))return false;errno=0;return yasl_private_surface_start_interactive_resize(s,(enum yasl_private_resize_edge)edge)||yasl_public_fail(errno); }
-bool yasl_surface_show_window_menu(yasl_surface *s,double x,double y,const yasl_event *e)
-{ if(!yasl_public_surface(s,false)||!trigger_valid(s,e))return false;
- struct yasl_private_rect2i r;if(!logical_rect(s,x,y,0,0,&r))return false;errno=0;return yasl_private_surface_show_window_menu(s,(struct yasl_private_vector2i){r.x,r.y})||yasl_public_fail(errno); }
-bool yasl_drag_start(yasl_surface *s,const yasl_event *e,const uint32_t *pixels,int w,int h,int hx,int hy)
-{
- if(!yasl_public_surface(s,false)||!trigger_valid(s,e))return false;
- struct yasl_private_drag_image image={.pixels=pixels,.size={w,h},.hotspot={hx,hy}};
- errno=0;return yasl_private_display_drag_start(display,s,pixels?&image:NULL,NULL,NULL)||yasl_public_fail(errno);
-}
-void yasl_drag_cancel(void) { if(yasl_public_owner())yasl_private_display_drag_cancel(display); }
-bool yasl_surface_drag_carry(yasl_surface *s,double x,double y)
-{ if(!yasl_public_surface(s,false))return false;
- struct yasl_private_rect2i r;if(!logical_rect(s,x,y,0,0,&r))return false;errno=0;return yasl_private_surface_drag_carry(s,(struct yasl_private_vector2i){r.x,r.y})||yasl_public_fail(errno); }
-bool yasl_surface_place_under_pointer(yasl_surface *s,double x,double y)
-{ if(!yasl_public_surface(s,false))return false;
- struct yasl_private_rect2i r;if(!logical_rect(s,x,y,0,0,&r))return false;errno=0;return yasl_private_surface_place_under_pointer(s,(struct yasl_private_vector2i){r.x,r.y})||yasl_public_fail(errno); }
-size_t yasl_drop_next_path(const char **cursor,char *out,size_t cap)
-{ return yasl_private_drop_next_path(cursor,out,cap); }
+ if (!s->parent)
+ return yasl_public_error(EINVAL);
+ if (!popup_spec(s->parent, w, h, x, y, aw, ah, anchor, gravity, constraints, &spec))
+ return -errno;
+ errno = 0;
+ return yasl_public_status(yasl_private_surface_popup_reposition(s, &spec) ||
+ yasl_public_fail(errno));
+}
+int yasl_surface_start_interactive_move(yasl_surface *s, const yasl_event *e)
+{
+ if (!yasl_public_surface(s, false) || !trigger_valid(s, e))
+ return -errno;
+ errno = 0;
+ return yasl_public_status(yasl_private_surface_start_interactive_move(s) ||
+ yasl_public_fail(errno));
+}
+int yasl_surface_start_interactive_resize(yasl_surface *s, enum yasl_resize_edge edge,
+ const yasl_event *e)
+{
+ if (!yasl_public_surface(s, false) || !trigger_valid(s, e))
+ return -errno;
+ errno = 0;
+ return yasl_public_status(
+ yasl_private_surface_start_interactive_resize(s, (enum yasl_private_resize_edge)edge) ||
+ yasl_public_fail(errno));
+}
+int yasl_surface_show_window_menu(yasl_surface *s, double x, double y, const yasl_event *e)
+{
+ if (!yasl_public_surface(s, false) || !trigger_valid(s, e))
+ return -errno;
+ struct yasl_private_rect2i r;
+ if (!logical_rect(s, x, y, 0, 0, &r))
+ return -errno;
+ errno = 0;
+ return yasl_public_status(
+ yasl_private_surface_show_window_menu(s, (struct yasl_private_vector2i){r.x, r.y}) ||
+ yasl_public_fail(errno));
+}
+int yasl_drag_start(yasl_surface *s, const yasl_event *e, const struct yasl_drag_image *source)
+{
+ if (!yasl_public_surface(s, false) || !trigger_valid(s, e))
+ return -errno;
+ struct yasl_private_drag_image image = {0};
+ if (source)
+ image = (struct yasl_private_drag_image){.pixels = source->pixels,
+ .size = {source->width, source->height},
+ .hotspot = {source->hotspot_x, source->hotspot_y}};
+ errno = 0;
+ return yasl_public_status(
+ yasl_private_display_drag_start(display, s, source ? &image : NULL, NULL, NULL) ||
+ yasl_public_fail(errno));
+}
+int yasl_drag_cancel(void)
+{
+ if (!yasl_public_owner())
+ return -errno;
+ yasl_private_display_drag_cancel(display);
+ return 0;
+}
+int yasl_surface_drag_carry(yasl_surface *s, double x, double y)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ struct yasl_private_rect2i r;
+ if (!logical_rect(s, x, y, 0, 0, &r))
+ return -errno;
+ errno = 0;
+ return yasl_public_status(
+ yasl_private_surface_drag_carry(s, (struct yasl_private_vector2i){r.x, r.y}) ||
+ yasl_public_fail(errno));
+}
+int yasl_surface_place_under_pointer(yasl_surface *s, double x, double y)
+{
+ if (!yasl_public_surface(s, false))
+ return -errno;
+ struct yasl_private_rect2i r;
+ if (!logical_rect(s, x, y, 0, 0, &r))
+ return -errno;
+ errno = 0;
+ return yasl_public_status(
+ yasl_private_surface_place_under_pointer(s, (struct yasl_private_vector2i){r.x, r.y}) ||
+ yasl_public_fail(errno));
+}
+size_t yasl_drop_next_path(const char **cursor, char *out, size_t cap)
+{
+ return yasl_private_drop_next_path(cursor, out, cap);
+}
-yasl_surface *yasl_gl_surface_create(const char *title,int w,int h,unsigned flags,const int *attrs,yasl_surface *share)
-{
- if(!creation_valid(w,h,flags)||(flags&YASL_SURFACE_CPU)) { yasl_public_fail(EINVAL);return NULL; }
- struct yasl_private_gl_options parsed;if(!yasl_private_gl_parse_options(attrs,&parsed)){yasl_public_fail(errno);return NULL;}
- bool fresh=!display;if(!initialize())return NULL;
- if(share && (share->display!=display || share->presenter!=YASL_PRIVATE_PRESENTER_GL)) {yasl_public_fail(EINVAL);return NULL;}
- if(!display->ops->surface_create_gl) {if(fresh)yasl_shutdown();yasl_public_fail(ENOTSUP);return NULL;}
- struct yasl_private_window_spec spec={.title=title,.size={w,h},.flags=flags,.external_gpu=true};
- yasl_surface *s=display->ops->surface_create_gl(display,&spec,attrs,share);
- if(!s){int error=errno;if(fresh)yasl_shutdown();yasl_public_fail(error);return NULL;}
- if(!yasl_private_events_register(s)){int error=errno;display->ops->surface_destroy(s);if(fresh)yasl_shutdown();yasl_public_fail(error);return NULL;}
- s->presenter=YASL_PRIVATE_PRESENTER_GL;yasl_public_register_surface(s,flags,NULL);return s;
-}
-bool yasl_gl_make_current(yasl_surface *s)
-{
- if(!display){yasl_public_set_error(YASL_ERROR_NOT_INITIALIZED,0);return false;}
- if(s && (s->display!=display||s->presenter!=YASL_PRIVATE_PRESENTER_GL))return yasl_public_fail(EINVAL);
- if(!display->ops->gl_make_current)return yasl_public_fail(ENOTSUP);
- errno=0;return display->ops->gl_make_current(display,s)||yasl_public_fail(errno);
+yasl_surface *yasl_gl_surface_create(const struct yasl_surface_options *options,
+ const struct yasl_gl_options *gl)
+{
+ if (!options || options->type != YASL_SURFACE_NATIVE || (options->flags & ~14u)) {
+ yasl_public_fail(EINVAL);
+ return NULL;
+ }
+ const struct yasl_gl_options defaults = YASL_GL_OPTIONS_INIT;
+ if (!gl)
+ gl = &defaults;
+ const int attrs[] = {
+ YASL_PRIVATE_GL_MAJOR,
+ gl->major,
+ YASL_PRIVATE_GL_MINOR,
+ gl->minor,
+ YASL_PRIVATE_GL_PROFILE,
+ gl->profile,
+ YASL_PRIVATE_GL_API,
+ gl->api,
+ YASL_PRIVATE_GL_RED_BITS,
+ gl->red_bits,
+ YASL_PRIVATE_GL_GREEN_BITS,
+ gl->green_bits,
+ YASL_PRIVATE_GL_BLUE_BITS,
+ gl->blue_bits,
+ YASL_PRIVATE_GL_ALPHA_BITS,
+ gl->alpha_bits,
+ YASL_PRIVATE_GL_DEPTH_BITS,
+ gl->depth_bits,
+ YASL_PRIVATE_GL_STENCIL_BITS,
+ gl->stencil_bits,
+ YASL_PRIVATE_GL_SAMPLES,
+ gl->samples,
+ YASL_PRIVATE_GL_DEBUG,
+ gl->debug,
+ YASL_PRIVATE_GL_FORWARD_COMPAT,
+ gl->forward_compat,
+ YASL_PRIVATE_GL_SRGB,
+ gl->srgb,
+ YASL_PRIVATE_GL_END,
+ };
+ const char *title = options->title;
+ int w = options->width, h = options->height;
+ unsigned flags = options->flags;
+ yasl_surface *share = gl->share;
+ if (!creation_valid(w, h, flags)) {
+ return NULL;
+ }
+ struct yasl_private_gl_options parsed;
+ if (!yasl_private_gl_parse_options(attrs, &parsed)) {
+ yasl_public_fail(errno);
+ return NULL;
+ }
+ if (share && !display) {
+ yasl_public_fail(EINVAL);
+ return NULL;
+ }
+ bool fresh = !display;
+ if (!initialize())
+ return NULL;
+ if (share && (share->display != display || share->presenter != YASL_PRIVATE_PRESENTER_GL)) {
+ yasl_public_fail(EINVAL);
+ return NULL;
+ }
+ if (!display->ops->surface_create_gl) {
+ if (fresh)
+ yasl_shutdown();
+ yasl_public_fail(ENOTSUP);
+ return NULL;
+ }
+ struct yasl_private_window_spec spec = {
+ .title = title, .size = {w, h}, .flags = flags, .external_gpu = true};
+ errno = 0;
+ yasl_surface *s = display->ops->surface_create_gl(display, &spec, attrs, share);
+ if (!s) {
+ int error = errno;
+ if (fresh)
+ yasl_shutdown();
+ yasl_public_fail(error);
+ return NULL;
+ }
+ if (!yasl_private_events_register(s)) {
+ int error = errno;
+ display->ops->surface_destroy(s);
+ if (fresh)
+ yasl_shutdown();
+ yasl_public_fail(error);
+ return NULL;
+ }
+ s->presenter = YASL_PRIVATE_PRESENTER_GL;
+ yasl_public_register_surface(s, flags, NULL);
+ s->userdata = options->userdata;
+ return s;
+}
+int yasl_gl_make_current(yasl_surface *s)
+{
+ if (!display) {
+ return yasl_public_error(ENODEV);
+ }
+ if (s && (s->display != display || s->presenter != YASL_PRIVATE_PRESENTER_GL))
+ return yasl_public_error(EINVAL);
+ if (display->offline)
+ return yasl_public_error(ENOTCONN);
+ if (!display->ops->gl_make_current)
+ return yasl_public_error(ENOTSUP);
+ errno = 0;
+ return yasl_public_status(display->ops->gl_make_current(display, s) || yasl_public_fail(errno));
}
yasl_gl_proc yasl_gl_get_proc_address(const char *name)
{
- if(!display||!name||!display->ops->gl_get_proc_address){yasl_public_fail(ENOTSUP);return NULL;}
- yasl_gl_proc p=display->ops->gl_get_proc_address(display,name);if(!p)yasl_public_fail(ENOENT);return p;
+ if (!display) {
+ yasl_public_fail(ENODEV);
+ return NULL;
+ }
+ if (!name || !*name) {
+ yasl_public_fail(EINVAL);
+ return NULL;
+ }
+ if (display->offline) {
+ yasl_public_fail(ENOTCONN);
+ return NULL;
+ }
+ if (!display->ops->gl_get_proc_address) {
+ yasl_public_fail(ENOTSUP);
+ return NULL;
+ }
+ yasl_gl_proc p = display->ops->gl_get_proc_address(display, name);
+ if (!p)
+ yasl_public_fail(ENOENT);
+ return p;
}
-bool yasl_gl_swap_interval(yasl_surface *s,int interval)
+int yasl_gl_set_swap_interval(yasl_surface *s, int interval)
{
- if(!s||s->presenter!=YASL_PRIVATE_PRESENTER_GL||!s->display->ops->gl_swap_interval)return yasl_public_fail(EINVAL);
- errno=0;return s->display->ops->gl_swap_interval(s->display,s,interval)||yasl_public_fail(errno);
+ if (!display)
+ return yasl_public_error(ENODEV);
+ if (display->offline)
+ return yasl_public_error(ENOTCONN);
+ if (!s || s->display != display || s->presenter != YASL_PRIVATE_PRESENTER_GL ||
+ !s->display->ops->gl_swap_interval)
+ return yasl_public_error(EINVAL);
+ errno = 0;
+ return yasl_public_status(s->display->ops->gl_swap_interval(s->display, s, interval) ||
+ yasl_public_fail(errno));
}
-bool yasl_gl_swap_buffers(yasl_surface *s)
+int yasl_gl_present(yasl_surface *s)
{
- if(!s||s->presenter!=YASL_PRIVATE_PRESENTER_GL||!s->display->ops->gl_swap_buffers)return yasl_public_fail(EINVAL);
- errno=0;return s->display->ops->gl_swap_buffers(s->display,s)||yasl_public_fail(errno);
+ if (!display)
+ return yasl_public_error(ENODEV);
+ if (display->offline)
+ return yasl_public_error(ENOTCONN);
+ if (!s || s->display != display || s->presenter != YASL_PRIVATE_PRESENTER_GL ||
+ !s->display->ops->gl_swap_buffers)
+ return yasl_public_error(EINVAL);
+ errno = 0;
+ return yasl_public_status(s->display->ops->gl_swap_buffers(s->display, s) ||
+ yasl_public_fail(errno));
}
diff --git a/src/common/vulkan.c b/src/common/vulkan.c
index 62bc3a0..77a4dd4 100644
--- a/src/common/vulkan.c
+++ b/src/common/vulkan.c
@@ -20,69 +20,202 @@
#endif
static PFN_vkGetInstanceProcAddr get_proc;
static bool portability;
-void yasl_private_vulkan_reset(void) { get_proc=NULL; portability=false; }
-bool yasl_vulkan_init(PFN_vkGetInstanceProcAddr loader)
+void yasl_private_vulkan_reset(void)
{
- if(!loader)return yasl_public_fail(EINVAL);
- get_proc=loader;portability=false;
- PFN_vkEnumerateInstanceExtensionProperties enumerate=(PFN_vkEnumerateInstanceExtensionProperties)get_proc(VK_NULL_HANDLE,"vkEnumerateInstanceExtensionProperties");
- if(!enumerate)return yasl_public_fail(ENOTSUP);
- uint32_t count=0;VkResult r=enumerate(NULL,&count,NULL);
- if(r!=VK_SUCCESS)return yasl_public_fail(EIO);
- VkExtensionProperties *ext=count?calloc(count,sizeof(*ext)):NULL;
- if(count&&!ext)return yasl_public_fail(ENOMEM);
- r=enumerate(NULL,&count,ext);
- if(r==VK_SUCCESS)for(uint32_t i=0;i<count;i++)if(!strcmp(ext[i].extensionName,"VK_KHR_portability_enumeration"))portability=true;
- free(ext);return r==VK_SUCCESS||yasl_public_fail(EIO);
+ get_proc = NULL;
+ portability = false;
}
-const char *const *yasl_vulkan_instance_extensions(const yasl_surface *s,uint32_t *count)
+struct yasl_vulkan_binding {
+ VkInstance instance;
+ VkSurfaceKHR surface;
+ VkAllocationCallbacks allocator;
+ bool has_allocator;
+ PFN_vkDestroySurfaceKHR destroy;
+};
+int yasl_vulkan_init(PFN_vkGetInstanceProcAddr loader)
{
- static const char *wayland[]={"VK_KHR_surface","VK_KHR_wayland_surface"};
- static const char *x11[]={"VK_KHR_surface","VK_KHR_xlib_surface"};
- static const char *metal[]={"VK_KHR_surface","VK_EXT_metal_surface","VK_KHR_portability_enumeration"};
- if(count)*count=0;
- if(!s||!count||s->presenter==YASL_PRIVATE_PRESENTER_CPU||s->presenter==YASL_PRIVATE_PRESENTER_GL){yasl_public_fail(EINVAL);return NULL;}
- switch(s->display->ops->type) {
- case YASL_PRIVATE_BACKEND_WAYLAND:*count=2;return wayland;
- case YASL_PRIVATE_BACKEND_X11:*count=2;return x11;
- case YASL_PRIVATE_BACKEND_COCOA:*count=portability?3:2;return metal;
- default:yasl_public_fail(ENOTSUP);return NULL;
+ struct yasl_private_display *d = yasl_public_display();
+ if (d && !yasl_public_owner())
+ return -errno;
+ if (!loader)
+ return yasl_public_error(EINVAL);
+ if (d)
+ for (yasl_surface *s = d->surfaces; s; s = s->event_next)
+ if (s->vulkan)
+ return yasl_public_error(EBUSY);
+ PFN_vkEnumerateInstanceExtensionProperties enumerate =
+ (PFN_vkEnumerateInstanceExtensionProperties)loader(
+ VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
+ if (!enumerate)
+ return yasl_public_error(ENOTSUP);
+ uint32_t count = 0;
+ VkResult result = enumerate(NULL, &count, NULL);
+ if (result != VK_SUCCESS)
+ return yasl_public_error(EIO);
+ VkExtensionProperties *ext = count ? calloc(count, sizeof(*ext)) : NULL;
+ if (count && !ext)
+ return yasl_public_error(ENOMEM);
+ bool next_portability = false;
+ if (count)
+ result = enumerate(NULL, &count, ext);
+ if (result == VK_SUCCESS)
+ for (uint32_t i = 0; i < count; i++)
+ if (!strcmp(ext[i].extensionName, "VK_KHR_portability_enumeration"))
+ next_portability = true;
+ free(ext);
+ if (result != VK_SUCCESS)
+ return yasl_public_error(EIO);
+ get_proc = loader;
+ portability = next_portability;
+ return 0;
+}
+int yasl_vulkan_get_requirements(const yasl_surface *s, struct yasl_vulkan_requirements *out)
+{
+ static const char *const wayland[] = {"VK_KHR_surface", "VK_KHR_wayland_surface"};
+ static const char *const x11[] = {"VK_KHR_surface", "VK_KHR_xlib_surface"};
+ static const char *const metal[] = {"VK_KHR_surface", "VK_EXT_metal_surface",
+ "VK_KHR_portability_enumeration"};
+ if (!out)
+ return yasl_public_error(EINVAL);
+ *out = (struct yasl_vulkan_requirements){0};
+ if (!yasl_public_surface((yasl_surface *)s, false))
+ return -errno;
+ if (!get_proc)
+ return yasl_public_error(ENODEV);
+ if (s->presenter != YASL_PRIVATE_PRESENTER_NONE &&
+ s->presenter != YASL_PRIVATE_PRESENTER_VULKAN)
+ return yasl_public_error(EBUSY);
+ switch (s->display->ops->type) {
+ case YASL_PRIVATE_BACKEND_WAYLAND:
+ out->extensions = wayland;
+ out->extension_count = 2;
+ break;
+ case YASL_PRIVATE_BACKEND_X11:
+ out->extensions = x11;
+ out->extension_count = 2;
+ break;
+ case YASL_PRIVATE_BACKEND_COCOA:
+ out->extensions = metal;
+ out->extension_count = portability ? 3 : 2;
+ out->flags = portability ? VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR : 0;
+ break;
+ default:
+ return yasl_public_error(ENOTSUP);
}
+ return 0;
+}
+int yasl_vulkan_destroy_surface(yasl_surface *s)
+{
+ if (!yasl_public_surface(s, true))
+ return -errno;
+ struct yasl_vulkan_binding *binding = s->vulkan;
+ if (!binding)
+ return 0;
+ binding->destroy(binding->instance, binding->surface,
+ binding->has_allocator ? &binding->allocator : NULL);
+ free(binding);
+ s->vulkan = NULL;
+ s->presenter = YASL_PRIVATE_PRESENTER_NONE;
+ return 0;
}
-VkInstanceCreateFlags yasl_vulkan_instance_flags(const yasl_surface *s)
-{ return s&&s->display->ops->type==YASL_PRIVATE_BACKEND_COCOA&&portability?VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR:0; }
-VkResult yasl_vulkan_create_surface(yasl_surface *s,VkInstance instance,const VkAllocationCallbacks *allocator,VkSurfaceKHR *out)
+VkResult yasl_vulkan_create_surface(yasl_surface *s, VkInstance instance,
+ const VkAllocationCallbacks *allocator, VkSurfaceKHR *out)
{
- if(out)*out=VK_NULL_HANDLE;
- if(!yasl_public_surface(s,false)||!instance||!out||!get_proc){yasl_public_fail(EINVAL);return VK_ERROR_INITIALIZATION_FAILED;}
- if(s->display->offline){yasl_public_fail(ENOTCONN);return VK_ERROR_SURFACE_LOST_KHR;}
- if(s->presenter!=YASL_PRIVATE_PRESENTER_NONE){yasl_public_fail(EBUSY);return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;}
- VkResult result=VK_ERROR_EXTENSION_NOT_PRESENT;
- switch(s->display->ops->type) {
+ if (out)
+ *out = VK_NULL_HANDLE;
+ if (!yasl_public_surface(s, true))
+ return VK_ERROR_INITIALIZATION_FAILED;
+ if (!instance || !out) {
+ yasl_public_fail(EINVAL);
+ return VK_ERROR_INITIALIZATION_FAILED;
+ }
+ if (!get_proc) {
+ yasl_public_fail(ENODEV);
+ return VK_ERROR_INITIALIZATION_FAILED;
+ }
+ if (s->display->offline) {
+ yasl_public_fail(ENOTCONN);
+ return VK_ERROR_SURFACE_LOST_KHR;
+ }
+ if (s->vulkan || s->presenter != YASL_PRIVATE_PRESENTER_NONE) {
+ yasl_public_fail(EBUSY);
+ return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
+ }
+ PFN_vkDestroySurfaceKHR destroy =
+ (PFN_vkDestroySurfaceKHR)get_proc(instance, "vkDestroySurfaceKHR");
+ if (!destroy) {
+ yasl_public_fail(ENOTSUP);
+ return VK_ERROR_EXTENSION_NOT_PRESENT;
+ }
+ struct yasl_vulkan_binding *binding = calloc(1, sizeof(*binding));
+ if (!binding) {
+ yasl_public_fail(ENOMEM);
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
+ }
+ VkResult result = VK_ERROR_EXTENSION_NOT_PRESENT;
+ switch (s->display->ops->type) {
#if YASL_HAS_WAYLAND
case YASL_PRIVATE_BACKEND_WAYLAND: {
- PFN_vkCreateWaylandSurfaceKHR create=(PFN_vkCreateWaylandSurfaceKHR)get_proc(instance,"vkCreateWaylandSurfaceKHR");
- if(create) { VkWaylandSurfaceCreateInfoKHR info={.sType=VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
- .display=yasl_wayland_display(s),.surface=yasl_wayland_surface(s)};result=create(instance,&info,allocator,out); }break;
+ struct yasl_wayland_handles native;
+ if (yasl_wayland_get_native(s, &native) < 0)
+ break;
+ PFN_vkCreateWaylandSurfaceKHR create =
+ (PFN_vkCreateWaylandSurfaceKHR)get_proc(instance, "vkCreateWaylandSurfaceKHR");
+ if (create) {
+ VkWaylandSurfaceCreateInfoKHR info = {
+ .sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
+ .display = native.display,
+ .surface = native.surface};
+ result = create(instance, &info, allocator, out);
+ }
+ break;
}
#endif
#if YASL_HAS_X11
case YASL_PRIVATE_BACKEND_X11: {
- PFN_vkCreateXlibSurfaceKHR create=(PFN_vkCreateXlibSurfaceKHR)get_proc(instance,"vkCreateXlibSurfaceKHR");
- if(create) { VkXlibSurfaceCreateInfoKHR info={.sType=VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
- .dpy=yasl_x11_display(s),.window=(Window)yasl_x11_window(s)};result=create(instance,&info,allocator,out); }break;
+ struct yasl_x11_handles native;
+ if (yasl_x11_get_native(s, &native) < 0)
+ break;
+ PFN_vkCreateXlibSurfaceKHR create =
+ (PFN_vkCreateXlibSurfaceKHR)get_proc(instance, "vkCreateXlibSurfaceKHR");
+ if (create) {
+ VkXlibSurfaceCreateInfoKHR info = {.sType =
+ VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
+ .dpy = native.display,
+ .window = (Window)native.window};
+ result = create(instance, &info, allocator, out);
+ }
+ break;
}
#endif
#if YASL_HAS_COCOA
case YASL_PRIVATE_BACKEND_COCOA: {
- PFN_vkCreateMetalSurfaceEXT create=(PFN_vkCreateMetalSurfaceEXT)get_proc(instance,"vkCreateMetalSurfaceEXT");
- if(create&&s->display->ops->surface_get_metal_layer) { VkMetalSurfaceCreateInfoEXT info={.sType=VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT,
- .pLayer=s->display->ops->surface_get_metal_layer(s)};result=create(instance,&info,allocator,out); }break;
+ PFN_vkCreateMetalSurfaceEXT create =
+ (PFN_vkCreateMetalSurfaceEXT)get_proc(instance, "vkCreateMetalSurfaceEXT");
+ if (create && s->display->ops->surface_get_metal_layer) {
+ VkMetalSurfaceCreateInfoEXT info = {
+ .sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT,
+ .pLayer = s->display->ops->surface_get_metal_layer(s)};
+ result = create(instance, &info, allocator, out);
+ }
+ break;
}
#endif
- default:break;
+ default:
+ break;
+ }
+ if (result == VK_SUCCESS) {
+ binding->instance = instance;
+ binding->surface = *out;
+ binding->destroy = destroy;
+ binding->has_allocator = allocator != NULL;
+ if (allocator)
+ binding->allocator = *allocator;
+ s->vulkan = binding;
+ s->presenter = YASL_PRIVATE_PRESENTER_VULKAN;
+ } else {
+ free(binding);
+ yasl_public_fail(result == VK_ERROR_EXTENSION_NOT_PRESENT ? ENOTSUP : EIO);
}
- if(result==VK_SUCCESS)s->presenter=YASL_PRIVATE_PRESENTER_VULKAN;
- else yasl_public_fail(result==VK_ERROR_EXTENSION_NOT_PRESENT?ENOTSUP:EIO);
return result;
}
diff --git a/src/platform/wayland/backend.c b/src/platform/wayland/backend.c
index 7ac1c65..6b7758f 100644
--- a/src/platform/wayland/backend.c
+++ b/src/platform/wayland/backend.c
@@ -434,6 +434,11 @@ static void *impl_surface_get_native_handle(struct yasl_surface *s)
const struct yasl_private_backend_ops yasl_private_wayland_backend = {
#if YASL_HAS_EGL
+ .surface_create_gl = impl_surface_create_gl,
+ .gl_make_current = impl_gl_make_current,
+ .gl_get_proc_address = impl_gl_get_proc_address,
+ .gl_swap_interval = impl_gl_swap_interval,
+ .gl_swap_buffers = impl_gl_swap_buffers,
#endif
.surface_update_text_input = impl_surface_update_text_input,
.type = YASL_PRIVATE_BACKEND_WAYLAND,