mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 09:59:44 +00:00
add native Wayland window backend
Wayland support was missing a proper windowing layer: the GLFW backend leaves the known platform gaps (cursor themes, window icon, window position, fullscreen output selection). The new backend talks to the compositor directly: - xdg-shell, EGL, xkbcommon (keyboard, repeat, layout) instead of GLFW; - cursors through wp_cursor_shape_manager_v1, so no cursor theme lookup is required and no "standard cursor shape unavailable" errors appear; - window icon and title are drawn by the engine itself, since Wayland has no window icon protocol and GNOME provides no server-side decorations; - fullscreen requests the current output (set_fullscreen with NULL), so the window no longer jumps to another monitor; - interactive resize presents a cached frame scaled to the new size while the pointer is down and refreshes the real frame when the drag pauses, keeping the window glued to the cursor. libdecor is not used: libdecor_frame_commit() redraws the whole decoration and acks the configure in the same call, and mutter sends 200-330 configures per second during an interactive resize, so the decoration was redrawn hundreds of times per second (measured: 2 s of work per second, loop stalls up to 6 s). The decorations are drawn by the engine instead, which measured 2.3 ms average configure-to-present latency during a real drag. GLFW stays the backend for X11 and Windows; VOXEL_WINDOW_BACKEND=x11 forces it on Wayland sessions.
This commit is contained in:
parent
1dd1af1a72
commit
1e689e88b1
18 changed files with 6567 additions and 526 deletions
|
|
@ -1,4 +1,6 @@
|
|||
#include "window/Window.hpp"
|
||||
#include "window/detail/BaseInput.hpp"
|
||||
#include "window/detail/WindowBackends.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
|
@ -18,9 +20,42 @@
|
|||
|
||||
static debug::Logger logger("window");
|
||||
|
||||
static const char* get_platform_name() {
|
||||
#if GLFW_VERSION_MAJOR > 3 || (GLFW_VERSION_MAJOR == 3 && GLFW_VERSION_MINOR >= 4)
|
||||
switch (glfwGetPlatform()) {
|
||||
case GLFW_PLATFORM_WIN32: return "Win32";
|
||||
case GLFW_PLATFORM_COCOA: return "Cocoa";
|
||||
case GLFW_PLATFORM_WAYLAND: return "Wayland (GLFW)";
|
||||
case GLFW_PLATFORM_X11: return "X11";
|
||||
case GLFW_PLATFORM_NULL: return "Null";
|
||||
}
|
||||
#endif
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
static std::unordered_set<std::string> supported_gl_extensions;
|
||||
static void window_size_callback(GLFWwindow* window, int width, int height);
|
||||
|
||||
static GLFWmonitor* get_window_monitor(GLFWwindow* window) {
|
||||
int x = 0, y = 0, width = 0, height = 0;
|
||||
glfwGetWindowPos(window, &x, &y);
|
||||
glfwGetWindowSize(window, &width, &height);
|
||||
x += width / 2;
|
||||
y += height / 2;
|
||||
int count = 0;
|
||||
GLFWmonitor** monitors = glfwGetMonitors(&count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
int monitorX = 0, monitorY = 0;
|
||||
glfwGetMonitorPos(monitors[i], &monitorX, &monitorY);
|
||||
const GLFWvidmode* mode = glfwGetVideoMode(monitors[i]);
|
||||
if (mode && x >= monitorX && x < monitorX + mode->width &&
|
||||
y >= monitorY && y < monitorY + mode->height) {
|
||||
return monitors[i];
|
||||
}
|
||||
}
|
||||
return glfwGetPrimaryMonitor();
|
||||
}
|
||||
|
||||
static void init_gl_extensions_list() {
|
||||
GLint numExtensions = 0;
|
||||
glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions);
|
||||
|
|
@ -85,21 +120,6 @@ static void GLAPIENTRY gl_message_callback(
|
|||
#endif
|
||||
|
||||
static bool initialize_gl(int width, int height) {
|
||||
glewExperimental = GL_TRUE;
|
||||
|
||||
GLenum glewErr = glewInit();
|
||||
if (glewErr != GLEW_OK) {
|
||||
if (glewErr == GLEW_ERROR_NO_GLX_DISPLAY) {
|
||||
// see issue #240
|
||||
logger.warning()
|
||||
<< "glewInit() returned GLEW_ERROR_NO_GLX_DISPLAY; ignored";
|
||||
} else {
|
||||
logger.error() << "failed to initialize GLEW:\n"
|
||||
<< glewGetErrorString(glewErr);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef __APPLE__
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glDebugMessageCallback(gl_message_callback, nullptr);
|
||||
|
|
@ -163,137 +183,8 @@ static void glfw_error_callback(int error, const char* description) {
|
|||
}
|
||||
}
|
||||
|
||||
inline constexpr short KEYS_BUFFER_SIZE = 1036;
|
||||
inline constexpr short MOUSE_KEYS_OFFSET = 1024;
|
||||
|
||||
static GLFWcursor* standard_cursors[static_cast<int>(CursorShape::LAST) + 1] = {};
|
||||
|
||||
class BaseInput : public Input {
|
||||
public:
|
||||
std::vector<uint> codepoints;
|
||||
int scroll = 0;
|
||||
|
||||
void onKeyCallback(int key, bool pressed) {
|
||||
bool prevPressed = keys[key];
|
||||
keys[key] = pressed;
|
||||
frames[key] = currentFrame;
|
||||
if (pressed && !prevPressed) {
|
||||
const auto& callbacks = keyCallbacks.find(static_cast<Keycode>(key));
|
||||
if (callbacks != keyCallbacks.end()) {
|
||||
callbacks->second.notify();
|
||||
}
|
||||
}
|
||||
if (pressed && key < MOUSE_KEYS_OFFSET) {
|
||||
pressedKeys.push_back(static_cast<Keycode>(key));
|
||||
}
|
||||
}
|
||||
|
||||
void onMouseCallback(int button, bool pressed) {
|
||||
int key = button + MOUSE_KEYS_OFFSET;
|
||||
onKeyCallback(key, pressed);
|
||||
}
|
||||
|
||||
bool isCursorLocked() const override {
|
||||
return cursorLocked;
|
||||
}
|
||||
|
||||
void setCursorPosition(double xpos, double ypos) {
|
||||
if (cursorDrag) {
|
||||
delta.x += xpos - cursor.x;
|
||||
delta.y += ypos - cursor.y;
|
||||
} else {
|
||||
cursorDrag = true;
|
||||
}
|
||||
cursor.x = xpos;
|
||||
cursor.y = ypos;
|
||||
}
|
||||
|
||||
Bindings& getBindings() override {
|
||||
return bindings;
|
||||
}
|
||||
|
||||
const Bindings& getBindings() const override {
|
||||
return bindings;
|
||||
}
|
||||
|
||||
ObserverHandler addKeyCallback(Keycode key, InputCallback callback) override {
|
||||
return keyCallbacks[key].add(std::move(callback));
|
||||
}
|
||||
|
||||
ObserverHandler addMouseCallback(Mousecode button, InputCallback callback) override {
|
||||
return addKeyCallback(
|
||||
static_cast<Keycode>(MOUSE_KEYS_OFFSET + static_cast<int>(button)),
|
||||
std::move(callback)
|
||||
);
|
||||
}
|
||||
|
||||
const std::vector<Keycode>& getPressedKeys() const override {
|
||||
return pressedKeys;
|
||||
}
|
||||
|
||||
const std::vector<uint>& getCodepoints() const override {
|
||||
return codepoints;
|
||||
}
|
||||
|
||||
CursorState getCursor() const override {
|
||||
return {isCursorLocked(), cursor, delta};
|
||||
}
|
||||
|
||||
int getScroll() override {
|
||||
return scroll;
|
||||
}
|
||||
|
||||
bool pressed(Keycode key) const override {
|
||||
int keycode = static_cast<int>(key);
|
||||
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
|
||||
return false;
|
||||
}
|
||||
return keys[keycode];
|
||||
}
|
||||
bool jpressed(Keycode keycode) const override {
|
||||
return pressed(keycode) &&
|
||||
frames[static_cast<int>(keycode)] == currentFrame;
|
||||
}
|
||||
|
||||
bool clicked(Mousecode code) const override {
|
||||
return pressed(
|
||||
static_cast<Keycode>(MOUSE_KEYS_OFFSET + static_cast<int>(code))
|
||||
);
|
||||
}
|
||||
bool jclicked(Mousecode code) const override {
|
||||
return clicked(code) &&
|
||||
frames[static_cast<int>(code) + MOUSE_KEYS_OFFSET] ==
|
||||
currentFrame;
|
||||
}
|
||||
|
||||
void simulateKey(Keycode key, bool pressed) override {
|
||||
onKeyCallback(static_cast<int>(key), pressed);
|
||||
}
|
||||
|
||||
void simulateClick(int button, bool pressed) override {
|
||||
onMouseCallback(static_cast<int>(button), pressed);
|
||||
}
|
||||
|
||||
void simulateCursorPos(double xpos, double ypos) override {
|
||||
setCursorPosition(xpos, ypos);
|
||||
}
|
||||
|
||||
void simulateCodepoint(uint codepoint) override {
|
||||
codepoints.push_back(codepoint);
|
||||
}
|
||||
protected:
|
||||
uint currentFrame = 0;
|
||||
uint frames[KEYS_BUFFER_SIZE] {};
|
||||
std::vector<Keycode> pressedKeys;
|
||||
Bindings bindings;
|
||||
bool keys[KEYS_BUFFER_SIZE] {};
|
||||
std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;
|
||||
bool cursorLocked = false;
|
||||
bool cursorDrag = false;
|
||||
glm::vec2 delta {};
|
||||
glm::vec2 cursor {};
|
||||
};
|
||||
|
||||
class GLFWInput : public BaseInput {
|
||||
public:
|
||||
GLFWInput(GLFWwindow* window)
|
||||
|
|
@ -301,48 +192,14 @@ public:
|
|||
}
|
||||
|
||||
void pollEvents(bool waitForRefresh) override {
|
||||
delta.x = 0.0f;
|
||||
delta.y = 0.0f;
|
||||
scroll = 0;
|
||||
currentFrame++;
|
||||
codepoints.clear();
|
||||
pressedKeys.clear();
|
||||
beginFrame();
|
||||
if (waitForRefresh) {
|
||||
glfwWaitEventsTimeout(0.5);
|
||||
} else {
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
for (auto& [_, binding] : bindings.getAll()) {
|
||||
if (!binding.enabled) {
|
||||
binding.state = false;
|
||||
continue;
|
||||
}
|
||||
binding.justChanged = false;
|
||||
|
||||
bool newstate = false;
|
||||
switch (binding.type) {
|
||||
case InputType::KEYBOARD:
|
||||
newstate = pressed(static_cast<Keycode>(binding.code));
|
||||
break;
|
||||
case InputType::MOUSE:
|
||||
newstate = clicked(static_cast<Mousecode>(binding.code));
|
||||
break;
|
||||
}
|
||||
|
||||
if (newstate) {
|
||||
if (!binding.state) {
|
||||
binding.state = true;
|
||||
binding.justChanged = true;
|
||||
binding.onactived.notify();
|
||||
}
|
||||
} else {
|
||||
if (binding.state) {
|
||||
binding.state = false;
|
||||
binding.justChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
updateBindings();
|
||||
}
|
||||
|
||||
const char* getClipboardText() const override {
|
||||
|
|
@ -455,7 +312,7 @@ public:
|
|||
|
||||
void setMode(WindowMode mode) override {
|
||||
Window::mode = mode;
|
||||
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
|
||||
GLFWmonitor* monitor = get_window_monitor(window);
|
||||
const GLFWvidmode* glfwMode = glfwGetVideoMode(monitor);
|
||||
|
||||
if (input.isCursorLocked()){
|
||||
|
|
@ -507,6 +364,10 @@ public:
|
|||
glfwSetWindowTitle(window, title.c_str());
|
||||
}
|
||||
|
||||
bool isIconSupported() const override {
|
||||
return true;
|
||||
}
|
||||
|
||||
void setIcon(const ImageData* image) override {
|
||||
if (image == nullptr) {
|
||||
glfwSetWindowIcon(window, 0, nullptr);
|
||||
|
|
@ -709,7 +570,7 @@ static void setup_callbacks(GLFWwindow* window) {
|
|||
std::tuple<
|
||||
std::unique_ptr<Window>,
|
||||
std::unique_ptr<Input>
|
||||
> Window::initialize(DisplaySettings* settings, std::string title) {
|
||||
> glfw_window_initialize(DisplaySettings* settings, std::string title) {
|
||||
int width = settings->width.get();
|
||||
int height = settings->height.get();
|
||||
|
||||
|
|
@ -718,6 +579,7 @@ std::tuple<
|
|||
logger.error() << "failed to initialize GLFW";
|
||||
return {nullptr, nullptr};
|
||||
}
|
||||
logger.info() << "windowing platform: " << get_platform_name();
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
|
|
@ -782,7 +644,6 @@ std::tuple<
|
|||
setup_callbacks(window);
|
||||
|
||||
glfwSwapInterval(1);
|
||||
input_util::initialize();
|
||||
create_standard_cursors();
|
||||
|
||||
glm::vec2 scale;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue