voxelcore/src/window/wayland/WaylandWindow.hpp
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

198 lines
6.1 KiB
C++

#pragma once
#include "window/wayland/WaylandCommon.hpp"
#include <algorithm>
#include "window/wayland/WaylandSettings.hpp"
#include "window/wayland/WaylandInput.hpp"
#include "graphics/core/ImageData.hpp"
#include "window/Window.hpp"
#include <EGL/egl.h>
#include <string>
#include <vector>
class WaylandWindow : public Window {
public:
WaylandInput& input;
DisplaySettings* settings;
EGLDisplay eglDisplay = EGL_NO_DISPLAY;
EGLContext eglContext = EGL_NO_CONTEXT;
EGLSurface eglSurface = EGL_NO_SURFACE;
wl_egl_window* eglWindow = nullptr;
int32_t pendingWidth = 0;
int32_t pendingHeight = 0;
bool stateMaximized = false;
bool stateSuspended = false;
bool stateActivated = true;
bool configured = false;
static constexpr double RESIZE_TIMEOUT = 0.4;
static constexpr double FRAME_TIMEOUT = 0.025;
static constexpr double RESIZE_PAUSE = 0.12;
static constexpr double MIN_CACHE_TIMEOUT = 0.3;
static constexpr double MAX_CACHE_TIMEOUT = 3.0;
int framerate = -1;
double prevSwap = 0.0;
double lastResize = 0.0;
bool barEnabled = false;
bool fullscreen = false;
std::string title = "VoxelCore";
int hoveredButton = -1;
bool pointerInBar = false;
SettingsWatcher settingsWatcher;
bool layoutRead = false;
void applyLayoutValue(const std::string& value);
std::vector<DecorationButton> leftButtons;
std::vector<DecorationButton> rightButtons;
std::vector<DecorationButtonLayout> barButtons;
double lastLayoutCheck = 0.0;
double lastBarPress = 0.0;
double lastBarPressX = 0.0;
double lastBarPressY = 0.0;
double barPressX = 0.0;
double barPressY = 0.0;
uint32_t barPressSerial = 0;
bool barPressed = false;
bool initialSizeApplied = false;
mutable bool frameRendered = false;
bool sizeChanged = false;
bool barDragging = false;
int barPressedButton = -1;
int initialWidth = 0;
int initialHeight = 0;
double pointerX = 0.0;
double pointerY = 0.0;
bool frameOnCallback = false;
double frameInterval = 0.0;
double nextFrameTime = 0.0;
double frameCost = 0.0;
double cacheTimeout = 0.5;
bool resizing = false;
bool renderPending = true;
bool presentPending = true;
double lastPresent = 0.0;
wl_callback* frameCallback = nullptr;
bool frameDone = false;
GLuint stretchProgram = 0;
GLint stretchTextureUniform = -1;
GLuint stretchVao = 0;
GLuint stretchVbo = 0;
GLuint cacheFbo = 0;
GLuint cacheTexture = 0;
int cacheWidth = 0;
int cacheHeight = 0;
double lastCacheTime = 0.0;
double lastSwapEnd = 0.0;
bool shouldRefresh = true;
bool shouldClose = false;
bool maximized = false;
bool suspended = false;
bool activated = true;
bool focusWarning = false;
WaylandWindow(
WaylandInput& input, DisplaySettings* settings, int width, int height
);
~WaylandWindow() override;
double time() override;
void swapBuffers() override;
void setShouldRefresh() override;
bool checkShouldRefresh() override;
bool isMaximized() const override;
bool isFocused() const override;
bool isIconified() const override;
/// @brief Whether the engine is going to draw a frame now.
/// The answer is remembered, so swapBuffers() can tell a freshly drawn
/// frame from a buffer the engine skipped (it would be undefined).
bool isFrameRequired() const override;
bool isShouldClose() const override;
void setShouldClose(bool flag) override;
void setCursor(CursorShape shape) override;
void setMode(WindowMode mode) override;
WindowMode getMode() const override;
void focus() override;
void setTitle(const std::string& title) override;
const std::string& getTitle() const override;
int getDecorationHeight() const override;
int getDecorationHoveredButton() const override;
static constexpr int BAR_HEIGHT = 48;
static constexpr int BAR_BUTTON = 24;
static constexpr int BAR_PADDING = 12;
static constexpr int BAR_GAP = 12;
static constexpr int BAR_EDGE = 8;
static constexpr int SHADOW_MARGIN = 10;
static constexpr int RESIZE_GRAB_EXTRA = 2;
bool shadowEnabled = false;
GLuint shadowProgram = 0;
GLint shadowUniforms[8] {};
int marginSize() const;
double contentX(double x) const {
return x - marginSize();
}
double contentY(double y) const {
return y - marginSize();
}
glm::ivec2 surfaceSize() const;
void updateWindowGeometry();
void initShadowRenderer();
void drawShadow();
void composeFrame();
void makeContentOpaque(int x, int y, int width, int height);
const std::vector<DecorationButtonLayout>& getDecorationButtons() const
override;
void updateButtonLayout();
void updateBarGeometry();
bool isIconSupported() const override {
return false;
}
void setIcon(const ImageData*) override {
}
void setFullscreen(bool enabled);
void setMaximized(bool enabled);
void resize(int width, int height);
void onConfigure(int32_t width, int32_t height, wl_array* states);
void applyConfigure();
std::unique_ptr<ImageData> takeScreenshot() override;
void setFramerate(int framerate) override;
void initStretchRenderer();
void resizeCache(int width, int height);
void cacheContent();
void drawStretched();
void enableOwnDecorations();
void disableOwnDecorations();
bool barVisible() const;
int barButtonAt(double x, double y) const;
uint32_t barEdgeAt(double x, double y) const;
static CursorShape edgeCursor(uint32_t edges);
uint32_t pointerEdges() const;
bool handleBarMotion(double x, double y);
bool handleBarButton(uint32_t serial, int button, bool pressed);
bool handleBarScroll() const;
void onPointerLeave();
void onFrameDone();
void requestFrame();
void paceFrame();
void markResizing();
void updateResizing();
void setIdleInhibit(bool enabled);
static const wl_callback_listener& frameListener();
static double now();
};