diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6be8a91ea..3ee250a4d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -84,6 +84,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND VOXELENGINE_WAYLAND) pkg_check_modules(egl IMPORTED_TARGET egl) find_library(WAYLAND_EGL_LIBRARY NAMES wayland-egl libwayland-egl.so.1) find_program(WAYLAND_SCANNER wayland-scanner) + pkg_check_modules(libsystemd IMPORTED_TARGET libsystemd) if(NOT wayland-client_FOUND OR NOT xkbcommon_FOUND OR NOT egl_FOUND OR NOT WAYLAND_EGL_LIBRARY OR NOT WAYLAND_SCANNER) @@ -128,6 +129,13 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND VOXELENGINE_WAYLAND) VoxelCoreSrc PRIVATE ${WAYLAND_SOURCES} ${wayland_sources}) target_include_directories(VoxelCoreSrc PUBLIC ${WAYLAND_GENERATED}) target_compile_definitions(VoxelCoreSrc PUBLIC VOXELENGINE_WAYLAND=1) + if(libsystemd_FOUND) + target_compile_definitions(VoxelCoreSrc PUBLIC VOXELENGINE_SDBUS=1) + target_link_libraries(VoxelCoreSrc PRIVATE PkgConfig::libsystemd) + message(STATUS "Wayland backend: sd-bus settings are on") + else() + message(STATUS "Wayland backend: sd-bus settings are off") + endif() target_link_libraries( VoxelCoreSrc PRIVATE PkgConfig::wayland-client diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 4f39f9356..3e5edb371 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -1,4 +1,9 @@ +#include + +#include + #include "Engine.hpp" +#include "frontend/screens/MenuScreen.hpp" #include "WindowBar.hpp" #ifndef GLEW_STATIC @@ -310,9 +315,20 @@ void Engine::renderFrame() { if (input->isCursorLocked() != (gui->getActiveFrame() == nullptr)) { input->toggleCursor(); } + glViewport( + 0, + 0, + static_cast(window->getSize().x), + static_cast(window->getSize().y) + ); screen->draw(time.getDelta()); DrawContext ctx(nullptr, *window, nullptr); + gui->setContentInset( + dynamic_cast(screen.get()) != nullptr + ? 0 + : window->getDecorationHeight() + ); gui->draw(ctx, *assets->getStorage()); draw_window_bar(*this); } diff --git a/src/engine/WindowBar.cpp b/src/engine/WindowBar.cpp index 661b30d4c..ff0f33ecd 100644 --- a/src/engine/WindowBar.cpp +++ b/src/engine/WindowBar.cpp @@ -1,12 +1,18 @@ #include "WindowBar.hpp" #include "Engine.hpp" +#include "debug/Logger.hpp" #include "graphics/core/Batch2D.hpp" +#include +#include +#include "graphics/core/Shader.hpp" #include "graphics/core/Font.hpp" #include "graphics/ui/GUI.hpp" #include "window/Window.hpp" #include "assets/Assets.hpp" +#include +#include #include #include @@ -33,46 +39,85 @@ void draw_window_bar(Engine& engine) { const auto size = window.getSize(); const float barHeight = static_cast(height); + glViewport(0, 0, static_cast(size.x), static_cast(size.y)); + if (auto shader = engine.requireAssets().get("ui"); shader != nullptr) { + shader->use(); + shader->uniformMatrix( + "u_projview", + glm::ortho( + 0.0f, + static_cast(size.x), + static_cast(size.y), + 0.0f, + -1.0f, + 1.0f + ) + ); + } + batch->begin(); batch->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.82f)); batch->rect(0.0f, 0.0f, size.x, barHeight); - batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.10f)); - batch->rect(0.0f, barHeight - 1.0f, size.x, 1.0f); - const std::wstring title = to_wide(window.getTitle()); - const int textX = static_cast(barHeight * 0.4f); - const int textY = (height - font->getLineHeight()) / 2; - batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.85f)); - font->draw(*batch, title, textX, textY, nullptr, 0); - batch->untexture(); - - const float button = barHeight; + const auto& buttons = window.getDecorationButtons(); const int hovered = window.getDecorationHoveredButton(); - for (int i = 0; i < 3; i++) { - const float x = size.x - button * (i + 1); - const float cx = x + button * 0.5f; - const float cy = barHeight * 0.5f; - if (hovered == i) { - batch->setColor( - i == 0 ? glm::vec4(0.62f, 0.13f, 0.09f, 0.9f) - : glm::vec4(1.0f, 1.0f, 1.0f, 0.16f) - ); - batch->rect(x, 0.0f, button, barHeight - 1.0f); + + float titleLeft = static_cast(barHeight * 0.4f); + float titleRight = size.x - 8.0f; + for (const auto& item : buttons) { + if (item.x + item.width * 0.5f < size.x * 0.5f) { + titleLeft = std::max(titleLeft, item.x + item.width + 8.0f); + } else { + titleRight = std::min(titleRight, item.x - 8.0f); } - const float ink = hovered == i ? 1.0f : 0.8f; - const float thickness = 2.0f; + } + const std::wstring title = to_wide(window.getTitle()); + const float available = std::max(0.0f, titleRight - titleLeft); + size_t titleLength = title.size(); + while (titleLength > 0 && font->calcWidth(title, titleLength) > available) { + titleLength--; + } + if (titleLength > 0) { + batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.85f)); + font->draw( + *batch, std::wstring_view(title).substr(0, titleLength), + static_cast(titleLeft), + (height - font->getLineHeight()) / 2, + nullptr, 0 + ); + batch->untexture(); + } + + const float thickness = 2.0f; + for (size_t i = 0; i < buttons.size(); i++) { + const auto& item = buttons[i]; + const float x = item.x; + const float cx = x + item.width * 0.5f; + const float cy = barHeight * 0.5f; + const bool isHovered = hovered == static_cast(i); + if (isHovered) { + batch->setColor( + item.button == DecorationButton::CLOSE + ? glm::vec4(0.62f, 0.13f, 0.09f, 0.9f) + : glm::vec4(1.0f, 1.0f, 1.0f, 0.16f) + ); + batch->rect(x, 0.0f, item.width, barHeight); + } + const float ink = isHovered ? 1.0f : 0.8f; batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, ink)); - if (i == 0) { - for (int s = 0; s < 8; s++) { - batch->rect(cx - 5.0f + s, cy - 5.0f + s, thickness, thickness); - batch->rect(cx + 3.0f - s, cy - 5.0f + s, thickness, thickness); + if (item.button == DecorationButton::CLOSE) { + for (int step = 0; step < 8; step++) { + batch->rect(cx - 5.0f + step, cy - 5.0f + step, thickness, thickness); + batch->rect(cx + 3.0f - step, cy - 5.0f + step, thickness, thickness); } - } else if (i == 1 && !window.isMaximized()) { + } else if (item.button == DecorationButton::MINIMIZE) { + batch->rect(cx - 6.0f, cy - 1.0f, 12.0f, thickness); + } else if (!window.isMaximized()) { batch->rect(cx - 6.0f, cy - 6.0f, 12.0f, thickness); batch->rect(cx - 6.0f, cy + 4.0f, 12.0f, thickness); batch->rect(cx - 6.0f, cy - 6.0f, thickness, 12.0f); batch->rect(cx + 4.0f, cy - 6.0f, thickness, 12.0f); - } else if (i == 1) { + } else { batch->rect(cx - 7.0f, cy + 1.0f, 11.0f, thickness); batch->rect(cx - 7.0f, cy + 1.0f, thickness, 6.0f); batch->rect(cx + 2.0f, cy + 1.0f, thickness, 6.0f); @@ -80,8 +125,6 @@ void draw_window_bar(Engine& engine) { batch->rect(cx - 2.0f, cy - 5.0f, 9.0f, thickness); batch->rect(cx - 2.0f, cy - 5.0f, thickness, 6.0f); batch->rect(cx + 5.0f, cy - 5.0f, thickness, 6.0f); - } else { - batch->rect(cx - 6.0f, cy - 1.0f, 12.0f, thickness); } } batch->flush(); diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 6751b885c..50748323e 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -625,8 +625,9 @@ void Hud::setDebug(bool flag) { void Hud::draw(const DrawContext& ctx){ const auto& viewport = ctx.getViewport(); + uint inset = static_cast(ctx.getWindow().getDecorationHeight()); - updateElementsPosition(viewport); + updateElementsPosition(glm::uvec2(viewport.x, viewport.y - inset)); uicamera->setFov(viewport.y); uicamera->setAspectRatio(viewport.x / static_cast(viewport.y)); @@ -698,7 +699,7 @@ void Hud::updateElementsPosition(const glm::uvec2& viewport) { } } if (exchangeSlot != nullptr) { - exchangeSlot->setPos(input.getCursor().pos); + exchangeSlot->setPos(gui.getCursorPosition()); } hotbarView->setPos(glm::vec2(viewport.x / 2, viewport.y - 65)); hotbarView->setSelected(player.getChosenSlot()); diff --git a/src/graphics/core/DrawContext.hpp b/src/graphics/core/DrawContext.hpp index 7e568e337..38b597929 100644 --- a/src/graphics/core/DrawContext.hpp +++ b/src/graphics/core/DrawContext.hpp @@ -36,6 +36,10 @@ public: Batch2D* getBatch2D() const; const glm::uvec2& getViewport() const; + + Window& getWindow() const { + return window; + } [[nodiscard]] DrawContext sub(Flushable* flushable=nullptr) const; void setViewport(const glm::uvec2& viewport); diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index a5f3010ff..d80c03d71 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -109,7 +109,7 @@ void GUI::updateTooltip(float delta) { tooltip->setVisible(true); label->setText(langs::get(text)); auto size = label->getSize() + glm::vec2(4.0f); - auto pos = cursor.pos + glm::vec2(10.0f); + auto pos = getCursorPosition() + glm::vec2(10.0f); auto rootSize = container->getSize(); pos.x = glm::min(pos.x, rootSize.x - size.x); pos.y = glm::min(pos.y, rootSize.y - size.y); @@ -242,7 +242,9 @@ void GUI::actFocused() { } void GUI::act(float delta, const glm::uvec2& vp) { - container->setSize(vp); + const uint inset = static_cast(viewportOffset); + container->setPos({0.0f, static_cast(inset)}); + container->setSize({vp.x, vp.y > inset ? vp.y - inset : vp.y}); for (auto& pair : frames) { pair.second->act(delta); } @@ -276,6 +278,11 @@ void GUI::postAct() { focusedOnStart = focus.get(); } +glm::vec2 GUI::getCursorPosition() const { + const auto& cursor = input.getCursor(); + return {cursor.pos.x, cursor.pos.y - static_cast(viewportOffset)}; +} + void GUI::draw(const DrawContext& pctx, Assets& assets) { auto ctx = pctx.sub(batch2D.get()); diff --git a/src/graphics/ui/GUI.hpp b/src/graphics/ui/GUI.hpp index e8b284a65..4ccc51147 100644 --- a/src/graphics/ui/GUI.hpp +++ b/src/graphics/ui/GUI.hpp @@ -123,6 +123,16 @@ namespace gui { /// @param assets active assets storage void draw(const DrawContext& pctx, Assets& assets); + int viewportOffset = 0; + + /// @brief Get the cursor position in the main container coordinates + glm::vec2 getCursorPosition() const; + + /// @brief Shift the main container down by the given number of pixels + void setContentInset(int inset) { + viewportOffset = inset; + } + Batch2D* getBatch2D() { return batch2D.get(); } diff --git a/src/window/Window.hpp b/src/window/Window.hpp index 0109faf7b..020e36c0a 100644 --- a/src/window/Window.hpp +++ b/src/window/Window.hpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include "graphics/core/commons.hpp" #include "typedefs.hpp" @@ -10,6 +12,18 @@ class ImageData; class Input; struct DisplaySettings; +enum class DecorationButton { + CLOSE, + MINIMIZE, + MAXIMIZE, +}; + +struct DecorationButtonLayout { + DecorationButton button; + float x; + float width; +}; + enum class WindowMode { WINDOWED, FULLSCREEN, @@ -57,6 +71,11 @@ public: return -1; } + virtual const std::vector& getDecorationButtons() const { + static const std::vector empty; + return empty; + } + virtual bool isIconSupported() const = 0; virtual void setIcon(const ImageData* image) = 0; diff --git a/src/window/wayland/WaylandDecorations.cpp b/src/window/wayland/WaylandDecorations.cpp index ab2f221de..d72544f11 100644 --- a/src/window/wayland/WaylandDecorations.cpp +++ b/src/window/wayland/WaylandDecorations.cpp @@ -2,9 +2,172 @@ #include "settings.hpp" -static constexpr int BAR_HEIGHT = 30; -static constexpr int BAR_BUTTON = 30; -static constexpr int BAR_EDGE = 5; +#include +#include +#include +#include +#include +#include + +namespace { + constexpr const char* LAYOUT_SCHEMA = "org.gnome.desktop.wm.preferences"; + constexpr const char* LAYOUT_KEY = "button-layout"; + + std::vector parse_buttons(const std::string& text) { + std::vector buttons; + size_t start = 0; + while (start <= text.size()) { + const size_t end = text.find(',', start); + const std::string name = text.substr( + start, end == std::string::npos ? std::string::npos : end - start + ); + if (name == "close") { + buttons.push_back(DecorationButton::CLOSE); + } else if (name == "minimize" || name == "min") { + buttons.push_back(DecorationButton::MINIMIZE); + } else if (name == "maximize" || name == "max") { + buttons.push_back(DecorationButton::MAXIMIZE); + } + if (end == std::string::npos) { + break; + } + start = end + 1; + } + return buttons; + } + + bool read_button_layout( + std::vector& left, std::vector& right + ) { + FILE* pipe = popen( + "gsettings get org.gnome.desktop.wm.preferences button-layout" + " 2>/dev/null", + "r" + ); + if (pipe == nullptr) { + return false; + } + char buffer[256] {}; + const char* line = fgets(buffer, sizeof(buffer), pipe); + const int status = pclose(pipe); + if (line == nullptr || status != 0) { + return false; + } + std::string value(buffer); + value.erase( + std::remove_if(value.begin(), value.end(), [] (char c) { + return c == '\'' || c == '"' || c == '\n' || c == '\r'; + }), + value.end() + ); + const size_t separator = value.find(':'); + if (separator == std::string::npos) { + left.clear(); + right = parse_buttons(value); + } else { + left = parse_buttons(value.substr(0, separator)); + right = parse_buttons(value.substr(separator + 1)); + } + return true; + } + + constexpr double DRAG_THRESHOLD = 5.0; + constexpr double DOUBLE_CLICK_TIME = 0.4; + constexpr double DOUBLE_CLICK_DISTANCE = 8.0; +} + +void WaylandWindow::applyLayoutValue(const std::string& rawValue) { + std::string value = rawValue; + value.erase( + std::remove_if(value.begin(), value.end(), [] (char c) { + return c == '\'' || c == '"' || c == '\n' || c == '\r'; + }), + value.end() + ); + const size_t separator = value.find(':'); + std::vector left; + std::vector right; + if (separator == std::string::npos) { + right = parse_buttons(value); + } else { + left = parse_buttons(value.substr(0, separator)); + right = parse_buttons(value.substr(separator + 1)); + } + if (left == leftButtons && right == rightButtons) { + return; + } + leftButtons = std::move(left); + rightButtons = std::move(right); + updateBarGeometry(); + setShouldRefresh(); +} + +void WaylandWindow::updateButtonLayout() { + if (!layoutRead) { + layoutRead = true; + std::string initial; + if (settingsWatcher.read(LAYOUT_SCHEMA, LAYOUT_KEY, initial)) { + applyLayoutValue(initial); + } else { + std::vector left; + std::vector right; + if (read_button_layout(left, right)) { + leftButtons = std::move(left); + rightButtons = std::move(right); + updateBarGeometry(); + } + } + } + std::string value; + if (settingsWatcher.poll(LAYOUT_SCHEMA, LAYOUT_KEY, value)) { + applyLayoutValue(value); + return; + } + if (settingsWatcher.isAvailable()) { + return; + } + const double current = now(); + if (current - lastLayoutCheck < 2.0) { + return; + } + lastLayoutCheck = current; + std::vector left; + std::vector right; + if (!read_button_layout(left, right)) { + return; + } + if (left == leftButtons && right == rightButtons) { + return; + } + leftButtons = std::move(left); + rightButtons = std::move(right); + updateBarGeometry(); +} + +void WaylandWindow::updateBarGeometry() { + barButtons.clear(); + const float width = static_cast(size.x); + for (size_t i = 0; i < leftButtons.size(); i++) { + const float x = static_cast(i) * BAR_BUTTON; + if (x + BAR_BUTTON > width) { + break; + } + barButtons.push_back({leftButtons[i], x, BAR_BUTTON}); + } + for (size_t i = 0; i < rightButtons.size(); i++) { + const float x = width - + static_cast(rightButtons.size() - i) * BAR_BUTTON; + if (x < 0.0f) { + break; + } + barButtons.push_back({rightButtons[i], x, BAR_BUTTON}); + } +} + +const std::vector& WaylandWindow::getDecorationButtons() const { + return barButtons; +} + const std::string& WaylandWindow::getTitle() const { return title; } @@ -33,27 +196,34 @@ int WaylandWindow::barButtonAt(double x, double y) const { if (!barVisible() || y < 0.0 || y >= BAR_HEIGHT) { return -1; } - for (int i = 0; i < 3; i++) { - const double bx = size.x - BAR_BUTTON * (i + 1); - if (x >= bx && x < bx + BAR_BUTTON) { - return i; + for (size_t i = 0; i < barButtons.size(); i++) { + const auto& item = barButtons[i]; + if (x >= item.x && x < item.x + item.width) { + return static_cast(i); } } return -1; } uint32_t WaylandWindow::barEdgeAt(double x, double y) const { + if (barButtonAt(x, y) >= 0) { + return 0; + } + const bool inBar = barVisible() && y >= 0.0 && y < BAR_HEIGHT; uint32_t edges = 0; + if (y < BAR_EDGE) { + edges |= XDG_TOPLEVEL_RESIZE_EDGE_TOP; + } else if (y >= size.y - BAR_EDGE) { + edges |= XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM; + } + if (inBar && y >= BAR_EDGE) { + return edges; + } if (x < BAR_EDGE) { edges |= XDG_TOPLEVEL_RESIZE_EDGE_LEFT; } else if (x >= size.x - BAR_EDGE) { edges |= XDG_TOPLEVEL_RESIZE_EDGE_RIGHT; } - if (barVisible() && y < BAR_EDGE) { - edges |= XDG_TOPLEVEL_RESIZE_EDGE_TOP; - } else if (y >= size.y - BAR_EDGE) { - edges |= XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM; - } return edges; } @@ -80,61 +250,110 @@ CursorShape WaylandWindow::edgeCursor(uint32_t edges) { bool WaylandWindow::handleBarMotion(double x, double y) { pointerX = x; pointerY = y; - const int button = barButtonAt(x, y); - if (barVisible() && y >= 0.0 && y < BAR_HEIGHT) { - hoveredButton = button; + if (barPressed && !barDragging && + (std::abs(x - barPressX) > DRAG_THRESHOLD || + std::abs(y - barPressY) > DRAG_THRESHOLD)) { + barDragging = true; + hoveredButton = -1; + xdg_toplevel_move(state.toplevel, state.seat, barPressSerial); + } + const uint32_t edges = barEdgeAt(x, y); + if (edges != 0) { + hoveredButton = -1; + pointerInBar = false; + input.setOverrideCursor(edgeCursor(edges)); + return true; + } + const bool inBar = barVisible() && y >= 0.0 && y < BAR_HEIGHT; + if (inBar && !pointerInBar) { + lastLayoutCheck = 0.0; + } + pointerInBar = inBar; + if (inBar) { + hoveredButton = barDragging ? -1 : barButtonAt(x, y); input.setOverrideCursor( - button >= 0 ? CursorShape::POINTER : CursorShape::ARROW + hoveredButton >= 0 ? CursorShape::POINTER : CursorShape::ARROW ); return true; } hoveredButton = -1; - const uint32_t edges = barEdgeAt(x, y); - input.setOverrideCursor( - edges != 0 ? std::optional(edgeCursor(edges)) - : std::nullopt - ); + input.setOverrideCursor(std::nullopt); return false; } -bool WaylandWindow::handleBarButton(uint32_t serial, bool pressed) { +bool WaylandWindow::handleBarButton(uint32_t serial, int button, bool pressed) { const double x = pointerX; const double y = pointerY; - const int button = barButtonAt(x, y); - if (button >= 0) { - if (pressed) { - if (button == 0) { - setShouldClose(true); - } else if (button == 1) { - setMaximized(!maximized); - } else { - xdg_toplevel_set_minimized(state.toplevel); - } - } - return true; - } - if (barVisible() && y >= 0.0 && y < BAR_HEIGHT) { - if (pressed) { - const double current = now(); - if (current - lastBarPress < 0.4) { - lastBarPress = 0.0; - setMaximized(!maximized); - } else { - lastBarPress = current; - xdg_toplevel_move(state.toplevel, state.seat, serial); - } - } - return true; - } const uint32_t edges = barEdgeAt(x, y); - if (pressed && edges != 0) { - xdg_toplevel_resize(state.toplevel, state.seat, serial, edges); + const bool inBar = barVisible() && y >= 0.0 && y < BAR_HEIGHT; + if (button != 0) { + return inBar || edges != 0; + } + if (edges != 0) { + if (pressed) { + xdg_toplevel_resize(state.toplevel, state.seat, serial, edges); + } return true; } - return false; + if (!inBar) { + return false; + } + if (pressed) { + const double current = now(); + const bool doubleClick = + current - lastBarPress < DOUBLE_CLICK_TIME && + std::abs(x - lastBarPressX) < DOUBLE_CLICK_DISTANCE && + std::abs(y - lastBarPressY) < DOUBLE_CLICK_DISTANCE; + lastBarPress = current; + lastBarPressX = x; + lastBarPressY = y; + if (doubleClick) { + lastBarPress = 0.0; + setMaximized(!maximized); + return true; + } + barPressed = true; + barDragging = false; + barPressedButton = barButtonAt(x, y); + barPressX = x; + barPressY = y; + barPressSerial = serial; + hoveredButton = barPressedButton; + return true; + } + const int releasedButton = barPressedButton; + const bool dragged = barDragging; + barPressed = false; + barDragging = false; + barPressedButton = -1; + if (!dragged && releasedButton >= 0 && + releasedButton < static_cast(barButtons.size()) && + barButtonAt(x, y) == releasedButton) { + switch (barButtons[releasedButton].button) { + case DecorationButton::CLOSE: + setShouldClose(true); + break; + case DecorationButton::MAXIMIZE: + setMaximized(!maximized); + break; + case DecorationButton::MINIMIZE: + xdg_toplevel_set_minimized(state.toplevel); + break; + } + } + return true; +} + +bool WaylandWindow::handleBarScroll() const { + return (barVisible() && pointerY >= 0.0 && pointerY < BAR_HEIGHT) || + barEdgeAt(pointerX, pointerY) != 0; } void WaylandWindow::onPointerLeave() { hoveredButton = -1; + pointerInBar = false; + barPressed = false; + barDragging = false; + barPressedButton = -1; input.setOverrideCursor(std::nullopt); } diff --git a/src/window/wayland/WaylandListeners.cpp b/src/window/wayland/WaylandListeners.cpp index 8c9742920..d92393678 100644 --- a/src/window/wayland/WaylandListeners.cpp +++ b/src/window/wayland/WaylandListeners.cpp @@ -61,7 +61,7 @@ static void pointer_button( case 0x117: index = 7; break; } const bool pressed = buttonState == WL_POINTER_BUTTON_STATE_PRESSED; - if (index == 0 && window && window->handleBarButton(serial, pressed)) { + if (index >= 0 && window && window->handleBarButton(serial, index, pressed)) { return; } if (index >= 0 && input) { @@ -74,6 +74,9 @@ static void pointer_axis( void*, wl_pointer*, uint32_t, uint32_t axis, wl_fixed_t value ) { if (input && axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { + if (window && window->handleBarScroll()) { + return; + } input->scroll += wl_fixed_to_double(value) > 0 ? -1 : 1; input->refreshWindow(); } diff --git a/src/window/wayland/WaylandSettings.cpp b/src/window/wayland/WaylandSettings.cpp new file mode 100644 index 000000000..58817b16f --- /dev/null +++ b/src/window/wayland/WaylandSettings.cpp @@ -0,0 +1,176 @@ +#include "window/wayland/WaylandSettings.hpp" + +#include "window/wayland/WaylandCommon.hpp" + +#include + +#ifdef VOXELENGINE_SDBUS +#include +#endif + +namespace { + constexpr const char* PORTAL_SERVICE = "org.freedesktop.portal.Desktop"; + constexpr const char* PORTAL_PATH = "/org/freedesktop/portal/desktop"; + constexpr const char* PORTAL_INTERFACE = "org.freedesktop.portal.Settings"; + +#ifdef VOXELENGINE_SDBUS + std::string readVariant(sd_bus_message* message) { + const char* contents = nullptr; + char type = 0; + if (sd_bus_message_peek_type(message, &type, &contents) < 0) { + return {}; + } + if (type == 'v') { + if (sd_bus_message_enter_container(message, 'v', nullptr) < 0) { + return {}; + } + const std::string result = readVariant(message); + sd_bus_message_exit_container(message); + return result; + } + if (type == 's') { + const char* text = nullptr; + if (sd_bus_message_read(message, "s", &text) < 0 || text == nullptr) { + return {}; + } + return text; + } + return {}; + } +#endif +} + +struct SettingsWatcher::Impl { +#ifdef VOXELENGINE_SDBUS + sd_bus* bus = nullptr; + std::string changedKey; + std::string changedValue; + bool changed = false; + + static int onSettingChanged( + sd_bus_message* message, void* userdata, sd_bus_error* + ) { + auto* self = static_cast(userdata); + const char* schema = nullptr; + const char* key = nullptr; + if (sd_bus_message_read(message, "ss", &schema, &key) < 0) { + return 0; + } + const std::string value = readVariant(message); + if (key != nullptr && !value.empty()) { + self->changedKey = key; + self->changedValue = value; + self->changed = true; + } + return 0; + } +#endif +}; + +SettingsWatcher::SettingsWatcher() : impl(new Impl()) { + open(); +} + +SettingsWatcher::~SettingsWatcher() { + close(); + delete impl; +} + +bool SettingsWatcher::open() { +#ifdef VOXELENGINE_SDBUS + if (impl->bus != nullptr) { + return true; + } + if (sd_bus_default_user(&impl->bus) < 0) { + impl->bus = nullptr; + return false; + } + sd_bus_match_signal( + impl->bus, nullptr, nullptr, PORTAL_PATH, PORTAL_INTERFACE, + "SettingChanged", &Impl::onSettingChanged, impl + ); + return true; +#else + return false; +#endif +} + +void SettingsWatcher::close() { +#ifdef VOXELENGINE_SDBUS + if (impl->bus != nullptr) { + sd_bus_flush_close_unref(impl->bus); + impl->bus = nullptr; + } +#endif +} + +bool SettingsWatcher::isAvailable() const { +#ifdef VOXELENGINE_SDBUS + return impl->bus != nullptr; +#else + return false; +#endif +} + +bool SettingsWatcher::read( + const char* schema, const char* key, std::string& value +) { +#ifdef VOXELENGINE_SDBUS + if (impl->bus == nullptr) { + return false; + } + sd_bus_error error = SD_BUS_ERROR_NULL; + sd_bus_message* reply = nullptr; + const int result = sd_bus_call_method( + impl->bus, PORTAL_SERVICE, PORTAL_PATH, PORTAL_INTERFACE, "Read", + &error, &reply, "ss", schema, key + ); + bool success = false; + if (result >= 0) { + if (sd_bus_message_enter_container(reply, 'v', nullptr) >= 0) { + const std::string text = readVariant(reply); + if (!text.empty()) { + value = text; + success = true; + } + sd_bus_message_exit_container(reply); + } + } + if (reply != nullptr) { + sd_bus_message_unref(reply); + } + sd_bus_error_free(&error); + return success; +#else + return false; +#endif +} + +bool SettingsWatcher::poll( + const char* schema, const char* key, std::string& value +) { +#ifdef VOXELENGINE_SDBUS + if (impl->bus == nullptr) { + open(); + return false; + } + sd_bus_message* message = nullptr; + int result = 0; + while ((result = sd_bus_process(impl->bus, &message)) > 0) { + if (message != nullptr) { + sd_bus_message_unref(message); + message = nullptr; + } + } + if (!impl->changed || impl->changedKey != key) { + return false; + } + impl->changed = false; + value = impl->changedValue; + (void)schema; + (void)key; + return true; +#else + return false; +#endif +} diff --git a/src/window/wayland/WaylandSettings.hpp b/src/window/wayland/WaylandSettings.hpp new file mode 100644 index 000000000..30012e9e3 --- /dev/null +++ b/src/window/wayland/WaylandSettings.hpp @@ -0,0 +1,23 @@ +#pragma once + +#include + +class SettingsWatcher { +public: + SettingsWatcher(); + ~SettingsWatcher(); + + bool isAvailable() const; + + bool read(const char* schema, const char* key, std::string& value); + + bool poll(const char* schema, const char* key, std::string& value); + +private: + bool open(); + void close(); + bool handleMessage(const char* schema, const char* key, std::string& value); + + struct Impl; + Impl* impl = nullptr; +}; diff --git a/src/window/wayland/WaylandWindow.cpp b/src/window/wayland/WaylandWindow.cpp index 5a69e7a1a..3c4748d8d 100644 --- a/src/window/wayland/WaylandWindow.cpp +++ b/src/window/wayland/WaylandWindow.cpp @@ -1,5 +1,9 @@ #include "window/wayland/WaylandWindow.hpp" +#include +#include +#include + #include "settings.hpp" #include "graphics/core/Texture.hpp" #include "util/platform.hpp" @@ -7,7 +11,11 @@ WaylandWindow::WaylandWindow( WaylandInput& input, DisplaySettings* settings, int width, int height ) - : Window({width, height}), input(input), settings(settings) { + : Window({width, height}), + input(input), + settings(settings), + initialWidth(width), + initialHeight(height) { scissorArea = glm::vec4(0.0f, 0.0f, size.x, size.y); } @@ -184,9 +192,10 @@ void WaylandWindow::setMode(WindowMode mode) { break; case WindowMode::WINDOWED: setFullscreen(false); - setMaximized(false); + if (maximized) { + setMaximized(false); + } setIdleInhibit(false); - resize(settings->width.get(), settings->height.get()); break; } wl_surface_commit(state.surface); @@ -238,6 +247,11 @@ void WaylandWindow::resize(int width, int height) { } glViewport(0, 0, width, height); size = {width, height}; + if (Window::mode == WindowMode::WINDOWED && !maximized) { + settings->width.set(width); + settings->height.set(height); + } + updateBarGeometry(); scissorArea = glm::vec4(0.0f, 0.0f, size.x, size.y); } @@ -264,11 +278,19 @@ void WaylandWindow::onConfigure(int32_t width, int32_t height, wl_array* states) } void WaylandWindow::applyConfigure() { - if (pendingWidth > 0 && pendingHeight > 0) { - resize(pendingWidth, pendingHeight); - } maximized = stateMaximized; suspended = stateSuspended; + if (pendingWidth > 0 && pendingHeight > 0) { + if (!configured && Window::mode == WindowMode::WINDOWED && !maximized && + initialWidth > 0 && initialHeight > 0) { + pendingWidth = initialWidth; + pendingHeight = initialHeight; + } + resize(pendingWidth, pendingHeight); + } + if (stateActivated && !activated) { + lastLayoutCheck = 0.0; + } activated = stateActivated; configured = true; setShouldRefresh(); @@ -339,10 +361,10 @@ std::unique_ptr WaylandWindow::takeScreenshot() { auto data = std::make_unique(size.x * size.y * 3); glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, size.x, size.y, GL_RGB, GL_UNSIGNED_BYTE, data.get()); + glReadBuffer(static_cast(readBuffer)); return std::make_unique( ImageFormat::RGB888, size.x, size.y, data.release() ); - glReadBuffer(static_cast(readBuffer)); } void WaylandWindow::setFramerate(int framerate) { @@ -471,6 +493,7 @@ void WaylandWindow::requestFrame() { } void WaylandWindow::paceFrame() { + updateButtonLayout(); frameOnCallback = frameDone; if (lastSwapEnd > 0.0) { const double cost = now() - lastSwapEnd; diff --git a/src/window/wayland/WaylandWindow.hpp b/src/window/wayland/WaylandWindow.hpp index aadfd9fb2..6186b9433 100644 --- a/src/window/wayland/WaylandWindow.hpp +++ b/src/window/wayland/WaylandWindow.hpp @@ -1,6 +1,9 @@ #pragma once #include "window/wayland/WaylandCommon.hpp" + +#include +#include "window/wayland/WaylandSettings.hpp" #include "window/wayland/WaylandInput.hpp" #include "graphics/core/ImageData.hpp" @@ -37,7 +40,25 @@ 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 leftButtons; + std::vector rightButtons; + std::vector 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 barDragging = false; +int barPressedButton = -1; +int initialWidth = 0; +int initialHeight = 0; double pointerX = 0.0; double pointerY = 0.0; bool frameOnCallback = false; @@ -89,6 +110,23 @@ glm::vec4 scissorArea {}; int getDecorationHeight() const override; int getDecorationHoveredButton() const override; + + + + static constexpr int BAR_HEIGHT = 30; + static constexpr int BAR_BUTTON = 30; + static constexpr int BAR_EDGE = 8; + + int decorationOffset() const { + return barVisible() ? BAR_HEIGHT : 0; + } + + const std::vector& getDecorationButtons() const override; + + void updateButtonLayout(); + + void updateBarGeometry(); + bool isIconSupported() const override { return false; } @@ -115,7 +153,8 @@ glm::vec4 scissorArea {}; uint32_t barEdgeAt(double x, double y) const; static CursorShape edgeCursor(uint32_t edges); bool handleBarMotion(double x, double y); - bool handleBarButton(uint32_t serial, bool pressed); + bool handleBarButton(uint32_t serial, int button, bool pressed); + bool handleBarScroll() const; void onPointerLeave(); void onFrameDone(); void requestFrame();