Merge pull request #228 from hoellen/feat-rtsignal

Add pkill signals to custom modules
This commit is contained in:
Alex 2019-03-18 19:53:02 +01:00 committed by GitHub
commit 418767c405
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 5 deletions

View File

@ -22,6 +22,7 @@ class Bar {
~Bar() = default; ~Bar() = default;
auto toggle() -> void; auto toggle() -> void;
void handleSignal(int);
const Client& client; const Client& client;
Gtk::Window window; Gtk::Window window;

View File

@ -2,6 +2,7 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <iostream> #include <iostream>
#include <csignal>
#include "util/sleeper_thread.hpp" #include "util/sleeper_thread.hpp"
#include "util/command.hpp" #include "util/command.hpp"
#include "util/json.hpp" #include "util/json.hpp"
@ -14,6 +15,7 @@ class Custom : public ALabel {
Custom(const std::string&, const Json::Value&); Custom(const std::string&, const Json::Value&);
~Custom(); ~Custom();
auto update() -> void; auto update() -> void;
void refresh(int /*signal*/);
private: private:
void delayWorker(); void delayWorker();
void continuousWorker(); void continuousWorker();

View File

@ -21,7 +21,10 @@ public:
SleeperThread& operator=(std::function<void()> func) SleeperThread& operator=(std::function<void()> func)
{ {
thread_ = std::thread([this, func] { thread_ = std::thread([this, func] {
while (do_run_) func(); while (do_run_) {
signal_ = false;
func();
}
}); });
return *this; return *this;
} }
@ -34,18 +37,19 @@ public:
auto sleep_for(std::chrono::system_clock::duration dur) auto sleep_for(std::chrono::system_clock::duration dur)
{ {
std::unique_lock lk(mutex_); std::unique_lock lk(mutex_);
return condvar_.wait_for(lk, dur, [this] { return !do_run_; }); return condvar_.wait_for(lk, dur, [this] { return signal_ || !do_run_; });
} }
auto sleep_until(std::chrono::time_point<std::chrono::system_clock, auto sleep_until(std::chrono::time_point<std::chrono::system_clock,
std::chrono::system_clock::duration> time_point) std::chrono::system_clock::duration> time_point)
{ {
std::unique_lock lk(mutex_); std::unique_lock lk(mutex_);
return condvar_.wait_until(lk, time_point, [this] { return !do_run_; }); return condvar_.wait_until(lk, time_point, [this] { return signal_ || !do_run_; });
} }
auto wake_up() auto wake_up()
{ {
signal_ = true;
condvar_.notify_all(); condvar_.notify_all();
} }
@ -53,6 +57,7 @@ public:
{ {
{ {
std::lock_guard<std::mutex> lck(mutex_); std::lock_guard<std::mutex> lck(mutex_);
signal_ = true;
do_run_ = false; do_run_ = false;
} }
condvar_.notify_all(); condvar_.notify_all();
@ -71,6 +76,7 @@ private:
std::condition_variable condvar_; std::condition_variable condvar_;
std::mutex mutex_; std::mutex mutex_;
bool do_run_ = true; bool do_run_ = true;
bool signal_ = false;
}; };
} }

View File

@ -6,8 +6,9 @@
waybar::ALabel::ALabel(const Json::Value& config, const std::string format, uint16_t interval) waybar::ALabel::ALabel(const Json::Value& config, const std::string format, uint16_t interval)
: config_(config), : config_(config),
format_(config_["format"].isString() ? config_["format"].asString() : format), format_(config_["format"].isString() ? config_["format"].asString() : format),
interval_(std::chrono::seconds(config_["interval"].isUInt() interval_(config_["interval"] == "once" ? std::chrono::seconds(100000000) :
? config_["interval"].asUInt() : interval)), default_format_(format_) std::chrono::seconds(config_["interval"].isUInt() ?
config_["interval"].asUInt() : interval)), default_format_(format_)
{ {
event_box_.add(label_); event_box_.add(label_);
if (config_["max-length"].isUInt()) { if (config_["max-length"].isUInt()) {

View File

@ -185,6 +185,22 @@ void waybar::Bar::handleDescription(void* /*data*/,
// Nothing here // Nothing here
} }
void waybar::Bar::handleSignal(int signal)
{
for (auto& module : modules_left_) {
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
if(custom) custom->refresh(signal);
}
for (auto& module : modules_center_) {
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
if(custom) custom->refresh(signal);
}
for (auto& module : modules_right_) {
auto* custom = dynamic_cast<waybar::modules::Custom*>(module.get());
if(custom) custom->refresh(signal);
}
}
void waybar::Bar::layerSurfaceHandleConfigure(void* data, void waybar::Bar::layerSurfaceHandleConfigure(void* data,
struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width, struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width,
uint32_t height) uint32_t height)

View File

@ -19,6 +19,14 @@ int main(int argc, char* argv[])
} }
}); });
for (int sig = SIGRTMIN + 1; sig <= SIGRTMAX; ++sig) {
std::signal(sig, [] (int sig/*signal*/) {
for (auto& bar : waybar::client->bars) {
bar->handleSignal(sig);
}
});
}
return c.main(argc, argv); return c.main(argc, argv);
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << e.what() << std::endl; std::cerr << e.what() << std::endl;

View File

@ -77,6 +77,13 @@ void waybar::modules::Custom::continuousWorker()
}; };
} }
void waybar::modules::Custom::refresh(int sig /*signal*/)
{
if(sig == SIGRTMIN + config_["signal"].asInt()) {
thread_.wake_up();
}
}
auto waybar::modules::Custom::update() -> void auto waybar::modules::Custom::update() -> void
{ {
// Hide label if output is empty // Hide label if output is empty