aboutsummaryrefslogtreecommitdiff
path: root/src/waffle/wayland/wayland_platform.c
blob: 05dddb866de2053e5e63a05259a9566c64efb79a (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
// Copyright 2012 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// @addtogroup wayland_platform
/// @{

/// @file

#include "wayland_platform.h"

#define _POSIX_C_SOURCE 200112 // glib feature macro for unsetenv()

#include <dlfcn.h>
#include <stdlib.h>

#include <waffle/native.h>
#include <waffle/waffle_enum.h>
#include <waffle/core/wcore_error.h>
#include <waffle/linux/linux_platform.h>

#include "wayland_config.h"
#include "wayland_context.h"
#include "wayland_display.h"
#include "wayland_dl.h"
#include "wayland_gl_misc.h"
#include "wayland_priv_egl.h"
#include "wayland_priv_types.h"
#include "wayland_window.h"

static const struct native_dispatch wayland_dispatch = {
    .display_connect = wayland_display_connect,
    .display_disconnect = wayland_display_disconnect,
    .display_supports_context_api = wayland_display_supports_context_api,
    .config_choose = wayland_config_choose,
    .config_destroy = wayland_config_destroy,
    .context_create = wayland_context_create,
    .context_destroy = wayland_context_destroy,
    .dl_can_open = wayland_dl_can_open,
    .dl_sym = wayland_dl_sym,
    .window_create = wayland_window_create,
    .window_destroy = wayland_window_destroy,
    .window_swap_buffers = wayland_window_swap_buffers,
    .make_current = wayland_make_current,
    .get_proc_address = wayland_get_proc_address,
};

union native_platform*
wayland_platform_create(
        int gl_api,
        const struct native_dispatch **dispatch)
{
    union native_platform *self;
    NATIVE_ALLOC(self, wl);
    if (!self) {
        wcore_error(WAFFLE_OUT_OF_MEMORY);
        return NULL;
    }

    switch (gl_api) {
        case WAFFLE_OPENGL:
        case WAFFLE_OPENGL_ES1:
        case WAFFLE_OPENGL_ES2:
            break;
        default:
            wcore_error_internal("gl_api has bad value 0x%x", gl_api);
            goto error;
    }

    self->wl->gl_api = gl_api;
    self->wl->linux_ = linux_platform_create();
    if (!self->wl->linux_)
        goto error;

    setenv("EGL_PLATFORM", "wayland", true);

    *dispatch = &wayland_dispatch;
    return self;

error:
    wayland_platform_destroy(self);
    return NULL;
}

bool
wayland_platform_destroy(union native_platform *self)
{
    bool ok = true;

    if (!self)
        return true;

    unsetenv("EGL_PLATFORM");

    if (self->wl->linux_)
        ok &= linux_platform_destroy(self->wl->linux_);

    free(self);
    return ok;
}

/// @}