From 4406c9665de6b4a4dc1e0b50c363fe58f5535dd6 Mon Sep 17 00:00:00 2001 From: Campbell Jones Date: Fri, 13 Dec 2024 19:18:59 -0500 Subject: [PATCH] Resolve some warnings from clang-tidy and Resharper, improve memory handling --- src/decorations/ssd.cpp | 41 ++++++++++++++++++++++++++++++----------- src/input/cursor.cpp | 6 +++--- src/server.cpp | 2 +- src/surface/view.cpp | 10 +++++----- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/decorations/ssd.cpp b/src/decorations/ssd.cpp index 9283c37b6..209d460cc 100644 --- a/src/decorations/ssd.cpp +++ b/src/decorations/ssd.cpp @@ -3,6 +3,10 @@ #include "surface/view.hpp" #include "server.hpp" +#include "wlr-wrap-start.hpp" +#include +#include "wlr-wrap-end.hpp" + constexpr uint8_t TITLEBAR_HEIGHT = 24; constexpr uint32_t TITLEBAR_ACTIVE_COLOR = 0x303030; constexpr uint32_t TITLEBAR_INACTIVE_COLOR = 0x202020; @@ -11,7 +15,7 @@ constexpr uint8_t EXTENTS_WIDTH = 12; constexpr uint32_t BORDER_ACTIVE_COLOR = 0x505050; constexpr uint32_t BORDER_INACTIVE_COLOR = 0x404040; -static wlr_box border_dimensions(wlr_box& view_dimensions) { +static wlr_box border_dimensions(const wlr_box& view_dimensions) { return { .x = EXTENTS_WIDTH, .y = EXTENTS_WIDTH, @@ -20,7 +24,7 @@ static wlr_box border_dimensions(wlr_box& view_dimensions) { }; } -static wlr_box extents_dimensions(wlr_box& view_dimensions) { +static wlr_box extents_dimensions(const wlr_box& view_dimensions) { return { .x = 0, .y = 0, @@ -29,7 +33,7 @@ static wlr_box extents_dimensions(wlr_box& view_dimensions) { }; } -static constexpr std::array rrggbb_to_floats(uint32_t rrggbb) { +static constexpr std::array rrggbb_to_floats(const uint32_t rrggbb) { return std::array( {(float) (rrggbb >> 16 & 0xff) / 255.0f, (float) (rrggbb >> 8 & 0xff) / 255.0f, (float) (rrggbb & 0xff) / 255.0f, 1.0}); } @@ -40,26 +44,41 @@ Ssd::Ssd(View& parent) noexcept : view(parent) { wlr_scene_node_set_position(&scene_tree->node, 0, 0); wlr_scene_node_set_enabled(&scene_tree->node, true); - auto titlebar_color = rrggbb_to_floats(TITLEBAR_INACTIVE_COLOR); + constexpr auto titlebar_color = rrggbb_to_floats(TITLEBAR_INACTIVE_COLOR); auto view_geo = view.get_surface_geometry(); titlebar_rect = wlr_scene_rect_create(scene_tree, view_geo.width, TITLEBAR_HEIGHT, titlebar_color.data()); - titlebar_rect->node.data = new SceneRectData{.type = SceneRectType::TITLEBAR, .parent = &parent}; + try { + titlebar_rect->node.data = new SceneRectData{.type = SceneRectType::TITLEBAR, .parent = &parent}; + } catch ([[maybe_unused]] std::bad_alloc& ex) { + wlr_log(WLR_ERROR, "Failed to allocate memory for window decoration titlebar"); + exit(EXIT_FAILURE); + } wlr_scene_node_set_position(&titlebar_rect->node, BORDER_WIDTH + EXTENTS_WIDTH, BORDER_WIDTH + EXTENTS_WIDTH); wlr_scene_node_lower_to_bottom(&titlebar_rect->node); wlr_scene_node_set_enabled(&titlebar_rect->node, true); - auto extents_color = std::array({0.0f, 0.0f, 0.0f, 0.0f}); + constexpr auto extents_color = std::array({0.0f, 0.0f, 0.0f, 0.0f}); auto extents_box = extents_dimensions(view_geo); extents_rect = wlr_scene_rect_create(scene_tree, extents_box.width, extents_box.height, extents_color.data()); - extents_rect->node.data = new SceneRectData{.type = SceneRectType::EXTENTS, .parent = &parent}; + try { + extents_rect->node.data = new SceneRectData{.type = SceneRectType::EXTENTS, .parent = &parent}; + } catch ([[maybe_unused]] std::bad_alloc& ex) { + wlr_log(WLR_ERROR, "Failed to allocate memory for window decoration extents"); + exit(EXIT_FAILURE); + } wlr_scene_node_set_position(&extents_rect->node, extents_box.x, extents_box.y); wlr_scene_node_lower_to_bottom(&extents_rect->node); wlr_scene_node_set_enabled(&extents_rect->node, true); - auto border_color = rrggbb_to_floats(BORDER_INACTIVE_COLOR); + constexpr auto border_color = rrggbb_to_floats(BORDER_INACTIVE_COLOR); auto border_box = border_dimensions(view_geo); border_rect = wlr_scene_rect_create(scene_tree, border_box.width, border_box.height, border_color.data()); - border_rect->node.data = new SceneRectData{.type = SceneRectType::BORDER, .parent = &parent}; + try { + border_rect->node.data = new SceneRectData{.type = SceneRectType::BORDER, .parent = &parent}; + } catch ([[maybe_unused]] std::bad_alloc& ex) { + wlr_log(WLR_ERROR, "Failed to allocate memory for window decoration border"); + exit(EXIT_FAILURE); + } wlr_scene_node_set_position(&border_rect->node, border_box.x, border_box.y); wlr_scene_node_lower_to_bottom(&border_rect->node); wlr_scene_node_set_enabled(&border_rect->node, true); @@ -76,10 +95,10 @@ void Ssd::update() const { auto view_geo = view.surface_current; wlr_scene_rect_set_size(titlebar_rect, view_geo.width, TITLEBAR_HEIGHT); - auto border_box = border_dimensions(view_geo); + const auto border_box = border_dimensions(view_geo); wlr_scene_rect_set_size(border_rect, border_box.width, border_box.height); - auto extents_box = extents_dimensions(view_geo); + const auto extents_box = extents_dimensions(view_geo); wlr_scene_rect_set_size(extents_rect, extents_box.width, extents_box.height); } diff --git a/src/input/cursor.cpp b/src/input/cursor.cpp index f1d157516..d2f45082d 100644 --- a/src/input/cursor.cpp +++ b/src/input/cursor.cpp @@ -193,16 +193,16 @@ static void cursor_button_notify(wl_listener* listener, void* data) { cursor.reset_mode(); } } else if (magpie_surface != nullptr && magpie_surface->is_view()) { - auto view = std::dynamic_pointer_cast(magpie_surface); + const auto view = std::dynamic_pointer_cast(magpie_surface); /* Focus that client if the button was _pressed_ */ server.focus_view(std::dynamic_pointer_cast(magpie_surface)); - auto ssd_at_cursor = server.ssd_at(cursor.wlr.x, cursor.wlr.y); + const auto ssd_at_cursor = server.ssd_at(cursor.wlr.x, cursor.wlr.y); if (ssd_at_cursor == SceneRectType::TITLEBAR) { view->begin_interactive(MAGPIE_CURSOR_MOVE, 0); } else if (ssd_at_cursor == SceneRectType::BORDER || ssd_at_cursor == SceneRectType::EXTENTS) { - auto surface_geo = view->surface_current; + const auto surface_geo = view->surface_current; uint8_t edges = WLR_EDGE_NONE; if (cursor.wlr.x < surface_geo.x) { diff --git a/src/server.cpp b/src/server.cpp index 9fab0c09b..17f15a535 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -136,7 +136,7 @@ void Server::focus_layer(const std::shared_ptr& layer) { SceneRectType Server::ssd_at(const double lx, const double ly) const { double sx; double sy; - wlr_scene_node* node = wlr_scene_node_at(&scene->tree.node, lx, ly, &sx, &sy); + const wlr_scene_node* node = wlr_scene_node_at(&scene->tree.node, lx, ly, &sx, &sy); if (node != nullptr && node->type == WLR_SCENE_NODE_RECT && node->data != nullptr) { return static_cast(node->data)->type; diff --git a/src/surface/view.cpp b/src/surface/view.cpp index 9db016c2e..0b1edac74 100644 --- a/src/surface/view.cpp +++ b/src/surface/view.cpp @@ -99,16 +99,16 @@ int32_t View::find_surface_min_y() const { min_y = min_y == INT32_MAX ? 0 : min_y; if (ssd.has_value()) { return min_y + ssd->get_visual_vertical_offset(); - } else { - return min_y; } + + return min_y; } void View::begin_interactive(const CursorMode mode, const uint32_t edges) { Server& server = get_server(); Cursor& cursor = server.seat->cursor; - auto focused_view = server.focused_view.lock(); + const auto focused_view = server.focused_view.lock(); if (focused_view == nullptr || get_wlr_surface() != wlr_surface_get_root_surface(focused_view->get_wlr_surface())) { /* Deny move/resize requests from unfocused clients. */ @@ -422,7 +422,7 @@ wlr_box View::get_geometry_with_decorations() const { } wlr_box View::get_min_size_with_decorations() const { - auto surface_min_size = get_surface_min_size(); + const auto surface_min_size = get_surface_min_size(); if (ssd.has_value()) { return {.x = 0, .y = 0, @@ -434,7 +434,7 @@ wlr_box View::get_min_size_with_decorations() const { } wlr_box View::get_max_size_with_decorations() const { - auto surface_max_size = get_surface_max_size(); + const auto surface_max_size = get_surface_max_size(); if (ssd.has_value()) { return {.x = 0, .y = 0,