Handle screens disconnection (#29)

This commit is contained in:
Alex
2018-08-19 13:39:57 +02:00
committed by GitHub
parent ce50a627be
commit 6705134034
23 changed files with 205 additions and 133 deletions

View File

@ -16,26 +16,32 @@ waybar::modules::Battery::Battery(Json::Value config)
if (batteries_.empty()) {
throw std::runtime_error("No batteries.");
}
auto fd = inotify_init1(IN_CLOEXEC);
if (fd == -1) {
fd_ = inotify_init1(IN_CLOEXEC);
if (fd_ == -1) {
throw std::runtime_error("Unable to listen batteries.");
}
for (auto &bat : batteries_) {
inotify_add_watch(fd, (bat / "uevent").c_str(), IN_ACCESS);
inotify_add_watch(fd_, (bat / "uevent").c_str(), IN_ACCESS);
}
// Trigger first value
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Battery::update));
// Trigger first values
update();
label_.set_name("battery");
thread_ = [this, fd] {
thread_.sig_update.connect(sigc::mem_fun(*this, &Battery::update));
thread_ = [this] {
struct inotify_event event = {0};
int nbytes = read(fd, &event, sizeof(event));
int nbytes = read(fd_, &event, sizeof(event));
if (nbytes != sizeof(event)) {
return;
}
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Battery::update));
thread_.sig_update.emit();
};
}
waybar::modules::Battery::~Battery()
{
close(fd_);
}
auto waybar::modules::Battery::update() -> void
{
try {

View File

@ -5,9 +5,10 @@ waybar::modules::Clock::Clock(Json::Value config)
{
label_.set_name("clock");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 60;
thread_.sig_update.connect(sigc::mem_fun(*this, &Clock::update));
thread_ = [this, interval] {
auto now = waybar::chrono::clock::now();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Clock::update));
thread_.sig_update.emit();
auto timeout = std::chrono::floor<std::chrono::seconds>(now
+ std::chrono::seconds(interval));
thread_.sleep_until(timeout);

View File

@ -5,8 +5,9 @@ waybar::modules::Cpu::Cpu(Json::Value config)
{
label_.set_name("cpu");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10;
thread_.sig_update.connect(sigc::mem_fun(*this, &Cpu::update));
thread_ = [this, interval] {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Cpu::update));
thread_.sig_update.emit();
thread_.sleep_for(chrono::seconds(interval));
};
}

View File

@ -16,10 +16,11 @@ waybar::modules::Custom::Custom(std::string name, Json::Value config)
}
}
if (can_update) {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Custom::update));
thread_.sig_update.emit();
}
thread_.sleep_for(chrono::seconds(interval));
};
thread_.sig_update.connect(sigc::mem_fun(*this, &Custom::update));
}
auto waybar::modules::Custom::update() -> void

View File

@ -5,8 +5,9 @@ waybar::modules::Memory::Memory(Json::Value config)
{
label_.set_name("memory");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 30;
thread_.sig_update.connect(sigc::mem_fun(*this, &Memory::update));
thread_ = [this, interval] {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Memory::update));
thread_.sig_update.emit();
thread_.sleep_for(chrono::seconds(interval));
};
}

View File

@ -29,8 +29,10 @@ waybar::modules::Network::Network(Json::Value config)
}
}
label_.set_name("network");
// Trigger first values
getInfo();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Network::update));
update();
thread_.sig_update.connect(sigc::mem_fun(*this, &Network::update));
thread_ = [this] {
char buf[4096];
uint64_t len = netlinkResponse(sock_fd_, buf, sizeof(buf),
@ -67,11 +69,16 @@ waybar::modules::Network::Network(Json::Value config)
}
if (need_update) {
getInfo();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Network::update));
thread_.sig_update.emit();
}
};
}
waybar::modules::Network::~Network()
{
close(sock_fd_);
}
auto waybar::modules::Network::update() -> void
{
auto format = config_["format"] ? config_["format"].asString() : "{ifname}";

View File

@ -28,6 +28,13 @@ waybar::modules::Pulseaudio::Pulseaudio(Json::Value config)
pa_threaded_mainloop_unlock(mainloop_);
}
waybar::modules::Pulseaudio::~Pulseaudio()
{
mainloop_api_->quit(mainloop_api_, 0);
pa_threaded_mainloop_stop(mainloop_);
pa_threaded_mainloop_free(mainloop_);
}
void waybar::modules::Pulseaudio::contextStateCb(pa_context *c, void *data)
{
auto pa = static_cast<waybar::modules::Pulseaudio *>(data);

View File

@ -10,6 +10,7 @@ waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
ipc_eventfd_ = ipcOpenSocket(socketPath);
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"window\" ]");
getFocusedWindow();
thread_.sig_update.connect(sigc::mem_fun(*this, &Window::update));
thread_ = [this] {
try {
auto res = ipcRecvResponse(ipc_eventfd_);
@ -17,7 +18,7 @@ waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
if ((parsed["change"] == "focus" || parsed["change"] == "title")
&& parsed["container"]["focused"].asBool()) {
window_ = parsed["container"]["name"].asString();
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Window::update));
thread_.sig_update.emit();
}
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
@ -25,6 +26,12 @@ waybar::modules::sway::Window::Window(Bar &bar, Json::Value config)
};
}
waybar::modules::sway::Window::~Window()
{
close(ipcfd_);
close(ipc_eventfd_);
}
auto waybar::modules::sway::Window::update() -> void
{
label_.set_text(window_);

View File

@ -9,30 +9,35 @@ waybar::modules::sway::Workspaces::Workspaces(Bar &bar, Json::Value config)
ipcfd_ = ipcOpenSocket(socketPath);
ipc_eventfd_ = ipcOpenSocket(socketPath);
ipcSingleCommand(ipc_eventfd_, IPC_SUBSCRIBE, "[ \"workspace\" ]");
thread_.sig_update.connect(sigc::mem_fun(*this, &Workspaces::update));
thread_ = [this] {
try {
// Wait for the name of the output
if (!config_["all-outputs"].asBool() && bar_.outputName.empty()) {
while (bar_.outputName.empty()) {
if (!config_["all-outputs"].asBool() && bar_.output_name.empty()) {
while (bar_.output_name.empty()) {
thread_.sleep_for(chrono::milliseconds(150));
}
} else if (!workspaces_.empty()) {
ipcRecvResponse(ipc_eventfd_);
}
std::lock_guard<std::mutex> lock(mutex_);
auto res = ipcSingleCommand(ipcfd_, IPC_GET_WORKSPACES, "");
workspaces_ = parser_.parse(res.payload);
Glib::signal_idle()
.connect_once(sigc::mem_fun(*this, &Workspaces::update));
thread_.sig_update.emit();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
};
}
waybar::modules::sway::Workspaces::~Workspaces()
{
close(ipcfd_);
close(ipc_eventfd_);
}
auto waybar::modules::sway::Workspaces::update() -> void
{
std::lock_guard<std::mutex> lock(mutex_);
auto res = ipcSingleCommand(ipcfd_, IPC_GET_WORKSPACES, "");
workspaces_ = parser_.parse(res.payload);
bool needReorder = false;
for (auto it = buttons_.begin(); it != buttons_.end();) {
auto ws = std::find_if(workspaces_.begin(), workspaces_.end(),
@ -46,7 +51,7 @@ auto waybar::modules::sway::Workspaces::update() -> void
}
for (auto node : workspaces_) {
if (!config_["all-outputs"].asBool()
&& bar_.outputName != node["output"].asString()) {
&& bar_.output_name != node["output"].asString()) {
continue;
}
auto it = buttons_.find(node["num"].asInt());