diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index c9992695..2c7ba8a8 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -14,7 +14,7 @@ namespace waybar::modules { class Custom : public ALabel { public: - Custom(const std::string&, const std::string&, const Json::Value&); + Custom(const std::string&, const std::string&, const Json::Value&, const std::string&); virtual ~Custom(); auto update() -> void override; void refresh(int /*signal*/) override; @@ -30,6 +30,7 @@ class Custom : public ALabel { bool handleToggle(GdkEventButton* const& e) override; const std::string name_; + const std::string output_name_; std::string text_; std::string id_; std::string alt_; diff --git a/include/util/command.hpp b/include/util/command.hpp index 0d729b77..4b9decaa 100644 --- a/include/util/command.hpp +++ b/include/util/command.hpp @@ -66,7 +66,7 @@ inline int close(FILE* fp, pid_t pid) { return stat; } -inline FILE* open(const std::string& cmd, int& pid) { +inline FILE* open(const std::string& cmd, int& pid, const std::string& output_name) { if (cmd == "") return nullptr; int fd[2]; // Open the pipe with the close-on-exec flag set, so it will not be inherited @@ -109,6 +109,9 @@ inline FILE* open(const std::string& cmd, int& pid) { ::close(fd[0]); dup2(fd[1], 1); setpgid(child_pid, child_pid); + if (output_name != "") { + setenv("WAYBAR_OUTPUT_NAME", output_name.c_str(), 1); + } execlp("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0); exit(0); } else { @@ -118,9 +121,9 @@ inline FILE* open(const std::string& cmd, int& pid) { return fdopen(fd[0], "r"); } -inline struct res exec(const std::string& cmd) { +inline struct res exec(const std::string& cmd, const std::string& output_name) { int pid; - auto fp = command::open(cmd, pid); + auto fp = command::open(cmd, pid, output_name); if (!fp) return {-1, ""}; auto output = command::read(fp); auto stat = command::close(fp, pid); @@ -129,7 +132,7 @@ inline struct res exec(const std::string& cmd) { inline struct res execNoRead(const std::string& cmd) { int pid; - auto fp = command::open(cmd, pid); + auto fp = command::open(cmd, pid, ""); if (!fp) return {-1, ""}; auto stat = command::close(fp, pid); return {WEXITSTATUS(stat), ""}; diff --git a/src/factory.cpp b/src/factory.cpp index ce5b925e..f3d7ebe6 100644 --- a/src/factory.cpp +++ b/src/factory.cpp @@ -205,7 +205,7 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name, return new waybar::modules::Temperature(id, config_[name]); } if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) { - return new waybar::modules::Custom(ref.substr(7), id, config_[name]); + return new waybar::modules::Custom(ref.substr(7), id, config_[name], bar_.output->name); } if (ref.compare(0, 5, "cffi/") == 0 && ref.size() > 5) { return new waybar::modules::CFFI(ref.substr(5), id, config_[name]); diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index fa03c3be..6101f874 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -5,9 +5,10 @@ #include "util/scope_guard.hpp" waybar::modules::Custom::Custom(const std::string& name, const std::string& id, - const Json::Value& config) + const Json::Value& config, const std::string& output_name) : ALabel(config, "custom-" + name, id, "{}"), name_(name), + output_name_(output_name), id_(id), percentage_(0), fp_(nullptr), @@ -42,7 +43,7 @@ void waybar::modules::Custom::delayWorker() { } if (can_update) { if (config_["exec"].isString()) { - output_ = util::command::exec(config_["exec"].asString()); + output_ = util::command::exec(config_["exec"].asString(), output_name_); } dp.emit(); } @@ -53,7 +54,7 @@ void waybar::modules::Custom::delayWorker() { void waybar::modules::Custom::continuousWorker() { auto cmd = config_["exec"].asString(); pid_ = -1; - fp_ = util::command::open(cmd, pid_); + fp_ = util::command::open(cmd, pid_, output_name_); if (!fp_) { throw std::runtime_error("Unable to open " + cmd); } @@ -79,7 +80,7 @@ void waybar::modules::Custom::continuousWorker() { if (config_["restart-interval"].isUInt()) { pid_ = -1; thread_.sleep_for(std::chrono::seconds(config_["restart-interval"].asUInt())); - fp_ = util::command::open(cmd, pid_); + fp_ = util::command::open(cmd, pid_, output_name_); if (!fp_) { throw std::runtime_error("Unable to open " + cmd); } @@ -112,7 +113,7 @@ void waybar::modules::Custom::waitingWorker() { } if (can_update) { if (config_["exec"].isString()) { - output_ = util::command::exec(config_["exec"].asString()); + output_ = util::command::exec(config_["exec"].asString(), output_name_); } dp.emit(); } diff --git a/src/modules/image.cpp b/src/modules/image.cpp index 843cd954..08b03b92 100644 --- a/src/modules/image.cpp +++ b/src/modules/image.cpp @@ -45,7 +45,7 @@ auto waybar::modules::Image::update() -> void { if (config_["path"].isString()) { path_ = config_["path"].asString(); } else if (config_["exec"].isString()) { - output_ = util::command::exec(config_["exec"].asString()); + output_ = util::command::exec(config_["exec"].asString(), ""); parseOutputRaw(); } else { path_ = "";