voxelcore/src/window/detail/BaseInput.cpp
alexei-zebra 1e689e88b1
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.
2026-10-10 12:23:18 +03:00

152 lines
3.8 KiB
C++

#include "BaseInput.hpp"
void BaseInput::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 BaseInput::onMouseCallback(int button, bool pressed) {
onKeyCallback(button + MOUSE_KEYS_OFFSET, pressed);
}
bool BaseInput::isCursorLocked() const {
return cursorLocked;
}
void BaseInput::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& BaseInput::getBindings() {
return bindings;
}
const Bindings& BaseInput::getBindings() const {
return bindings;
}
ObserverHandler BaseInput::addKeyCallback(Keycode key, InputCallback callback) {
return keyCallbacks[key].add(std::move(callback));
}
ObserverHandler BaseInput::addMouseCallback(Mousecode button, InputCallback callback) {
return addKeyCallback(
static_cast<Keycode>(MOUSE_KEYS_OFFSET + static_cast<int>(button)),
std::move(callback)
);
}
const std::vector<Keycode>& BaseInput::getPressedKeys() const {
return pressedKeys;
}
const std::vector<uint>& BaseInput::getCodepoints() const {
return codepoints;
}
CursorState BaseInput::getCursor() const {
return {isCursorLocked(), cursor, delta};
}
int BaseInput::getScroll() {
return scroll;
}
bool BaseInput::pressed(Keycode key) const {
int keycode = static_cast<int>(key);
if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) {
return false;
}
return keys[keycode];
}
bool BaseInput::jpressed(Keycode keycode) const {
return pressed(keycode) &&
frames[static_cast<int>(keycode)] == currentFrame;
}
bool BaseInput::clicked(Mousecode code) const {
return pressed(
static_cast<Keycode>(MOUSE_KEYS_OFFSET + static_cast<int>(code))
);
}
bool BaseInput::jclicked(Mousecode code) const {
return clicked(code) &&
frames[static_cast<int>(code) + MOUSE_KEYS_OFFSET] == currentFrame;
}
void BaseInput::simulateKey(Keycode key, bool pressed) {
onKeyCallback(static_cast<int>(key), pressed);
}
void BaseInput::simulateClick(int button, bool pressed) {
onMouseCallback(button, pressed);
}
void BaseInput::simulateCursorPos(double xpos, double ypos) {
setCursorPosition(xpos, ypos);
}
void BaseInput::simulateCodepoint(uint codepoint) {
codepoints.push_back(codepoint);
}
void BaseInput::beginFrame() {
delta.x = 0.0f;
delta.y = 0.0f;
scroll = 0;
currentFrame++;
codepoints.clear();
pressedKeys.clear();
}
void BaseInput::updateBindings() {
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;
}
}
}
}