summaryrefslogtreecommitdiff
path: root/src/common/native.c
blob: 1b32094e7aa6cdf17d6a8a12696dfbe9d0478d2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "backend.h"

#include <errno.h>

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;
}

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;
}

struct yasl_surface *yasl_private_surface_create_native(struct yasl_private_display *d,
                                                        const struct yasl_private_window_spec *spec,
                                                        void *graphics_device)
{
    if (!graphics_device) {
        errno = EINVAL;
        return NULL;
    }
    return yasl_private_create_toplevel(d, spec, graphics_device);
}

bool yasl_private_surface_get_native_frame(struct yasl_surface *s,
                                           const struct yasl_private_framebuffer *target,
                                           struct yasl_private_native_frame *out)
{
    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);
}

#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"
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"
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"
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
int yasl_metal_get_layer(yasl_surface *s, CAMetalLayer **out)
{
    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;
}