summaryrefslogtreecommitdiff
path: root/src/canvas/wayland/wayland.cpp
blob: 9e2a64254e0b4949bf1ace2276cb0b7faf728dfa (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Display images inside a terminal
// Copyright (C) 2023  JustKidding
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

#include "wayland.hpp"
#include "application.hpp"
#include "image.hpp"
#include "os.hpp"
#include "util.hpp"

#ifdef ENABLE_OPENGL
#  include "window/waylandegl.hpp"
#  include <EGL/eglext.h>
#endif
#include "window/waylandshm.hpp"

constexpr struct wl_registry_listener registry_listener = {.global = WaylandCanvas::registry_handle_global,
                                                           .global_remove = [](auto...) { /*unused*/ }};

constexpr struct xdg_wm_base_listener xdg_wm_base_listener = {.ping = WaylandCanvas::xdg_wm_base_ping};

constexpr struct wl_output_listener wl_output_listener = {.geometry = [](auto...) { /*unused*/ },
                                                          .mode = [](auto...) { /*unused*/ },
                                                          .done = WaylandCanvas::output_done,
                                                          .scale = WaylandCanvas::output_scale,
                                                          .name = WaylandCanvas::output_name,
                                                          .description = [](auto...) { /*unused*/ }};

void WaylandCanvas::registry_handle_global(void *data, wl_registry *registry, uint32_t name, const char *interface,
                                           [[maybe_unused]] uint32_t version)
{
    const std::string_view interface_str{interface};
#ifdef WSL
    const uint32_t compositor_ver = 4;
    const uint32_t shm_ver = 1;
    const uint32_t xdg_base_ver = 1;
    const uint32_t output_ver = 3;
#else
    const uint32_t compositor_ver = 5;
    const uint32_t shm_ver = 1;
    const uint32_t xdg_base_ver = 2;
    const uint32_t output_ver = 4;
#endif

    auto *canvas = static_cast<WaylandCanvas *>(data);
    if (interface_str == wl_compositor_interface.name) {
        canvas->compositor = static_cast<struct wl_compositor *>(
            wl_registry_bind(registry, name, &wl_compositor_interface, compositor_ver));
    } else if (interface_str == wl_shm_interface.name) {
        canvas->wl_shm = static_cast<struct wl_shm *>(wl_registry_bind(registry, name, &wl_shm_interface, shm_ver));
    } else if (interface_str == xdg_wm_base_interface.name) {
        canvas->xdg_base =
            static_cast<struct xdg_wm_base *>(wl_registry_bind(registry, name, &xdg_wm_base_interface, xdg_base_ver));
        xdg_wm_base_add_listener(canvas->xdg_base, &xdg_wm_base_listener, canvas);
    } else if (interface_str == wl_output_interface.name) {
        auto *output =
            static_cast<struct wl_output *>(wl_registry_bind(registry, name, &wl_output_interface, output_ver));
        wl_output_add_listener(output, &wl_output_listener, canvas);
    }
}

void WaylandCanvas::xdg_wm_base_ping([[maybe_unused]] void *data, struct xdg_wm_base *xdg_wm_base, uint32_t serial)
{
    xdg_wm_base_pong(xdg_wm_base, serial);
}

void WaylandCanvas::output_name(void *data, [[maybe_unused]] struct wl_output *output, const char *name)
{
    auto *canvas = static_cast<WaylandCanvas *>(data);
    canvas->output_pair.first = name;
}

void WaylandCanvas::output_scale(void *data, [[maybe_unused]] struct wl_output *output, int32_t scale)
{
    auto *canvas = static_cast<WaylandCanvas *>(data);
    canvas->output_pair.second = scale;
}

void WaylandCanvas::output_done(void *data, [[maybe_unused]] struct wl_output *output)
{
    auto *canvas = static_cast<WaylandCanvas *>(data);
    const auto active_output = canvas->config->get_focused_output_name();
    if (active_output == canvas->output_pair.first) {
        canvas->flags->scale_factor = canvas->output_pair.second;
        canvas->flags->needs_scaling = canvas->output_pair.second > 1;
    }
    canvas->output_info.insert(canvas->output_pair);
}

WaylandCanvas::WaylandCanvas()
    : display(wl_display_connect(nullptr))
{
    if (display == nullptr) {
        throw std::runtime_error("Failed to connect to wayland display.");
    }
    logger = spdlog::get("wayland");
    flags = Flags::instance();
    config = WaylandConfig::get();
    registry = wl_display_get_registry(display);
    wl_registry_add_listener(registry, &registry_listener, this);
    wl_display_roundtrip(display);
    event_handler = std::thread(&WaylandCanvas::handle_events, this);

#ifdef ENABLE_OPENGL
    if (flags->use_opengl) {
        try {
            egl = std::make_unique<EGLUtil<struct wl_display, struct wl_egl_window>>(EGL_PLATFORM_WAYLAND_EXT, display);
        } catch (const std::runtime_error &) {
            egl_available = false;
        }
    } else {
        egl_available = false;
    }
#endif

    logger->info("Canvas created");
}

void WaylandCanvas::show()
{
    for (const auto &[key, value] : windows) {
        value->show();
    }
}

void WaylandCanvas::hide()
{
    for (const auto &[key, value] : windows) {
        value->hide();
    }
}

WaylandCanvas::~WaylandCanvas()
{
    windows.clear();
    if (event_handler.joinable()) {
        event_handler.join();
    }

#ifdef ENABLE_OPENGL
    egl.reset();
#endif

    if (wl_shm != nullptr) {
        wl_shm_destroy(wl_shm);
    }
    if (compositor != nullptr) {
        wl_compositor_destroy(compositor);
    }
    if (registry != nullptr) {
        wl_registry_destroy(registry);
    }
    wl_display_disconnect(display);
}

void WaylandCanvas::add_image(const std::string &identifier, std::unique_ptr<Image> new_image)
{
    std::shared_ptr<WaylandWindow> window;
#ifdef ENABLE_OPENGL
    if (egl_available) {
        try {
            window = std::make_shared<WaylandEglWindow>(compositor, xdg_base, egl.get(), std::move(new_image),
                                                        config.get(), &xdg_agg);
        } catch (const std::runtime_error &err) {
            return;
        }
    }
#endif
    if (window == nullptr) {
        window = std::make_shared<WaylandShmWindow>(this, std::move(new_image), &xdg_agg, config.get());
    }

    window->finish_init();
    windows.insert_or_assign(identifier, std::move(window));
}

void WaylandCanvas::handle_events()
{
    const auto wl_fd = wl_display_get_fd(display);
    bool in_event = false;

    while (!Application::stop_flag) {
        // prepare to read wayland events
        while (wl_display_prepare_read(display) != 0) {
            wl_display_dispatch_pending(display);
        }
        wl_display_flush(display);

        try {
            constexpr int waitms = 100;
            in_event = os::wait_for_data_on_fd(wl_fd, waitms);
        } catch (const std::system_error &err) {
            Application::stop_flag = true;
            break;
        }

        if (in_event) {
            wl_display_read_events(display);
            wl_display_dispatch_pending(display);
        } else {
            wl_display_cancel_read(display);
        }
    }
}

void WaylandCanvas::remove_image(const std::string &identifier)
{
    windows.erase(identifier);
    wl_display_flush(display);
}