refactor: proper modules destruction

This commit is contained in:
Alexis
2018-12-26 11:13:36 +01:00
parent 8af813c2f1
commit 399f61df98
25 changed files with 284 additions and 216 deletions

View File

@ -1,11 +1,18 @@
#include "modules/sway/ipc/client.hpp"
waybar::modules::sway::Ipc::Ipc()
: fd_(-1), fd_event_(-1)
{}
{
const std::string& socketPath = getSocketPath();
fd_ = open(socketPath);
fd_event_ = open(socketPath);
}
waybar::modules::sway::Ipc::~Ipc()
{
// To fail the IPC header
write(fd_, "close-sway-ipc", 14);
write(fd_event_, "close-sway-ipc", 14);
close(fd_);
close(fd_event_);
}
@ -53,13 +60,6 @@ int waybar::modules::sway::Ipc::open(const std::string& socketPath) const
return fd;
}
void waybar::modules::sway::Ipc::connect()
{
const std::string& socketPath = getSocketPath();
fd_ = open(socketPath);
fd_event_ = open(socketPath);
}
struct waybar::modules::sway::Ipc::ipc_response
waybar::modules::sway::Ipc::recv(int fd) const
{
@ -71,18 +71,23 @@ struct waybar::modules::sway::Ipc::ipc_response
while (total < ipc_header_size_) {
auto res = ::recv(fd, header.data() + total, ipc_header_size_ - total, 0);
if (res <= 0) {
throw std::runtime_error("Unable to receive IPC response");
throw std::runtime_error("Unable to receive IPC header");
}
total += res;
}
auto magic = std::string(header.data(), header.data() + ipc_magic_.size());
if (magic != ipc_magic_) {
throw std::runtime_error("Invalid IPC magic");
}
total = 0;
std::string payload;
payload.reserve(data32[0] + 1);
while (total < data32[0]) {
auto res = ::recv(fd, payload.data() + total, data32[0] - total, 0);
if (res < 0) {
throw std::runtime_error("Unable to receive IPC response");
throw std::runtime_error("Unable to receive IPC payload");
}
total += res;
}

View File

@ -7,7 +7,6 @@ waybar::modules::sway::Mode::Mode(const std::string& id, const Bar& bar, const J
if (!id.empty()) {
label_.get_style_context()->add_class(id);
}
ipc_.connect();
ipc_.subscribe("[ \"mode\" ]");
// Launch worker
worker();
@ -20,14 +19,12 @@ void waybar::modules::sway::Mode::worker()
try {
auto res = ipc_.handleEvent();
auto parsed = parser_.parse(res.payload);
if ((parsed["change"]) != "default" ) {
if (parsed["change"] != "default") {
mode_ = parsed["change"].asString();
dp.emit();
}
else if ((parsed["change"]) == "default" ) {
} else {
mode_.clear();
dp.emit();
}
dp.emit();
} catch (const std::exception& e) {
std::cerr << "Mode: " << e.what() << std::endl;
}

View File

@ -11,7 +11,6 @@ waybar::modules::sway::Window::Window(const std::string& id, const Bar &bar, con
label_.set_hexpand(true);
label_.set_ellipsize(Pango::EllipsizeMode::ELLIPSIZE_END);
}
ipc_.connect();
ipc_.subscribe("[\"window\",\"workspace\"]");
getFocusedWindow();
// Launch worker

View File

@ -8,7 +8,6 @@ waybar::modules::sway::Workspaces::Workspaces(const std::string& id, const Bar&
if (!id.empty()) {
box_.get_style_context()->add_class(id);
}
ipc_.connect();
ipc_.subscribe("[ \"workspace\" ]");
// Launch worker
worker();
@ -21,9 +20,9 @@ void waybar::modules::sway::Workspaces::worker()
// Wait for the name of the output
if (!config_["all-outputs"].asBool() && bar_.output_name.empty()) {
while (bar_.output_name.empty()) {
thread_.sleep_for(chrono::milliseconds(150));
thread_.sleep_for(std::chrono::milliseconds(150));
}
} else if (thread_.isRunning() && !workspaces_.empty()) {
} else if (!workspaces_.empty()) {
ipc_.handleEvent();
}
{