Skip to content

Commit

Permalink
Resolve some warnings from clang-tidy and Resharper, improve memory h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
serebit committed Dec 14, 2024
1 parent 88be465 commit 140c6a1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
41 changes: 30 additions & 11 deletions src/decorations/ssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "surface/view.hpp"
#include "server.hpp"

#include "wlr-wrap-start.hpp"
#include <wlr/util/log.h>
#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;
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -29,7 +33,7 @@ static wlr_box extents_dimensions(wlr_box& view_dimensions) {
};
}

static constexpr std::array<float, 4> rrggbb_to_floats(uint32_t rrggbb) {
static constexpr std::array<float, 4> rrggbb_to_floats(const uint32_t rrggbb) {
return std::array<float, 4>(
{(float) (rrggbb >> 16 & 0xff) / 255.0f, (float) (rrggbb >> 8 & 0xff) / 255.0f, (float) (rrggbb & 0xff) / 255.0f, 1.0});
}
Expand All @@ -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<float, 4>({0.0f, 0.0f, 0.0f, 0.0f});
constexpr auto extents_color = std::array<float, 4>({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);
Expand All @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/input/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<View>(magpie_surface);
const auto view = std::dynamic_pointer_cast<View>(magpie_surface);

/* Focus that client if the button was _pressed_ */
server.focus_view(std::dynamic_pointer_cast<View>(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) {
Expand Down
2 changes: 1 addition & 1 deletion src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void Server::focus_layer(const std::shared_ptr<Layer>& 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<SceneRectData*>(node->data)->type;
Expand Down
10 changes: 5 additions & 5 deletions src/surface/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 140c6a1

Please sign in to comment.