- Setup wayland display and window. Window surface is created and appears in hotbar. Window is not visible.
This commit is contained in:
185
source/platform/linux/wayland/server.cpp
Normal file
185
source/platform/linux/wayland/server.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
// =====================================================================================================================
|
||||
// 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 <https://www.gnu.org/licenses/>.
|
||||
// =====================================================================================================================
|
||||
|
||||
///
|
||||
/// \file wayland_server.h
|
||||
/// \brief
|
||||
///
|
||||
///
|
||||
/// \details
|
||||
/// \author Medusa Slockbower
|
||||
///
|
||||
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
|
||||
///
|
||||
///
|
||||
|
||||
#include <fennec/core/logger.h>
|
||||
#include <fennec/platform/linux/wayland/server.h>
|
||||
#include <fennec/platform/linux/wayland/window.h>
|
||||
#include <fennec/platform/linux/wayland/lib/loader.h>
|
||||
#include <fennec/platform/linux/wayland/lib/wayland.h>
|
||||
#include <fennec/platform/linux/wayland/lib/headers/xdg-shell-client-protocols.h>
|
||||
|
||||
namespace fennec
|
||||
{
|
||||
|
||||
wayland_server::wayland_server(fennec::platform* p)
|
||||
: display_server(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;
|
||||
}
|
||||
}
|
||||
|
||||
void wayland_server::disconnect() {
|
||||
|
||||
// early escape
|
||||
if (display == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 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
|
||||
display = nullptr;
|
||||
registry = nullptr;
|
||||
compositor = nullptr;
|
||||
seat = nullptr;
|
||||
fifo = false;
|
||||
}
|
||||
|
||||
bool wayland_server::connected() const {
|
||||
return display != nullptr;
|
||||
}
|
||||
|
||||
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<wayland_server*>(data);
|
||||
const cstring interface = cstring(name, strlen(name) + 1);
|
||||
|
||||
if (interface == "wl_compositor") {
|
||||
server->compositor = static_cast<wl_compositor*>(wl_registry_bind(reg, id, &wl_compositor_interface, version));
|
||||
return;
|
||||
}
|
||||
|
||||
if (interface == "xdg_wm_base") {
|
||||
server->xdg = static_cast<xdg_wm_base*>(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_seat*>(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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user