Merge pull request #2517 from an-prata/master

Lighter Weight Signal-Based Custom Modules
pull/2534/head
Alexis Rouillard 2023-09-21 23:48:22 +02:00 committed by GitHub
commit 6997b34a81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 4 deletions

View File

@ -22,6 +22,7 @@ class Custom : public ALabel {
private:
void delayWorker();
void continuousWorker();
void waitingWorker();
void parseOutputRaw();
void parseOutputJson();
void handleEvent();

View File

@ -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;

View File

@ -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 ++
@ -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 ++

View File

@ -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();