diff --git a/include/modules/custom.hpp b/include/modules/custom.hpp index a6024a84..c9992695 100644 --- a/include/modules/custom.hpp +++ b/include/modules/custom.hpp @@ -22,6 +22,7 @@ class Custom : public ALabel { private: void delayWorker(); void continuousWorker(); + void waitingWorker(); void parseOutputRaw(); void parseOutputJson(); void handleEvent(); diff --git a/include/util/sleeper_thread.hpp b/include/util/sleeper_thread.hpp index 861d5f1f..3d8c05d1 100644 --- a/include/util/sleeper_thread.hpp +++ b/include/util/sleeper_thread.hpp @@ -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; diff --git a/man/waybar-custom.5.scd b/man/waybar-custom.5.scd index aeb419f9..a038e355 100644 --- a/man/waybar-custom.5.scd +++ b/man/waybar-custom.5.scd @@ -34,7 +34,8 @@ Addressed by *custom/* 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 ++ @@ -45,7 +46,8 @@ Addressed by *custom/* *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 ++ diff --git a/src/modules/custom.cpp b/src/modules/custom.cpp index 5a246aff..6cbb5e1e 100644 --- a/src/modules/custom.cpp +++ b/src/modules/custom.cpp @@ -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() { @@ -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();