refactor: kill custom modules scripts en destroy

This commit is contained in:
Alex
2019-04-23 15:56:38 +02:00
parent cccf60c30e
commit 90d89fe974
11 changed files with 107 additions and 35 deletions

View File

@ -32,21 +32,29 @@ waybar::ALabel::ALabel(const Json::Value& config, const std::string format, uint
}
}
waybar::ALabel::~ALabel() {
for (const auto &pid : pid_) {
if (pid != -1) {
kill(-pid, 9);
}
}
}
auto waybar::ALabel::update() -> void {
// Nothing here
}
bool waybar::ALabel::handleToggle(GdkEventButton* const& e) {
if (config_["on-click"].isString() && e->button == 1) {
waybar::util::command::forkExec(config_["on-click"].asString());
pid_.push_back(waybar::util::command::forkExec(config_["on-click"].asString()));
} else if (config_["on-click-middle"].isString() && e->button == 2) {
waybar::util::command::forkExec(config_["on-click-middle"].asString());
pid_.push_back(waybar::util::command::forkExec(config_["on-click-middle"].asString()));
} else if (config_["on-click-right"].isString() && e->button == 3) {
waybar::util::command::forkExec(config_["on-click-right"].asString());
pid_.push_back(waybar::util::command::forkExec(config_["on-click-right"].asString()));
} else if (config_["on-click-forward"].isString() && e->button == 8) {
waybar::util::command::forkExec(config_["on-click-backward"].asString());
pid_.push_back(waybar::util::command::forkExec(config_["on-click-backward"].asString()));
} else if (config_["on-click-backward"].isString() && e->button == 9) {
waybar::util::command::forkExec(config_["on-click-forward"].asString());
pid_.push_back(waybar::util::command::forkExec(config_["on-click-forward"].asString()));
}
if (config_["format-alt-click"].isUInt() && e->button == config_["format-alt-click"].asUInt()) {
alt_ = !alt_;
@ -82,9 +90,9 @@ bool waybar::ALabel::handleScroll(GdkEventScroll* e) {
}
}
if (direction_up && config_["on-scroll-up"].isString()) {
waybar::util::command::forkExec(config_["on-scroll-up"].asString());
pid_.push_back(waybar::util::command::forkExec(config_["on-scroll-up"].asString()));
} else if (config_["on-scroll-down"].isString()) {
waybar::util::command::forkExec(config_["on-scroll-down"].asString());
pid_.push_back(waybar::util::command::forkExec(config_["on-scroll-down"].asString()));
}
dp.emit();
return true;

View File

@ -1,7 +1,7 @@
#include "modules/custom.hpp"
waybar::modules::Custom::Custom(const std::string& name, const Json::Value& config)
: ALabel(config, "{}"), name_(name), fp_(nullptr) {
: ALabel(config, "{}"), name_(name), fp_(nullptr), pid_(-1) {
label_.set_name("custom-" + name_);
if (config_["exec"].isString()) {
if (interval_.count() > 0) {
@ -14,9 +14,9 @@ waybar::modules::Custom::Custom(const std::string& name, const Json::Value& conf
}
waybar::modules::Custom::~Custom() {
if (fp_) {
pclose(fp_);
fp_ = nullptr;
if (pid_ != -1) {
kill(-pid_, 9);
pid_ = -1;
}
}
@ -40,17 +40,18 @@ void waybar::modules::Custom::delayWorker() {
void waybar::modules::Custom::continuousWorker() {
auto cmd = config_["exec"].asString();
fp_ = popen(cmd.c_str(), "r");
pid_ = -1;
fp_ = util::command::open(cmd, pid_);
if (!fp_) {
throw std::runtime_error("Unable to open " + cmd);
}
thread_ = [this] {
thread_ = [&] {
char* buff = nullptr;
size_t len = 0;
if (getline(&buff, &len, fp_) == -1) {
int exit_code = 1;
if (fp_) {
exit_code = WEXITSTATUS(pclose(fp_));
exit_code = WEXITSTATUS(util::command::close(fp_, pid_));
fp_ = nullptr;
}
thread_.stop();

View File

@ -3,7 +3,11 @@
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
const Json::Value& config)
: ALabel(config, "{status}"), bar_(bar), status_("deactivated"), idle_inhibitor_(nullptr) {
: ALabel(config, "{status}"),
bar_(bar),
status_("deactivated"),
idle_inhibitor_(nullptr),
pid_(-1) {
label_.set_name("idle_inhibitor");
if (!id.empty()) {
label_.get_style_context()->add_class(id);
@ -19,6 +23,10 @@ waybar::modules::IdleInhibitor::~IdleInhibitor() {
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
idle_inhibitor_ = nullptr;
}
if (pid_ != -1) {
kill(-pid_, 9);
pid_ = -1;
}
}
auto waybar::modules::IdleInhibitor::update() -> void {
@ -43,7 +51,7 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
status_ = "activated";
}
if (config_["on-click"].isString() && e->button == 1) {
waybar::util::command::forkExec(config_["on-click"].asString());
pid_ = waybar::util::command::forkExec(config_["on-click"].asString());
}
} else {
ALabel::handleToggle(e);

View File

@ -104,7 +104,7 @@ struct Ipc::ipc_response Ipc::recv(int fd) {
}
total += res;
}
auto parsed = parser_.parse(&payload.front(), data32[0]);
auto parsed = parser_.parse(&payload.front());
return {data32[0], data32[1], parsed};
}