Merge pull request #2756 from grimpy/custom_output_env_var
Pass WAYBAR_OUTPUT_NAME environment variable to custom exec scripts
This commit is contained in:
commit
08361be9f0
|
@ -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_;
|
||||
|
|
|
@ -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), ""};
|
||||
|
|
|
@ -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]);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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_ = "";
|
||||
|
|
Loading…
Reference in New Issue