Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass WAYBAR_OUTPUT_NAME environment variable to custom exec scripts #2756

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Pass WAYBAR_OUTPUT_NAME environment variable to custom exec scripts
Signed-off-by: Jo De Boeck <deboeck.jo@gmail.com>
  • Loading branch information
grimpy committed Dec 19, 2023
commit 0ea5143493bff3257740bee349e7b7f02b633ef0
3 changes: 2 additions & 1 deletion include/modules/custom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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_;
Expand Down
11 changes: 7 additions & 4 deletions include/util/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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), ""};
Expand Down
2 changes: 1 addition & 1 deletion src/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
11 changes: 6 additions & 5 deletions src/modules/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_ = "";
Expand Down