voxelcore/src/window/wayland/WaylandInit.cpp
alexei-zebra b5416d1219
Fix the issues found while reviewing the Wayland frame work
A review of the branch turned up a number of defects, gaps and leftovers;
this commit addresses all of them.

Defects:
- the shadow uniform array held seven entries while eight locations were
  written, so the last one landed outside the object;
- CursorShape::LAST stayed in the middle of the enum after the new corner
  shapes were appended, which made the GLFW cursor table read out of
  bounds; the extra shapes are now mapped onto the ones GLFW knows, and
  the table is only filled for those;
- GlStateGuard asked glIsEnabled for an attribute pname, so it always got
  GL_INVALID_ENUM and never restored the vertex attribute arrays;
- the early returns in wayland_window_initialize left the global window
  and input pointers dangling when falling back to GLFW;
- the icon textures were listed nowhere, so none of them ever loaded;
- a shader failure disabled the shadow after the surface had already been
  sized for it, and VOXEL_DECORATIONS outlived the switch cleanup.

Behavior:
- the keyboard now releases every pressed key when it loses focus, and a
  repeat rate of zero means no repeat;
- the fourth UTF-8 lead byte is decoded with the right mask, control
  characters no longer reach text fields, and a vanished clipboard offer
  clears the cached text;
- compositor side decorations disable ours again instead of leaving two
  titles, the window destructor releases the rest of the state, and the
  content inset is computed before the GUI acts so the HUD and the
  container never disagree for a frame; the HUD clamps it;
- the window controls fall back to minimize, maximize and close, and the
  icons are drawn from textures only.

Cleanups: one monotonic timer instead of three, one vertex shader shared
by both renderers with a link check, one button layout parser, the inset
rule moved into Screen, the scissor stack moved into the base Window
(about 110 duplicated lines), named shadow and evdev constants, trimmed
includes, a README note about the Wayland dependencies, and the long
lines introduced by this branch wrapped.
2026-10-10 12:23:42 +03:00

287 lines
10 KiB
C++

#include "window/wayland/WaylandWindow.hpp"
#include "window/wayland/WaylandInput.hpp"
#include "window/wayland/WaylandListeners.hpp"
#include "settings.hpp"
#include "graphics/core/Texture.hpp"
#include <EGL/eglext.h>
std::tuple<std::unique_ptr<Window>, std::unique_ptr<Input>>
wayland_window_initialize(DisplaySettings* settings, std::string title) {
state.display = wl_display_connect(nullptr);
if (state.display == nullptr) {
waylandLogger.error() << "could not connect to the Wayland display";
window = nullptr;
input = nullptr;
if (state.display != nullptr) {
wl_display_disconnect(state.display);
state.display = nullptr;
}
return {nullptr, nullptr};
}
state.registry = wl_display_get_registry(state.display);
static const wl_registry_listener registryListener = [] {
wl_registry_listener value {};
value.global = registry_global;
value.global_remove = registry_global_remove;
return value;
}();
wl_registry_add_listener(state.registry, &registryListener, nullptr);
wl_display_roundtrip(state.display);
if (state.compositor == nullptr || state.wmBase == nullptr) {
waylandLogger.error() << "the compositor does not provide xdg-shell";
wl_display_disconnect(state.display);
state.display = nullptr;
window = nullptr;
input = nullptr;
if (state.display != nullptr) {
wl_display_disconnect(state.display);
state.display = nullptr;
}
return {nullptr, nullptr};
}
state.surface = wl_compositor_create_surface(state.compositor);
auto inputPtr = std::make_unique<WaylandInput>();
input = inputPtr.get();
auto windowPtr = std::make_unique<WaylandWindow>(
*inputPtr, settings, settings->width.get(), settings->height.get()
);
window = windowPtr.get();
if (state.dataDeviceManager && state.seat) {
state.dataDevice = wl_data_device_manager_get_data_device(
state.dataDeviceManager, state.seat
);
static const wl_data_device_listener deviceListener = [] {
wl_data_device_listener value {};
value.data_offer = data_device_data_offer;
value.enter = ignore_event<>;
value.leave = ignore_event<>;
value.motion = ignore_event<>;
value.drop = ignore_event<>;
value.selection = data_device_selection;
return value;
}();
wl_data_device_add_listener(state.dataDevice, &deviceListener, nullptr);
}
{
state.xdgSurface = xdg_wm_base_get_xdg_surface(
state.wmBase, state.surface
);
state.toplevel = xdg_surface_get_toplevel(state.xdgSurface);
window->setTitle(title);
xdg_toplevel_set_app_id(state.toplevel, "VoxelCore");
if (state.decorationManager) {
state.decoration =
zxdg_decoration_manager_v1_get_toplevel_decoration(
state.decorationManager, state.toplevel
);
static const zxdg_toplevel_decoration_v1_listener listener = [] {
zxdg_toplevel_decoration_v1_listener value {};
value.configure = zxdg_decoration_configure;
return value;
}();
zxdg_toplevel_decoration_v1_add_listener(
state.decoration, &listener, nullptr
);
zxdg_toplevel_decoration_v1_set_mode(
state.decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE
);
}
if (state.decorationManager == nullptr) {
window->enableOwnDecorations();
}
static const xdg_surface_listener surfaceListener = [] {
xdg_surface_listener value {};
value.configure = xdg_surface_configure;
return value;
}();
static const xdg_toplevel_listener toplevelListener = [] {
xdg_toplevel_listener value {};
value.configure = toplevel_configure;
value.close = toplevel_close;
value.configure_bounds = toplevel_configure_bounds;
value.wm_capabilities = toplevel_wm_capabilities;
return value;
}();
xdg_surface_add_listener(state.xdgSurface, &surfaceListener, nullptr);
xdg_toplevel_add_listener(state.toplevel, &toplevelListener, nullptr);
wl_surface_commit(state.surface);
}
while (!window->configured) {
if (wl_display_dispatch(state.display) == -1) {
waylandLogger.error() << "the Wayland connection was lost";
window = nullptr;
input = nullptr;
if (state.display != nullptr) {
wl_display_disconnect(state.display);
state.display = nullptr;
}
return {nullptr, nullptr};
}
}
window->eglDisplay = eglGetPlatformDisplay(
EGL_PLATFORM_WAYLAND_KHR, state.display, nullptr
);
if (window->eglDisplay == EGL_NO_DISPLAY ||
eglInitialize(window->eglDisplay, nullptr, nullptr) == EGL_FALSE) {
waylandLogger.error() << "could not initialize EGL";
window = nullptr;
input = nullptr;
if (state.display != nullptr) {
wl_display_disconnect(state.display);
state.display = nullptr;
}
return {nullptr, nullptr};
}
eglBindAPI(EGL_OPENGL_API);
EGLint samples = settings->samples.get();
const EGLint requestedSamples = samples > 0 ? samples : 0;
EGLConfig config = nullptr;
EGLint configCount = 0;
const auto chooseConfig = [&] (EGLint alpha, EGLint sampleCount) {
const EGLint attributes[] {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, alpha, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8,
EGL_SAMPLES, sampleCount,
EGL_NONE
};
config = nullptr;
configCount = 0;
eglChooseConfig(
window->eglDisplay, attributes, &config, 1, &configCount
);
if (configCount == 0) {
return false;
}
EGLint value = 0;
eglGetConfigAttrib(
window->eglDisplay, config, EGL_ALPHA_SIZE, &value
);
window->shadowEnabled = value >= 8;
return true;
};
if (!chooseConfig(8, requestedSamples)) {
if (requestedSamples > 0) {
waylandLogger.warning() << "multisampling is not available";
}
if (!chooseConfig(8, 0)) {
waylandLogger.warning()
<< "no EGL config with alpha, the drop shadow is off";
if (!chooseConfig(0, requestedSamples) && !chooseConfig(0, 0)) {
waylandLogger.error() << "no suitable EGL config";
window = nullptr;
input = nullptr;
if (state.display != nullptr) {
wl_display_disconnect(state.display);
state.display = nullptr;
}
return {nullptr, nullptr};
}
window->shadowEnabled = false;
}
}
const EGLint contextAttributes[] {
EGL_CONTEXT_MAJOR_VERSION, 3,
EGL_CONTEXT_MINOR_VERSION, 3,
EGL_NONE
};
window->eglContext = eglCreateContext(
window->eglDisplay, config, EGL_NO_CONTEXT, contextAttributes
);
const auto surfaceSize = window->surfaceSize();
window->eglWindow = wl_egl_window_create(
state.surface, surfaceSize.x, surfaceSize.y
);
window->eglSurface = eglCreateWindowSurface(
window->eglDisplay, config,
reinterpret_cast<EGLNativeWindowType>(window->eglWindow), nullptr
);
if (window->eglContext == EGL_NO_CONTEXT ||
window->eglSurface == EGL_NO_SURFACE ||
eglMakeCurrent(
window->eglDisplay, window->eglSurface, window->eglSurface,
window->eglContext
) == EGL_FALSE) {
waylandLogger.error() << "could not create the OpenGL context";
window = nullptr;
input = nullptr;
if (state.display != nullptr) {
wl_display_disconnect(state.display);
state.display = nullptr;
}
return {nullptr, nullptr};
}
eglSwapInterval(window->eglDisplay, 0);
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if (glewError != GLEW_OK && glewError != GLEW_ERROR_NO_GLX_DISPLAY) {
waylandLogger.error() << "failed to initialize GLEW:\n"
<< glewGetErrorString(glewError);
window = nullptr;
input = nullptr;
if (state.display != nullptr) {
wl_display_disconnect(state.display);
state.display = nullptr;
}
return {nullptr, nullptr};
}
window->initStretchRenderer();
window->initShadowRenderer();
glViewport(0, 0, window->getSize().x, window->getSize().y);
glClearColor(0.0f, 0.0f, 0.0f, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
GLint maxTextureSize[1] {static_cast<GLint>(Texture::MAX_RESOLUTION)};
glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxTextureSize);
if (maxTextureSize[0] > 0) {
Texture::MAX_RESOLUTION = maxTextureSize[0];
waylandLogger.info()
<< "max texture size is " << Texture::MAX_RESOLUTION;
}
waylandLogger.info() << "GL Vendor: "
<< reinterpret_cast<const char*>(glGetString(GL_VENDOR));
waylandLogger.info() << "GL Renderer: "
<< reinterpret_cast<const char*>(glGetString(GL_RENDERER));
waylandLogger.info() << "windowing platform: Wayland (native)";
return {std::move(windowPtr), std::move(inputPtr)};
}
void on_display_error() {
waylandLogger.error() << "the Wayland connection was lost";
if (window) {
window->setShouldClose(true);
}
}
void WaylandInput::pollEvents(bool waitForRefresh) {
updateClipboard();
beginFrame();
dispatch(waitForRefresh);
if (window) {
window->paceFrame();
}
updateRepeat();
updateBindings();
}
void WaylandInput::refreshWindow() {
if (window) {
window->setShouldRefresh();
}
}