Pass WAYBAR_OUTPUT_NAME environment variable to custom exec scripts

Signed-off-by: Jo De Boeck <deboeck.jo@gmail.com>
This commit is contained in:
Jo De Boeck 2023-12-19 22:54:12 +02:00
parent f5370fcff5
commit 0ea5143493
5 changed files with 17 additions and 12 deletions

View File

@ -14,7 +14,7 @@ namespace waybar::modules {
class Custom : public ALabel { class Custom : public ALabel {
public: 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(); virtual ~Custom();
auto update() -> void override; auto update() -> void override;
void refresh(int /*signal*/) override; void refresh(int /*signal*/) override;
@ -30,6 +30,7 @@ class Custom : public ALabel {
bool handleToggle(GdkEventButton* const& e) override; bool handleToggle(GdkEventButton* const& e) override;
const std::string name_; const std::string name_;
const std::string output_name_;
std::string text_; std::string text_;
std::string id_; std::string id_;
std::string alt_; std::string alt_;

View File

@ -66,7 +66,7 @@ inline int close(FILE* fp, pid_t pid) {
return stat; 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; if (cmd == "") return nullptr;
int fd[2]; int fd[2];
// Open the pipe with the close-on-exec flag set, so it will not be inherited // 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]); ::close(fd[0]);
dup2(fd[1], 1); dup2(fd[1], 1);
setpgid(child_pid, child_pid); 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); execlp("/bin/sh", "sh", "-c", cmd.c_str(), (char*)0);
exit(0); exit(0);
} else { } else {
@ -118,9 +121,9 @@ inline FILE* open(const std::string& cmd, int& pid) {
return fdopen(fd[0], "r"); 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; int pid;
auto fp = command::open(cmd, pid); auto fp = command::open(cmd, pid, output_name);
if (!fp) return {-1, ""}; if (!fp) return {-1, ""};
auto output = command::read(fp); auto output = command::read(fp);
auto stat = command::close(fp, pid); 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) { inline struct res execNoRead(const std::string& cmd) {
int pid; int pid;
auto fp = command::open(cmd, pid); auto fp = command::open(cmd, pid, "");
if (!fp) return {-1, ""}; if (!fp) return {-1, ""};
auto stat = command::close(fp, pid); auto stat = command::close(fp, pid);
return {WEXITSTATUS(stat), ""}; return {WEXITSTATUS(stat), ""};

View File

@ -205,7 +205,7 @@ waybar::AModule* waybar::Factory::makeModule(const std::string& name,
return new waybar::modules::Temperature(id, config_[name]); return new waybar::modules::Temperature(id, config_[name]);
} }
if (ref.compare(0, 7, "custom/") == 0 && ref.size() > 7) { 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) { if (ref.compare(0, 5, "cffi/") == 0 && ref.size() > 5) {
return new waybar::modules::CFFI(ref.substr(5), id, config_[name]); return new waybar::modules::CFFI(ref.substr(5), id, config_[name]);

View File

@ -5,9 +5,10 @@
#include "util/scope_guard.hpp" #include "util/scope_guard.hpp"
waybar::modules::Custom::Custom(const std::string& name, const std::string& id, 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, "{}"), : ALabel(config, "custom-" + name, id, "{}"),
name_(name), name_(name),
output_name_(output_name),
id_(id), id_(id),
percentage_(0), percentage_(0),
fp_(nullptr), fp_(nullptr),
@ -42,7 +43,7 @@ void waybar::modules::Custom::delayWorker() {
} }
if (can_update) { if (can_update) {
if (config_["exec"].isString()) { if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString()); output_ = util::command::exec(config_["exec"].asString(), output_name_);
} }
dp.emit(); dp.emit();
} }
@ -53,7 +54,7 @@ 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();
pid_ = -1; pid_ = -1;
fp_ = util::command::open(cmd, pid_); fp_ = util::command::open(cmd, pid_, output_name_);
if (!fp_) { if (!fp_) {
throw std::runtime_error("Unable to open " + cmd); throw std::runtime_error("Unable to open " + cmd);
} }
@ -79,7 +80,7 @@ void waybar::modules::Custom::continuousWorker() {
if (config_["restart-interval"].isUInt()) { if (config_["restart-interval"].isUInt()) {
pid_ = -1; pid_ = -1;
thread_.sleep_for(std::chrono::seconds(config_["restart-interval"].asUInt())); 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_) { if (!fp_) {
throw std::runtime_error("Unable to open " + cmd); throw std::runtime_error("Unable to open " + cmd);
} }
@ -112,7 +113,7 @@ void waybar::modules::Custom::waitingWorker() {
} }
if (can_update) { if (can_update) {
if (config_["exec"].isString()) { if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString()); output_ = util::command::exec(config_["exec"].asString(), output_name_);
} }
dp.emit(); dp.emit();
} }

View File

@ -45,7 +45,7 @@ auto waybar::modules::Image::update() -> void {
if (config_["path"].isString()) { if (config_["path"].isString()) {
path_ = config_["path"].asString(); path_ = config_["path"].asString();
} else if (config_["exec"].isString()) { } else if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString()); output_ = util::command::exec(config_["exec"].asString(), "");
parseOutputRaw(); parseOutputRaw();
} else { } else {
path_ = ""; path_ = "";