Skip to content

Commit 718fc42

Browse files
committed
refactor(client): remove unnecessary std::unique_ptr<struct waybar_output>.
Should fix Alexays#554, where unique_ptr was moved out from the outputs_ vector.
1 parent b9cd51a commit 718fc42

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

include/client.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class Client {
3030
const std::string &style) const;
3131
void bindInterfaces();
3232
const std::string getValidPath(const std::vector<std::string> &paths) const;
33-
void handleOutput(std::unique_ptr<struct waybar_output> &output);
34-
bool isValidOutput(const Json::Value &config, std::unique_ptr<struct waybar_output> &output);
33+
void handleOutput(struct waybar_output &output);
34+
bool isValidOutput(const Json::Value &config, struct waybar_output &output);
3535
auto setupConfig(const std::string &config_file) -> void;
3636
auto setupCss(const std::string &css_file) -> void;
37-
std::unique_ptr<struct waybar_output> &getOutput(void *);
38-
std::vector<Json::Value> getOutputConfigs(std::unique_ptr<struct waybar_output> &output);
37+
struct waybar_output &getOutput(void *);
38+
std::vector<Json::Value> getOutputConfigs(struct waybar_output &output);
3939

4040
static void handleGlobal(void *data, struct wl_registry *registry, uint32_t name,
4141
const char *interface, uint32_t version);
@@ -44,10 +44,10 @@ class Client {
4444
void handleMonitorAdded(Glib::RefPtr<Gdk::Monitor> monitor);
4545
void handleMonitorRemoved(Glib::RefPtr<Gdk::Monitor> monitor);
4646

47-
Json::Value config_;
48-
Glib::RefPtr<Gtk::StyleContext> style_context_;
49-
Glib::RefPtr<Gtk::CssProvider> css_provider_;
50-
std::vector<std::unique_ptr<struct waybar_output>> outputs_;
47+
Json::Value config_;
48+
Glib::RefPtr<Gtk::StyleContext> style_context_;
49+
Glib::RefPtr<Gtk::CssProvider> css_provider_;
50+
std::vector<struct waybar_output> outputs_;
5151
};
5252

5353
} // namespace waybar

src/client.cpp

+19-21
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void waybar::Client::handleGlobalRemove(void * data, struct wl_registry * /*re
4949
// Nothing here
5050
}
5151

52-
void waybar::Client::handleOutput(std::unique_ptr<struct waybar_output> &output) {
52+
void waybar::Client::handleOutput(struct waybar_output &output) {
5353
static const struct zxdg_output_v1_listener xdgOutputListener = {
5454
.logical_position = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {},
5555
.logical_size = [](void *, struct zxdg_output_v1 *, int32_t, int32_t) {},
@@ -58,42 +58,39 @@ void waybar::Client::handleOutput(std::unique_ptr<struct waybar_output> &output)
5858
.description = [](void *, struct zxdg_output_v1 *, const char *) {},
5959
};
6060
// owned by output->monitor; no need to destroy
61-
auto wl_output = gdk_wayland_monitor_get_wl_output(output->monitor->gobj());
62-
output->xdg_output.reset(zxdg_output_manager_v1_get_xdg_output(xdg_output_manager, wl_output));
63-
zxdg_output_v1_add_listener(output->xdg_output.get(), &xdgOutputListener, output.get());
61+
auto wl_output = gdk_wayland_monitor_get_wl_output(output.monitor->gobj());
62+
output.xdg_output.reset(zxdg_output_manager_v1_get_xdg_output(xdg_output_manager, wl_output));
63+
zxdg_output_v1_add_listener(output.xdg_output.get(), &xdgOutputListener, &output);
6464
}
6565

66-
bool waybar::Client::isValidOutput(const Json::Value & config,
67-
std::unique_ptr<struct waybar_output> &output) {
66+
bool waybar::Client::isValidOutput(const Json::Value &config, struct waybar_output &output) {
6867
bool found = true;
6968
if (config["output"].isArray()) {
7069
bool in_array = false;
7170
for (auto const &output_conf : config["output"]) {
72-
if (output_conf.isString() && output_conf.asString() == output->name) {
71+
if (output_conf.isString() && output_conf.asString() == output.name) {
7372
in_array = true;
7473
break;
7574
}
7675
}
7776
found = in_array;
7877
}
79-
if (config["output"].isString() && config["output"].asString() != output->name) {
78+
if (config["output"].isString() && config["output"].asString() != output.name) {
8079
found = false;
8180
}
8281
return found;
8382
}
8483

85-
std::unique_ptr<struct waybar::waybar_output> &waybar::Client::getOutput(void *addr) {
86-
auto it = std::find_if(outputs_.begin(), outputs_.end(), [&addr](const auto &output) {
87-
return output.get() == addr;
88-
});
84+
struct waybar::waybar_output &waybar::Client::getOutput(void *addr) {
85+
auto it = std::find_if(
86+
outputs_.begin(), outputs_.end(), [&addr](const auto &output) { return &output == addr; });
8987
if (it == outputs_.end()) {
9088
throw std::runtime_error("Unable to find valid output");
9189
}
9290
return *it;
9391
}
9492

95-
std::vector<Json::Value> waybar::Client::getOutputConfigs(
96-
std::unique_ptr<struct waybar_output> &output) {
93+
std::vector<Json::Value> waybar::Client::getOutputConfigs(struct waybar_output &output) {
9794
std::vector<Json::Value> configs;
9895
if (config_.isArray()) {
9996
for (auto const &config : config_) {
@@ -112,18 +109,18 @@ void waybar::Client::handleOutputName(void * data, struct zxdg_output_v1 *
112109
auto client = waybar::Client::inst();
113110
try {
114111
auto &output = client->getOutput(data);
115-
output->name = name;
112+
output.name = name;
116113
spdlog::debug("Output detected: {} ({} {})",
117114
name,
118-
output->monitor->get_manufacturer(),
119-
output->monitor->get_model());
115+
output.monitor->get_manufacturer(),
116+
output.monitor->get_model());
120117
auto configs = client->getOutputConfigs(output);
121118
if (configs.empty()) {
122-
output->xdg_output.reset();
119+
output.xdg_output.reset();
123120
} else {
124121
wl_display_roundtrip(client->wl_display);
125122
for (const auto &config : configs) {
126-
client->bars.emplace_back(std::make_unique<Bar>(output.get(), config));
123+
client->bars.emplace_back(std::make_unique<Bar>(&output, config));
127124
Glib::RefPtr<Gdk::Screen> screen = client->bars.back()->window.get_screen();
128125
client->style_context_->add_provider_for_screen(
129126
screen, client->css_provider_, GTK_STYLE_PROVIDER_PRIORITY_USER);
@@ -135,7 +132,8 @@ void waybar::Client::handleOutputName(void * data, struct zxdg_output_v1 *
135132
}
136133

137134
void waybar::Client::handleMonitorAdded(Glib::RefPtr<Gdk::Monitor> monitor) {
138-
auto &output = outputs_.emplace_back(new struct waybar_output({monitor}));
135+
auto &output = outputs_.emplace_back();
136+
output.monitor = monitor;
139137
handleOutput(output);
140138
}
141139

@@ -152,7 +150,7 @@ void waybar::Client::handleMonitorRemoved(Glib::RefPtr<Gdk::Monitor> monitor) {
152150
}
153151
}
154152
std::remove_if(outputs_.begin(), outputs_.end(), [&monitor](const auto &output) {
155-
return output->monitor == monitor;
153+
return output.monitor == monitor;
156154
});
157155
}
158156

0 commit comments

Comments
 (0)