Skip to content

Commit 4406c96

Browse files
committed
Resolve some warnings from clang-tidy and Resharper, improve memory handling
1 parent bda35e2 commit 4406c96

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

src/decorations/ssd.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include "surface/view.hpp"
44
#include "server.hpp"
55

6+
#include "wlr-wrap-start.hpp"
7+
#include <wlr/util/log.h>
8+
#include "wlr-wrap-end.hpp"
9+
610
constexpr uint8_t TITLEBAR_HEIGHT = 24;
711
constexpr uint32_t TITLEBAR_ACTIVE_COLOR = 0x303030;
812
constexpr uint32_t TITLEBAR_INACTIVE_COLOR = 0x202020;
@@ -11,7 +15,7 @@ constexpr uint8_t EXTENTS_WIDTH = 12;
1115
constexpr uint32_t BORDER_ACTIVE_COLOR = 0x505050;
1216
constexpr uint32_t BORDER_INACTIVE_COLOR = 0x404040;
1317

14-
static wlr_box border_dimensions(wlr_box& view_dimensions) {
18+
static wlr_box border_dimensions(const wlr_box& view_dimensions) {
1519
return {
1620
.x = EXTENTS_WIDTH,
1721
.y = EXTENTS_WIDTH,
@@ -20,7 +24,7 @@ static wlr_box border_dimensions(wlr_box& view_dimensions) {
2024
};
2125
}
2226

23-
static wlr_box extents_dimensions(wlr_box& view_dimensions) {
27+
static wlr_box extents_dimensions(const wlr_box& view_dimensions) {
2428
return {
2529
.x = 0,
2630
.y = 0,
@@ -29,7 +33,7 @@ static wlr_box extents_dimensions(wlr_box& view_dimensions) {
2933
};
3034
}
3135

32-
static constexpr std::array<float, 4> rrggbb_to_floats(uint32_t rrggbb) {
36+
static constexpr std::array<float, 4> rrggbb_to_floats(const uint32_t rrggbb) {
3337
return std::array<float, 4>(
3438
{(float) (rrggbb >> 16 & 0xff) / 255.0f, (float) (rrggbb >> 8 & 0xff) / 255.0f, (float) (rrggbb & 0xff) / 255.0f, 1.0});
3539
}
@@ -40,26 +44,41 @@ Ssd::Ssd(View& parent) noexcept : view(parent) {
4044
wlr_scene_node_set_position(&scene_tree->node, 0, 0);
4145
wlr_scene_node_set_enabled(&scene_tree->node, true);
4246

43-
auto titlebar_color = rrggbb_to_floats(TITLEBAR_INACTIVE_COLOR);
47+
constexpr auto titlebar_color = rrggbb_to_floats(TITLEBAR_INACTIVE_COLOR);
4448
auto view_geo = view.get_surface_geometry();
4549
titlebar_rect = wlr_scene_rect_create(scene_tree, view_geo.width, TITLEBAR_HEIGHT, titlebar_color.data());
46-
titlebar_rect->node.data = new SceneRectData{.type = SceneRectType::TITLEBAR, .parent = &parent};
50+
try {
51+
titlebar_rect->node.data = new SceneRectData{.type = SceneRectType::TITLEBAR, .parent = &parent};
52+
} catch ([[maybe_unused]] std::bad_alloc& ex) {
53+
wlr_log(WLR_ERROR, "Failed to allocate memory for window decoration titlebar");
54+
exit(EXIT_FAILURE);
55+
}
4756
wlr_scene_node_set_position(&titlebar_rect->node, BORDER_WIDTH + EXTENTS_WIDTH, BORDER_WIDTH + EXTENTS_WIDTH);
4857
wlr_scene_node_lower_to_bottom(&titlebar_rect->node);
4958
wlr_scene_node_set_enabled(&titlebar_rect->node, true);
5059

51-
auto extents_color = std::array<float, 4>({0.0f, 0.0f, 0.0f, 0.0f});
60+
constexpr auto extents_color = std::array<float, 4>({0.0f, 0.0f, 0.0f, 0.0f});
5261
auto extents_box = extents_dimensions(view_geo);
5362
extents_rect = wlr_scene_rect_create(scene_tree, extents_box.width, extents_box.height, extents_color.data());
54-
extents_rect->node.data = new SceneRectData{.type = SceneRectType::EXTENTS, .parent = &parent};
63+
try {
64+
extents_rect->node.data = new SceneRectData{.type = SceneRectType::EXTENTS, .parent = &parent};
65+
} catch ([[maybe_unused]] std::bad_alloc& ex) {
66+
wlr_log(WLR_ERROR, "Failed to allocate memory for window decoration extents");
67+
exit(EXIT_FAILURE);
68+
}
5569
wlr_scene_node_set_position(&extents_rect->node, extents_box.x, extents_box.y);
5670
wlr_scene_node_lower_to_bottom(&extents_rect->node);
5771
wlr_scene_node_set_enabled(&extents_rect->node, true);
5872

59-
auto border_color = rrggbb_to_floats(BORDER_INACTIVE_COLOR);
73+
constexpr auto border_color = rrggbb_to_floats(BORDER_INACTIVE_COLOR);
6074
auto border_box = border_dimensions(view_geo);
6175
border_rect = wlr_scene_rect_create(scene_tree, border_box.width, border_box.height, border_color.data());
62-
border_rect->node.data = new SceneRectData{.type = SceneRectType::BORDER, .parent = &parent};
76+
try {
77+
border_rect->node.data = new SceneRectData{.type = SceneRectType::BORDER, .parent = &parent};
78+
} catch ([[maybe_unused]] std::bad_alloc& ex) {
79+
wlr_log(WLR_ERROR, "Failed to allocate memory for window decoration border");
80+
exit(EXIT_FAILURE);
81+
}
6382
wlr_scene_node_set_position(&border_rect->node, border_box.x, border_box.y);
6483
wlr_scene_node_lower_to_bottom(&border_rect->node);
6584
wlr_scene_node_set_enabled(&border_rect->node, true);
@@ -76,10 +95,10 @@ void Ssd::update() const {
7695
auto view_geo = view.surface_current;
7796
wlr_scene_rect_set_size(titlebar_rect, view_geo.width, TITLEBAR_HEIGHT);
7897

79-
auto border_box = border_dimensions(view_geo);
98+
const auto border_box = border_dimensions(view_geo);
8099
wlr_scene_rect_set_size(border_rect, border_box.width, border_box.height);
81100

82-
auto extents_box = extents_dimensions(view_geo);
101+
const auto extents_box = extents_dimensions(view_geo);
83102
wlr_scene_rect_set_size(extents_rect, extents_box.width, extents_box.height);
84103
}
85104

src/input/cursor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,16 @@ static void cursor_button_notify(wl_listener* listener, void* data) {
193193
cursor.reset_mode();
194194
}
195195
} else if (magpie_surface != nullptr && magpie_surface->is_view()) {
196-
auto view = std::dynamic_pointer_cast<View>(magpie_surface);
196+
const auto view = std::dynamic_pointer_cast<View>(magpie_surface);
197197

198198
/* Focus that client if the button was _pressed_ */
199199
server.focus_view(std::dynamic_pointer_cast<View>(magpie_surface));
200200

201-
auto ssd_at_cursor = server.ssd_at(cursor.wlr.x, cursor.wlr.y);
201+
const auto ssd_at_cursor = server.ssd_at(cursor.wlr.x, cursor.wlr.y);
202202
if (ssd_at_cursor == SceneRectType::TITLEBAR) {
203203
view->begin_interactive(MAGPIE_CURSOR_MOVE, 0);
204204
} else if (ssd_at_cursor == SceneRectType::BORDER || ssd_at_cursor == SceneRectType::EXTENTS) {
205-
auto surface_geo = view->surface_current;
205+
const auto surface_geo = view->surface_current;
206206

207207
uint8_t edges = WLR_EDGE_NONE;
208208
if (cursor.wlr.x < surface_geo.x) {

src/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void Server::focus_layer(const std::shared_ptr<Layer>& layer) {
136136
SceneRectType Server::ssd_at(const double lx, const double ly) const {
137137
double sx;
138138
double sy;
139-
wlr_scene_node* node = wlr_scene_node_at(&scene->tree.node, lx, ly, &sx, &sy);
139+
const wlr_scene_node* node = wlr_scene_node_at(&scene->tree.node, lx, ly, &sx, &sy);
140140

141141
if (node != nullptr && node->type == WLR_SCENE_NODE_RECT && node->data != nullptr) {
142142
return static_cast<SceneRectData*>(node->data)->type;

src/surface/view.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ int32_t View::find_surface_min_y() const {
9999
min_y = min_y == INT32_MAX ? 0 : min_y;
100100
if (ssd.has_value()) {
101101
return min_y + ssd->get_visual_vertical_offset();
102-
} else {
103-
return min_y;
104102
}
103+
104+
return min_y;
105105
}
106106

107107
void View::begin_interactive(const CursorMode mode, const uint32_t edges) {
108108
Server& server = get_server();
109109

110110
Cursor& cursor = server.seat->cursor;
111-
auto focused_view = server.focused_view.lock();
111+
const auto focused_view = server.focused_view.lock();
112112

113113
if (focused_view == nullptr || get_wlr_surface() != wlr_surface_get_root_surface(focused_view->get_wlr_surface())) {
114114
/* Deny move/resize requests from unfocused clients. */
@@ -422,7 +422,7 @@ wlr_box View::get_geometry_with_decorations() const {
422422
}
423423

424424
wlr_box View::get_min_size_with_decorations() const {
425-
auto surface_min_size = get_surface_min_size();
425+
const auto surface_min_size = get_surface_min_size();
426426
if (ssd.has_value()) {
427427
return {.x = 0,
428428
.y = 0,
@@ -434,7 +434,7 @@ wlr_box View::get_min_size_with_decorations() const {
434434
}
435435

436436
wlr_box View::get_max_size_with_decorations() const {
437-
auto surface_max_size = get_surface_max_size();
437+
const auto surface_max_size = get_surface_max_size();
438438
if (ssd.has_value()) {
439439
return {.x = 0,
440440
.y = 0,

0 commit comments

Comments
 (0)