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

Lighter Weight Signal-Based Custom Modules #2517

Merged
merged 3 commits into from Sep 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions include/modules/custom.hpp
Expand Up @@ -22,6 +22,7 @@ class Custom : public ALabel {
private:
void delayWorker();
void continuousWorker();
void waitingWorker();
void parseOutputRaw();
void parseOutputJson();
void handleEvent();
Expand Down
6 changes: 6 additions & 0 deletions include/util/sleeper_thread.hpp
Expand Up @@ -58,6 +58,12 @@ class SleeperThread {

bool isRunning() const { return do_run_; }

auto sleep() {
std::unique_lock lk(mutex_);
CancellationGuard cancel_lock;
return condvar_.wait(lk);
}

auto sleep_for(std::chrono::system_clock::duration dur) {
std::unique_lock lk(mutex_);
CancellationGuard cancel_lock;
Expand Down
6 changes: 4 additions & 2 deletions man/waybar-custom.5.scd
Expand Up @@ -34,7 +34,8 @@ Addressed by *custom/<name>*
typeof: integer ++
The interval (in seconds) in which the information gets polled. ++
Use *once* if you want to execute the module only on startup. ++
You can update it manually with a signal. If no *interval* is defined, it is assumed that the out script loops it self.
You can update it manually with a signal. If no *interval* or *signal* is defined, it is assumed that the out script loops it self. ++
If a *signal* is defined then the script will run once on startup and will will only update with a signal.

*restart-interval*: ++
typeof: integer ++
Expand All @@ -45,7 +46,8 @@ Addressed by *custom/<name>*
*signal*: ++
typeof: integer ++
The signal number used to update the module. ++
The number is valid between 1 and N, where *SIGRTMIN+N* = *SIGRTMAX*.
The number is valid between 1 and N, where *SIGRTMIN+N* = *SIGRTMAX*. ++
If no interval is defined then a signal will be the only way to update the module.

*format*: ++
typeof: string ++
Expand Down
26 changes: 24 additions & 2 deletions src/modules/custom.cpp
Expand Up @@ -11,11 +11,13 @@ waybar::modules::Custom::Custom(const std::string& name, const std::string& id,
fp_(nullptr),
pid_(-1) {
dp.emit();
if (interval_.count() > 0) {
if (!config_["signal"].empty() && config_["interval"].empty()) {
waitingWorker();
} else if (interval_.count() > 0) {
delayWorker();
} else if (config_["exec"].isString()) {
continuousWorker();
}
}
}

waybar::modules::Custom::~Custom() {
Expand Down Expand Up @@ -92,6 +94,26 @@ void waybar::modules::Custom::continuousWorker() {
};
}

void waybar::modules::Custom::waitingWorker() {
thread_ = [this] {
bool can_update = true;
if (config_["exec-if"].isString()) {
output_ = util::command::execNoRead(config_["exec-if"].asString());
if (output_.exit_code != 0) {
can_update = false;
dp.emit();
}
}
if (can_update) {
if (config_["exec"].isString()) {
output_ = util::command::exec(config_["exec"].asString());
}
dp.emit();
}
thread_.sleep();
};
}

void waybar::modules::Custom::refresh(int sig) {
if (sig == SIGRTMIN + config_["signal"].asInt()) {
thread_.wake_up();
Expand Down