mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
fix: cancel thread and fix window close
This commit is contained in:
parent
3b16946c25
commit
6e7f22ac3a
@ -4,14 +4,15 @@
|
||||
#include <glibmm/markup.h>
|
||||
#include <gtkmm/eventbox.h>
|
||||
#include <json/json.h>
|
||||
|
||||
#include "IModule.hpp"
|
||||
|
||||
namespace waybar {
|
||||
|
||||
class AModule : public IModule {
|
||||
public:
|
||||
AModule(const Json::Value &, const std::string &, const std::string &,
|
||||
bool enable_click = false, bool enable_scroll = false);
|
||||
AModule(const Json::Value &, const std::string &, const std::string &, bool enable_click = false,
|
||||
bool enable_scroll = false);
|
||||
virtual ~AModule();
|
||||
virtual auto update() -> void;
|
||||
virtual operator Gtk::Widget &();
|
||||
@ -24,6 +25,7 @@ class AModule : public IModule {
|
||||
SCROLL_DIR getScrollDir(GdkEventScroll *e);
|
||||
bool tooltipEnabled();
|
||||
|
||||
const std::string name_;
|
||||
const Json::Value &config_;
|
||||
Gtk::EventBox event_box_;
|
||||
|
||||
|
@ -34,10 +34,10 @@ class Bar {
|
||||
|
||||
struct waybar_output *output;
|
||||
Json::Value config;
|
||||
Gtk::Window window;
|
||||
struct wl_surface * surface;
|
||||
bool visible = true;
|
||||
bool vertical = false;
|
||||
Gtk::Window window;
|
||||
|
||||
private:
|
||||
static constexpr const char *MIN_HEIGHT_MSG =
|
||||
|
@ -52,8 +52,6 @@ class Network : public ALabel {
|
||||
struct sockaddr_nl nladdr_ = {0};
|
||||
struct nl_sock* sock_ = nullptr;
|
||||
struct nl_sock* ev_sock_ = nullptr;
|
||||
int efd_;
|
||||
int ev_fd_;
|
||||
int nl80211_id_;
|
||||
std::mutex mutex_;
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
namespace waybar::util {
|
||||
|
||||
class Rfkill {
|
||||
public:;
|
||||
public:
|
||||
Rfkill(enum rfkill_type rfkill_type);
|
||||
~Rfkill() = default;
|
||||
void waitForEvent();
|
||||
@ -13,7 +13,7 @@ class Rfkill {
|
||||
|
||||
private:
|
||||
enum rfkill_type rfkill_type_;
|
||||
int state_ = 0;
|
||||
int state_ = 0;
|
||||
};
|
||||
|
||||
} // namespace waybar::util
|
||||
|
@ -59,6 +59,11 @@ class SleeperThread {
|
||||
do_run_ = false;
|
||||
}
|
||||
condvar_.notify_all();
|
||||
auto handle = thread_.native_handle();
|
||||
if (handle != 0) {
|
||||
// TODO: find a proper way to terminate thread...
|
||||
pthread_cancel(handle);
|
||||
}
|
||||
}
|
||||
|
||||
~SleeperThread() {
|
||||
|
@ -6,7 +6,7 @@ namespace waybar {
|
||||
|
||||
AModule::AModule(const Json::Value& config, const std::string& name, const std::string& id,
|
||||
bool enable_click, bool enable_scroll)
|
||||
: config_(std::move(config)) {
|
||||
: name_(std::move(name)), config_(std::move(config)) {
|
||||
// configure events' user commands
|
||||
if (config_["on-click"].isString() || config_["on-click-middle"].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() {
|
||||
for (const auto& pid : pid_) {
|
||||
if (pid != -1) {
|
||||
kill(-pid, 9);
|
||||
killpg(pid, SIGTERM);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
auto AModule::update() -> void {
|
||||
// Run user-provided update handler if configured
|
||||
if (config_["on-update"].isString()) {
|
||||
|
@ -10,8 +10,8 @@
|
||||
waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
|
||||
: output(w_output),
|
||||
config(w_config),
|
||||
window{Gtk::WindowType::WINDOW_TOPLEVEL},
|
||||
surface(nullptr),
|
||||
window{Gtk::WindowType::WINDOW_TOPLEVEL},
|
||||
layer_surface_(nullptr),
|
||||
anchor_(ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP),
|
||||
left_(Gtk::ORIENTATION_HORIZONTAL, 0),
|
||||
|
@ -145,7 +145,8 @@ void waybar::Client::handleMonitorRemoved(Glib::RefPtr<Gdk::Monitor> monitor) {
|
||||
for (auto it = bars.begin(); it != bars.end();) {
|
||||
if ((*it)->output->monitor == monitor) {
|
||||
auto output_name = (*it)->output->name;
|
||||
(*it)->window.close();
|
||||
(*it)->window.hide();
|
||||
gtk_app->remove_window((*it)->window);
|
||||
it = bars.erase(it);
|
||||
spdlog::info("Bar removed from output: {}", output_name);
|
||||
} else {
|
||||
|
@ -63,22 +63,20 @@ auto waybar::modules::MPD::update() -> void {
|
||||
|
||||
std::thread waybar::modules::MPD::event_listener() {
|
||||
return std::thread([this] {
|
||||
while (true) {
|
||||
try {
|
||||
if (connection_ == nullptr) {
|
||||
// Retry periodically if no connection
|
||||
dp.emit();
|
||||
std::this_thread::sleep_for(interval_);
|
||||
} else {
|
||||
waitForEvent();
|
||||
dp.emit();
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
if (strcmp(e.what(), "Connection to MPD closed") == 0) {
|
||||
spdlog::debug("{}: {}", module_name_, e.what());
|
||||
} else {
|
||||
spdlog::warn("{}: {}", module_name_, e.what());
|
||||
}
|
||||
try {
|
||||
if (connection_ == nullptr) {
|
||||
// Retry periodically if no connection
|
||||
dp.emit();
|
||||
std::this_thread::sleep_for(interval_);
|
||||
} else {
|
||||
waitForEvent();
|
||||
dp.emit();
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
if (strcmp(e.what(), "Connection to MPD closed") == 0) {
|
||||
spdlog::debug("{}: {}", module_name_, e.what());
|
||||
} else {
|
||||
spdlog::warn("{}: {}", module_name_, e.what());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -81,8 +81,6 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
|
||||
: ALabel(config, "network", id, "{ifname}", 60),
|
||||
ifid_(-1),
|
||||
family_(config["family"] == "ipv6" ? AF_INET6 : AF_INET),
|
||||
efd_(-1),
|
||||
ev_fd_(-1),
|
||||
cidr_(-1),
|
||||
signal_strength_dbm_(0),
|
||||
signal_strength_(0),
|
||||
@ -117,14 +115,6 @@ waybar::modules::Network::Network(const std::string &id, const Json::Value &conf
|
||||
}
|
||||
|
||||
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) {
|
||||
nl_socket_drop_membership(ev_sock_, RTNLGRP_LINK);
|
||||
if (family_ == AF_INET) {
|
||||
@ -156,30 +146,6 @@ void waybar::modules::Network::createEventSocket() {
|
||||
} else {
|
||||
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() {
|
||||
@ -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 {
|
||||
|
@ -3,11 +3,11 @@
|
||||
* Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
|
||||
* Copyright 2009 Marcel Holtmann <marcel@holtmann.org>
|
||||
* Copyright 2009 Tim Gardner <tim.gardner@canonical.com>
|
||||
*
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* 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. */
|
||||
|
||||
#include "util/rfkill.hpp"
|
||||
#include <linux/rfkill.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <cstring>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <linux/rfkill.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/poll.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type)
|
||||
: rfkill_type_(rfkill_type) {
|
||||
}
|
||||
waybar::util::Rfkill::Rfkill(const enum rfkill_type rfkill_type) : rfkill_type_(rfkill_type) {}
|
||||
|
||||
void waybar::util::Rfkill::waitForEvent() {
|
||||
struct rfkill_event event;
|
||||
struct pollfd p;
|
||||
ssize_t len;
|
||||
int fd, n;
|
||||
struct pollfd p;
|
||||
ssize_t len;
|
||||
int fd, n;
|
||||
|
||||
fd = open("/dev/rfkill", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
@ -53,8 +53,7 @@ void waybar::util::Rfkill::waitForEvent() {
|
||||
break;
|
||||
}
|
||||
|
||||
if (n == 0)
|
||||
continue;
|
||||
if (n == 0) continue;
|
||||
|
||||
len = read(fd, &event, sizeof(event));
|
||||
if (len < 0) {
|
||||
@ -67,17 +66,13 @@ void waybar::util::Rfkill::waitForEvent() {
|
||||
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;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
bool waybar::util::Rfkill::getState() const {
|
||||
return state_;
|
||||
}
|
||||
bool waybar::util::Rfkill::getState() const { return state_; }
|
||||
|
Loading…
Reference in New Issue
Block a user