mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Handle screens disconnection (#29)
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user