fix: cancel thread and fix window close

This commit is contained in:
Alex 2020-05-27 09:10:38 +02:00
parent 3b16946c25
commit 6e7f22ac3a
11 changed files with 47 additions and 94 deletions

View File

@ -4,14 +4,15 @@
#include <glibmm/markup.h> #include <glibmm/markup.h>
#include <gtkmm/eventbox.h> #include <gtkmm/eventbox.h>
#include <json/json.h> #include <json/json.h>
#include "IModule.hpp" #include "IModule.hpp"
namespace waybar { namespace waybar {
class AModule : public IModule { class AModule : public IModule {
public: public:
AModule(const Json::Value &, const std::string &, const std::string &, AModule(const Json::Value &, const std::string &, const std::string &, bool enable_click = false,
bool enable_click = false, bool enable_scroll = false); bool enable_scroll = false);
virtual ~AModule(); virtual ~AModule();
virtual auto update() -> void; virtual auto update() -> void;
virtual operator Gtk::Widget &(); virtual operator Gtk::Widget &();
@ -24,6 +25,7 @@ class AModule : public IModule {
SCROLL_DIR getScrollDir(GdkEventScroll *e); SCROLL_DIR getScrollDir(GdkEventScroll *e);
bool tooltipEnabled(); bool tooltipEnabled();
const std::string name_;
const Json::Value &config_; const Json::Value &config_;
Gtk::EventBox event_box_; Gtk::EventBox event_box_;

View File

@ -34,10 +34,10 @@ class Bar {
struct waybar_output *output; struct waybar_output *output;
Json::Value config; Json::Value config;
Gtk::Window window;
struct wl_surface * surface; struct wl_surface * surface;
bool visible = true; bool visible = true;
bool vertical = false; bool vertical = false;
Gtk::Window window;
private: private:
static constexpr const char *MIN_HEIGHT_MSG = static constexpr const char *MIN_HEIGHT_MSG =

View File

@ -52,8 +52,6 @@ class Network : public ALabel {
struct sockaddr_nl nladdr_ = {0}; struct sockaddr_nl nladdr_ = {0};
struct nl_sock* sock_ = nullptr; struct nl_sock* sock_ = nullptr;
struct nl_sock* ev_sock_ = nullptr; struct nl_sock* ev_sock_ = nullptr;
int efd_;
int ev_fd_;
int nl80211_id_; int nl80211_id_;
std::mutex mutex_; std::mutex mutex_;

View File

@ -5,7 +5,7 @@
namespace waybar::util { namespace waybar::util {
class Rfkill { class Rfkill {
public:; public:
Rfkill(enum rfkill_type rfkill_type); Rfkill(enum rfkill_type rfkill_type);
~Rfkill() = default; ~Rfkill() = default;
void waitForEvent(); void waitForEvent();
@ -13,7 +13,7 @@ class Rfkill {
private: private:
enum rfkill_type rfkill_type_; enum rfkill_type rfkill_type_;
int state_ = 0; int state_ = 0;
}; };
} // namespace waybar::util } // namespace waybar::util

View File

@ -59,6 +59,11 @@ class SleeperThread {
do_run_ = false; do_run_ = false;
} }
condvar_.notify_all(); condvar_.notify_all();
auto handle = thread_.native_handle();
if (handle != 0) {
// TODO: find a proper way to terminate thread...
pthread_cancel(handle);
}
} }
~SleeperThread() { ~SleeperThread() {

View File

@ -6,7 +6,7 @@ namespace waybar {
AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id, AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id,
bool enable_click, bool enable_scroll) bool enable_click, bool enable_scroll)
: config_(std::move(config)) { : name_(std::move(name)), config_(std::move(config)) {
// configure events' user commands // configure events' user commands
if (config_["on-click"].isString() || config_["on-click-middle"].isString() || if (config_["on-click"].isString() || config_["on-click-middle"].isString() ||
config_["on-click-backward"].isString() || config_["on-click-forward"].isString() || config_["on-click-backward"].isString() || config_["on-click-forward"].isString() ||
@ -23,11 +23,12 @@ AModule::AModule(const Json::Value& config, const std::string& name, const std::
AModule::~AModule() { AModule::~AModule() {
for (const auto& pid : pid_) { for (const auto& pid : pid_) {
if (pid != -1) { if (pid != -1) {
kill(-pid, 9); killpg(pid, SIGTERM);
} }
} }
} }
auto AModule::update() -> void { auto AModule::update() -> void {
// Run user-provided update handler if configured // Run user-provided update handler if configured
if (config_["on-update"].isString()) { if (config_["on-update"].isString()) {

View File

@ -10,8 +10,8 @@
waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config) waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
: output(w_output), : output(w_output),
config(w_config), config(w_config),
window{Gtk::WindowType::WINDOW_TOPLEVEL},
surface(nullptr), surface(nullptr),
window{Gtk::WindowType::WINDOW_TOPLEVEL},
layer_surface_(nullptr), layer_surface_(nullptr),
anchor_(ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP), anchor_(ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP),
left_(Gtk::ORIENTATION_HORIZONTAL, 0), left_(Gtk::ORIENTATION_HORIZONTAL, 0),

View File

@ -145,7 +145,8 @@ void waybar::Client::handleMonitorRemoved(Glib::RefPtr<Gdk::Monitor> monitor) {
for (auto it = bars.begin(); it != bars.end();) { for (auto it = bars.begin(); it != bars.end();) {
if ((*it)->output->monitor == monitor) { if ((*it)->output->monitor == monitor) {
auto output_name = (*it)->output->name; auto output_name = (*it)->output->name;
(*it)->window.close(); (*it)->window.hide();
gtk_app->remove_window((*it)->window);
it = bars.erase(it); it = bars.erase(it);
spdlog::info("Bar removed from output: {}", output_name); spdlog::info("Bar removed from output: {}", output_name);
} else { } else {

View File

@ -63,22 +63,20 @@ auto waybar::modules::MPD::update() -> void {
std::thread waybar::modules::MPD::event_listener() { std::thread waybar::modules::MPD::event_listener() {
return std::thread([this] { return std::thread([this] {
while (true) { try {
try { if (connection_ == nullptr) {
if (connection_ == nullptr) { // Retry periodically if no connection
// Retry periodically if no connection dp.emit();
dp.emit(); std::this_thread::sleep_for(interval_);
std::this_thread::sleep_for(interval_); } else {
} else { waitForEvent();
waitForEvent(); dp.emit();
dp.emit(); }
} } catch (const std::exception& e) {
} catch (const std::exception& e) { if (strcmp(e.what(), "Connection to MPD closed") == 0) {
if (strcmp(e.what(), "Connection to MPD closed") == 0) { spdlog::debug("{}: {}", module_name_, e.what());
spdlog::debug("{}: {}", module_name_, e.what()); } else {
} else { spdlog::warn("{}: {}", module_name_, e.what());
spdlog::warn("{}: {}", module_name_, e.what());
}
} }
} }
}); });

View File

@ -81,8 +81,6 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
: ALabel(config, "network", id, "{ifname}", 60), : ALabel(config, "network", id, "{ifname}", 60),
ifid_(-1), ifid_(-1),
family_(config["family"] == "ipv6" ? AF_INET6 : AF_INET), family_(config["family"] == "ipv6" ? AF_INET6 : AF_INET),
efd_(-1),
ev_fd_(-1),
cidr_(-1), cidr_(-1),
signal_strength_dbm_(0), signal_strength_dbm_(0),
signal_strength_(0), signal_strength_(0),
@ -117,14 +115,6 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
} }
waybar::modules::Network::~Network() { waybar::modules::Network::~Network() {
if (ev_fd_ > -1) {
eventfd_write(ev_fd_, 1);
std::this_thread::sleep_for(std::chrono::milliseconds(150));
close(ev_fd_);
}
if (efd_ > -1) {
close(efd_);
}
if (ev_sock_ != nullptr) { if (ev_sock_ != nullptr) {
nl_socket_drop_membership(ev_sock_, RTNLGRP_LINK); nl_socket_drop_membership(ev_sock_, RTNLGRP_LINK);
if (family_ == AF_INET) { if (family_ == AF_INET) {
@ -156,30 +146,6 @@ void waybar::modules::Network::createEventSocket() {
} else { } else {
nl_socket_add_membership(ev_sock_, RTNLGRP_IPV6_IFADDR); nl_socket_add_membership(ev_sock_, RTNLGRP_IPV6_IFADDR);
} }
efd_ = epoll_create1(EPOLL_CLOEXEC);
if (efd_ < 0) {
throw std::runtime_error("Can't create epoll");
}
{
ev_fd_ = eventfd(0, EFD_NONBLOCK);
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.events = EPOLLIN | EPOLLET;
event.data.fd = ev_fd_;
if (epoll_ctl(efd_, EPOLL_CTL_ADD, ev_fd_, &event) == -1) {
throw std::runtime_error("Can't add epoll event");
}
}
{
auto fd = nl_socket_get_fd(ev_sock_);
struct epoll_event event;
memset(&event, 0, sizeof(event));
event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
event.data.fd = fd;
if (epoll_ctl(efd_, EPOLL_CTL_ADD, fd, &event) == -1) {
throw std::runtime_error("Can't add epoll event");
}
}
} }
void waybar::modules::Network::createInfoSocket() { void waybar::modules::Network::createInfoSocket() {
@ -218,19 +184,6 @@ void waybar::modules::Network::worker() {
} }
} }
}; };
thread_ = [this] {
std::array<struct epoll_event, EPOLL_MAX> events{};
int ec = epoll_wait(efd_, events.data(), EPOLL_MAX, -1);
if (ec > 0) {
for (auto i = 0; i < ec; i++) {
if (events[i].data.fd != nl_socket_get_fd(ev_sock_) || nl_recvmsgs_default(ev_sock_) < 0) {
thread_.stop();
break;
}
}
}
};
} }
const std::string waybar::modules::Network::getNetworkState() const { const std::string waybar::modules::Network::getNetworkState() const {

View File

@ -3,11 +3,11 @@
* Copyright 2009 Johannes Berg <johannes@sipsolutions.net> * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
* Copyright 2009 Marcel Holtmann <marcel@holtmann.org> * Copyright 2009 Marcel Holtmann <marcel@holtmann.org>
* Copyright 2009 Tim Gardner <tim.gardner@canonical.com> * Copyright 2009 Tim Gardner <tim.gardner@canonical.com>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies. * copyright notice and this permission notice appear in all copies.
* *
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
@ -17,24 +17,24 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include "util/rfkill.hpp" #include "util/rfkill.hpp"
#include <linux/rfkill.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <linux/rfkill.h>
#include <stdlib.h>
#include <sys/poll.h> #include <sys/poll.h>
#include <unistd.h>
#include <cerrno> #include <cerrno>
#include <cstring>
#include <stdexcept> #include <stdexcept>
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type) waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type) : rfkill_type_(rfkill_type) {}
: rfkill_type_(rfkill_type) {
}
void waybar::util::Rfkill::waitForEvent() { void waybar::util::Rfkill::waitForEvent() {
struct rfkill_event event; struct rfkill_event event;
struct pollfd p; struct pollfd p;
ssize_t len; ssize_t len;
int fd, n; int fd, n;
fd = open("/dev/rfkill", O_RDONLY); fd = open("/dev/rfkill", O_RDONLY);
if (fd < 0) { if (fd < 0) {
@ -53,8 +53,7 @@ void waybar::util::Rfkill::waitForEvent() {
break; break;
} }
if (n == 0) if (n == 0) continue;
continue;
len = read(fd, &event, sizeof(event)); len = read(fd, &event, sizeof(event));
if (len < 0) { if (len < 0) {
@ -67,17 +66,13 @@ void waybar::util::Rfkill::waitForEvent() {
continue; continue;
} }
if(event.type == rfkill_type_ && event.op == RFKILL_OP_CHANGE) { if (event.type == rfkill_type_ && event.op == RFKILL_OP_CHANGE) {
state_ = event.soft || event.hard; state_ = event.soft || event.hard;
break; break;
} }
} }
close(fd); close(fd);
return;
} }
bool waybar::util::Rfkill::getState() const { return state_; }
bool waybar::util::Rfkill::getState() const {
return state_;
}