// =====================================================================================================================
// fennec, a free and open source game engine
// Copyright © 2025 Medusa Slockbower
//
// 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 .
// =====================================================================================================================
#include
#include
#include
#include
#include
#include
#include
namespace fennec
{
wayland_server::wayland_server(fennec::platform* p)
: display_server_base(p)
, display(nullptr), registry(nullptr), compositor(nullptr), seat(nullptr), fifo(false) {
// load shared lib
if (libwayland::load_symbols(platform)) {
return;
}
}
wayland_server::~wayland_server() {
// ensure we did disconnect
wayland_server::disconnect();
// unload shared lib
libwayland::unload_symbols(platform);
}
void wayland_server::connect() {
static constexpr wl_registry_listener listener = {
listen_global, listen_global_remove
};
if (display != nullptr) {
return;
}
// connect to wayland
display = wl_display_connect(nullptr);
if (display == nullptr) {
return;
}
// get the registry
registry = wl_display_get_registry(display);
if (registry == nullptr) {
assert(registry, "Failed to acquire registry from Wayland");
disconnect();
return;
}
// Aquire interfaces
wl_registry_add_listener(registry, &listener, this);
wl_display_roundtrip(display);
// Check for fifo v1, wayland's builtin fifo only allows one frame to be in flight at a time
if (not fifo) {
assert(fifo, "Compositor does not support fifo-v1 protocol, falling back to other display protocols.");
disconnect();
return;
}
auto contexts = ctx_registry::get_type_list();
while (not contexts.empty()) {
const auto& ctx = contexts.front();
gfx_context = ctx.ctor(this);
if (gfx_context->is_valid()) {
break;
} else {
delete gfx_context;
gfx_context = nullptr;
contexts.pop();
}
}
assertf(gfx_context != nullptr, "Failed to create graphics context.");
}
void wayland_server::disconnect() {
// early escape
if (display == nullptr) {
return;
}
// delete the gfx context
delete gfx_context;
// check compositor
if (compositor) {
wl_compositor_destroy(compositor);
}
// check registry
if (registry) {
wl_registry_destroy(registry);
}
// disconnect from wayland
wl_display_flush(display);
wl_display_disconnect(display);
// clear vars
gfx_context = nullptr;
display = nullptr;
registry = nullptr;
compositor = nullptr;
seat = nullptr;
fifo = false;
}
bool wayland_server::connected() const {
return display != nullptr;
}
void wayland_server::dispatch() {
wl_display_dispatch_pending(display);
}
window* wayland_server::create_window(const window::config& conf) {
size_t id = windows.insert(new wayland_window(this, windows.next_id(), conf));
return windows[id];
}
void wayland_server::listen_global(void* data, wl_registry* reg, uint32_t id, const char* name, uint32_t version) {
static constexpr wl_seat_listener seat_listener = {
listen_seat_capabilities, listen_seat_name
};
static constexpr xdg_wm_base_listener xdg_listener = {
listen_xdg_ping
};
wayland_server* server = static_cast(data);
const cstring interface = cstring(name, strlen(name) + 1);
if (interface == "wl_compositor") {
server->compositor = static_cast(wl_registry_bind(reg, id, &wl_compositor_interface, version));
return;
}
if (interface == "xdg_wm_base") {
server->xdg = static_cast(wl_registry_bind(reg, id, &xdg_wm_base_interface, version));
xdg_wm_base_add_listener(server->xdg, &xdg_listener, server);
return;
}
if (interface == "wl_seat") {
server->seat = static_cast(wl_registry_bind(reg, id, &wl_seat_interface, version));
wl_seat_add_listener(server->seat, &seat_listener, server);
return;
}
if (interface == "wp_fifo_manager_v1") {
server->fifo = true;
return;
}
}
void wayland_server::listen_global_remove(void*, wl_registry*, uint32_t) {
}
void wayland_server::listen_seat_capabilities(void*, wl_seat*, uint32_t) {
}
void wayland_server::listen_seat_name(void*, wl_seat*, const char*) {}
void wayland_server::listen_xdg_ping(void*, xdg_wm_base* xdg, uint32_t serial) {
xdg_wm_base_pong(xdg, serial);
}
}