mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
refactor: kill custom modules scripts en destroy
This commit is contained in:
parent
cccf60c30e
commit
90d89fe974
@ -11,7 +11,7 @@ namespace waybar {
|
|||||||
class ALabel : public IModule {
|
class ALabel : public IModule {
|
||||||
public:
|
public:
|
||||||
ALabel(const Json::Value &, const std::string format, uint16_t interval = 0);
|
ALabel(const Json::Value &, const std::string format, uint16_t interval = 0);
|
||||||
virtual ~ALabel() = default;
|
virtual ~ALabel();
|
||||||
virtual auto update() -> void;
|
virtual auto update() -> void;
|
||||||
virtual std::string getIcon(uint16_t, const std::string &alt = "");
|
virtual std::string getIcon(uint16_t, const std::string &alt = "");
|
||||||
virtual operator Gtk::Widget &();
|
virtual operator Gtk::Widget &();
|
||||||
@ -30,6 +30,9 @@ class ALabel : public IModule {
|
|||||||
|
|
||||||
virtual bool handleToggle(GdkEventButton *const &ev);
|
virtual bool handleToggle(GdkEventButton *const &ev);
|
||||||
virtual bool handleScroll(GdkEventScroll *);
|
virtual bool handleScroll(GdkEventScroll *);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<int> pid_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar
|
} // namespace waybar
|
||||||
|
@ -33,6 +33,7 @@ class Custom : public ALabel {
|
|||||||
waybar::util::command::res output_;
|
waybar::util::command::res output_;
|
||||||
waybar::util::JsonParser parser_;
|
waybar::util::JsonParser parser_;
|
||||||
FILE* fp_;
|
FILE* fp_;
|
||||||
|
int pid_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::modules
|
} // namespace waybar::modules
|
||||||
|
@ -19,6 +19,7 @@ class IdleInhibitor : public ALabel {
|
|||||||
const Bar& bar_;
|
const Bar& bar_;
|
||||||
std::string status_;
|
std::string status_;
|
||||||
struct zwp_idle_inhibitor_v1* idle_inhibitor_;
|
struct zwp_idle_inhibitor_v1* idle_inhibitor_;
|
||||||
|
int pid_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace waybar::modules
|
} // namespace waybar::modules
|
||||||
|
@ -11,12 +11,7 @@ struct res {
|
|||||||
std::string out;
|
std::string out;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline struct res exec(const std::string cmd) {
|
inline std::string read(FILE* fp) {
|
||||||
FILE* fp(popen(cmd.c_str(), "r"));
|
|
||||||
if (!fp) {
|
|
||||||
return {-1, ""};
|
|
||||||
}
|
|
||||||
|
|
||||||
std::array<char, 128> buffer = {0};
|
std::array<char, 128> buffer = {0};
|
||||||
std::string output;
|
std::string output;
|
||||||
while (feof(fp) == 0) {
|
while (feof(fp) == 0) {
|
||||||
@ -29,24 +24,75 @@ inline struct res exec(const std::string cmd) {
|
|||||||
if (!output.empty() && output[output.length() - 1] == '\n') {
|
if (!output.empty() && output[output.length() - 1] == '\n') {
|
||||||
output.erase(output.length() - 1);
|
output.erase(output.length() - 1);
|
||||||
}
|
}
|
||||||
int exit_code = WEXITSTATUS(pclose(fp));
|
return output;
|
||||||
return {exit_code, output};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool forkExec(std::string cmd) {
|
inline int close(FILE* fp, pid_t pid) {
|
||||||
if (cmd == "") return true;
|
int stat;
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
while (waitpid(pid, &stat, 0) == -1) {
|
||||||
|
if (errno != EINTR) {
|
||||||
|
stat = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stat;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline FILE* open(const std::string cmd, int& pid) {
|
||||||
|
if (cmd == "") return nullptr;
|
||||||
|
int fd[2];
|
||||||
|
pipe(fd);
|
||||||
|
|
||||||
|
pid_t child_pid = fork();
|
||||||
|
|
||||||
|
if (child_pid < 0) {
|
||||||
|
printf("Unable to exec cmd %s, error %s", cmd.c_str(), strerror(errno));
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!child_pid) {
|
||||||
|
::close(fd[0]);
|
||||||
|
dup2(fd[1], 1);
|
||||||
|
setpgid(child_pid, child_pid);
|
||||||
|
execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
||||||
|
exit(0);
|
||||||
|
} else {
|
||||||
|
::close(fd[1]);
|
||||||
|
}
|
||||||
|
pid = child_pid;
|
||||||
|
return fdopen(fd[0], "r");
|
||||||
|
}
|
||||||
|
|
||||||
|
inline struct res exec(std::string cmd) {
|
||||||
|
int pid;
|
||||||
|
auto fp = command::open(cmd, pid);
|
||||||
|
if (!fp) return {-1, ""};
|
||||||
|
auto output = command::read(fp);
|
||||||
|
auto stat = command::close(fp, pid);
|
||||||
|
return {WEXITSTATUS(stat), output};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int32_t forkExec(std::string cmd) {
|
||||||
|
if (cmd == "") return -1;
|
||||||
|
|
||||||
int32_t pid = fork();
|
int32_t pid = fork();
|
||||||
|
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
printf("Unable to exec cmd %s, error %s", cmd.c_str(), strerror(errno));
|
printf("Unable to exec cmd %s, error %s", cmd.c_str(), strerror(errno));
|
||||||
return false;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Child executes the command
|
// Child executes the command
|
||||||
if (!pid) execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
if (!pid) {
|
||||||
|
setpgid(pid, pid);
|
||||||
|
execl("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace waybar::util::command
|
} // namespace waybar::util::command
|
||||||
|
@ -7,14 +7,13 @@ namespace waybar::util {
|
|||||||
struct JsonParser {
|
struct JsonParser {
|
||||||
JsonParser() : reader_(builder_.newCharReader()) {}
|
JsonParser() : reader_(builder_.newCharReader()) {}
|
||||||
|
|
||||||
const Json::Value parse(const std::string& data, std::size_t size = 0) const {
|
const Json::Value parse(const std::string& data) const {
|
||||||
Json::Value root(Json::objectValue);
|
Json::Value root(Json::objectValue);
|
||||||
if (data.empty()) {
|
if (data.empty()) {
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
std::string err;
|
std::string err;
|
||||||
auto data_size = size > 0 ? size : data.size();
|
bool res = reader_->parse(data.c_str(), data.c_str() + data.size(), &root, &err);
|
||||||
bool res = reader_->parse(data.c_str(), data.c_str() + data_size, &root, &err);
|
|
||||||
if (!res) throw std::runtime_error(err);
|
if (!res) throw std::runtime_error(err);
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
@ -68,7 +68,8 @@
|
|||||||
"format-alt": "{:%Y-%m-%d}"
|
"format-alt": "{:%Y-%m-%d}"
|
||||||
},
|
},
|
||||||
"cpu": {
|
"cpu": {
|
||||||
"format": "{usage}% "
|
"format": "{usage}% ",
|
||||||
|
"tooltip": false
|
||||||
},
|
},
|
||||||
"memory": {
|
"memory": {
|
||||||
"format": "{}% "
|
"format": "{}% "
|
||||||
|
@ -74,6 +74,10 @@ window#waybar.hidded {
|
|||||||
animation-direction: alternate;
|
animation-direction: alternate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
label:focus {
|
||||||
|
background-color: #000000;
|
||||||
|
}
|
||||||
|
|
||||||
#cpu {
|
#cpu {
|
||||||
background: #2ecc71;
|
background: #2ecc71;
|
||||||
color: #000000;
|
color: #000000;
|
||||||
|
@ -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 {
|
auto waybar::ALabel::update() -> void {
|
||||||
// Nothing here
|
// Nothing here
|
||||||
}
|
}
|
||||||
|
|
||||||
bool waybar::ALabel::handleToggle(GdkEventButton* const& e) {
|
bool waybar::ALabel::handleToggle(GdkEventButton* const& e) {
|
||||||
if (config_["on-click"].isString() && e->button == 1) {
|
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) {
|
} 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) {
|
} 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) {
|
} 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) {
|
} 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()) {
|
if (config_["format-alt-click"].isUInt() && e->button == config_["format-alt-click"].asUInt()) {
|
||||||
alt_ = !alt_;
|
alt_ = !alt_;
|
||||||
@ -82,9 +90,9 @@ bool waybar::ALabel::handleScroll(GdkEventScroll* e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (direction_up && config_["on-scroll-up"].isString()) {
|
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()) {
|
} 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();
|
dp.emit();
|
||||||
return true;
|
return true;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include "modules/custom.hpp"
|
#include "modules/custom.hpp"
|
||||||
|
|
||||||
waybar::modules::Custom::Custom(const std::string& name, const Json::Value& config)
|
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_);
|
label_.set_name("custom-" + name_);
|
||||||
if (config_["exec"].isString()) {
|
if (config_["exec"].isString()) {
|
||||||
if (interval_.count() > 0) {
|
if (interval_.count() > 0) {
|
||||||
@ -14,9 +14,9 @@ waybar::modules::Custom::Custom(const std::string& name, const Json::Value& conf
|
|||||||
}
|
}
|
||||||
|
|
||||||
waybar::modules::Custom::~Custom() {
|
waybar::modules::Custom::~Custom() {
|
||||||
if (fp_) {
|
if (pid_ != -1) {
|
||||||
pclose(fp_);
|
kill(-pid_, 9);
|
||||||
fp_ = nullptr;
|
pid_ = -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,17 +40,18 @@ void waybar::modules::Custom::delayWorker() {
|
|||||||
|
|
||||||
void waybar::modules::Custom::continuousWorker() {
|
void waybar::modules::Custom::continuousWorker() {
|
||||||
auto cmd = config_["exec"].asString();
|
auto cmd = config_["exec"].asString();
|
||||||
fp_ = popen(cmd.c_str(), "r");
|
pid_ = -1;
|
||||||
|
fp_ = util::command::open(cmd, pid_);
|
||||||
if (!fp_) {
|
if (!fp_) {
|
||||||
throw std::runtime_error("Unable to open " + cmd);
|
throw std::runtime_error("Unable to open " + cmd);
|
||||||
}
|
}
|
||||||
thread_ = [this] {
|
thread_ = [&] {
|
||||||
char* buff = nullptr;
|
char* buff = nullptr;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
if (getline(&buff, &len, fp_) == -1) {
|
if (getline(&buff, &len, fp_) == -1) {
|
||||||
int exit_code = 1;
|
int exit_code = 1;
|
||||||
if (fp_) {
|
if (fp_) {
|
||||||
exit_code = WEXITSTATUS(pclose(fp_));
|
exit_code = WEXITSTATUS(util::command::close(fp_, pid_));
|
||||||
fp_ = nullptr;
|
fp_ = nullptr;
|
||||||
}
|
}
|
||||||
thread_.stop();
|
thread_.stop();
|
||||||
|
@ -3,7 +3,11 @@
|
|||||||
|
|
||||||
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
|
waybar::modules::IdleInhibitor::IdleInhibitor(const std::string& id, const Bar& bar,
|
||||||
const Json::Value& config)
|
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");
|
label_.set_name("idle_inhibitor");
|
||||||
if (!id.empty()) {
|
if (!id.empty()) {
|
||||||
label_.get_style_context()->add_class(id);
|
label_.get_style_context()->add_class(id);
|
||||||
@ -19,6 +23,10 @@ waybar::modules::IdleInhibitor::~IdleInhibitor() {
|
|||||||
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
zwp_idle_inhibitor_v1_destroy(idle_inhibitor_);
|
||||||
idle_inhibitor_ = nullptr;
|
idle_inhibitor_ = nullptr;
|
||||||
}
|
}
|
||||||
|
if (pid_ != -1) {
|
||||||
|
kill(-pid_, 9);
|
||||||
|
pid_ = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto waybar::modules::IdleInhibitor::update() -> void {
|
auto waybar::modules::IdleInhibitor::update() -> void {
|
||||||
@ -43,7 +51,7 @@ bool waybar::modules::IdleInhibitor::handleToggle(GdkEventButton* const& e) {
|
|||||||
status_ = "activated";
|
status_ = "activated";
|
||||||
}
|
}
|
||||||
if (config_["on-click"].isString() && e->button == 1) {
|
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 {
|
} else {
|
||||||
ALabel::handleToggle(e);
|
ALabel::handleToggle(e);
|
||||||
|
@ -104,7 +104,7 @@ struct Ipc::ipc_response Ipc::recv(int fd) {
|
|||||||
}
|
}
|
||||||
total += res;
|
total += res;
|
||||||
}
|
}
|
||||||
auto parsed = parser_.parse(&payload.front(), data32[0]);
|
auto parsed = parser_.parse(&payload.front());
|
||||||
return {data32[0], data32[1], parsed};
|
return {data32[0], data32[1], parsed};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user