Do not crash when a monitor is removed

This commit is contained in:
Jef Steelant 2022-11-09 09:34:19 +01:00
parent 8be5bab8ad
commit 8f4f67f69f
6 changed files with 62 additions and 16 deletions

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include <deque> #include <list>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
@ -7,11 +7,19 @@
#include <thread> #include <thread>
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
class EventHandler {
public:
virtual void onEvent(const std::string& ev) = 0;
virtual ~EventHandler() = default;
};
class IPC { class IPC {
public: public:
IPC() { startIPC(); } IPC() { startIPC(); }
void registerForIPC(const std::string&, std::function<void(const std::string&)>); void registerForIPC(const std::string&, EventHandler*);
void unregisterForIPC(EventHandler*);
std::string getSocket1Reply(const std::string& rq); std::string getSocket1Reply(const std::string& rq);
@ -20,7 +28,7 @@ class IPC {
void parseIPC(const std::string&); void parseIPC(const std::string&);
std::mutex callbackMutex; std::mutex callbackMutex;
std::deque<std::pair<std::string, std::function<void(const std::string&)>>> callbacks; std::list<std::pair<std::string, EventHandler*>> callbacks;
}; };
inline std::unique_ptr<IPC> gIPC; inline std::unique_ptr<IPC> gIPC;

View File

@ -7,10 +7,11 @@
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
class Language : public waybar::AButton { class Language : public waybar::AButton,
public EventHandler {
public: public:
Language(const std::string&, const waybar::Bar&, const Json::Value&); Language(const std::string&, const waybar::Bar&, const Json::Value&);
~Language() = default; ~Language();
auto update() -> void; auto update() -> void;

View File

@ -9,10 +9,11 @@
namespace waybar::modules::hyprland { namespace waybar::modules::hyprland {
class Window : public waybar::ALabel { class Window : public waybar::ALabel,
public EventHandler {
public: public:
Window(const std::string&, const waybar::Bar&, const Json::Value&); Window(const std::string&, const waybar::Bar&, const Json::Value&);
~Window() = default; ~Window();
auto update() -> void; auto update() -> void;
@ -28,4 +29,4 @@ class Window : public waybar::ALabel {
std::string lastView; std::string lastView;
}; };
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland

View File

@ -95,15 +95,37 @@ void IPC::parseIPC(const std::string& ev) {
for (auto& [eventname, handler] : callbacks) { for (auto& [eventname, handler] : callbacks) {
if (eventname == request) { if (eventname == request) {
handler(ev); handler->onEvent(ev);
} }
} }
} }
void IPC::registerForIPC(const std::string& ev, std::function<void(const std::string&)> fn) { void IPC::registerForIPC(const std::string& ev, EventHandler* ev_handler) {
if (!ev_handler) {
return;
}
callbackMutex.lock(); callbackMutex.lock();
callbacks.emplace_back(std::make_pair(ev, fn)); callbacks.emplace_back(std::make_pair(ev, ev_handler));
callbackMutex.unlock();
}
void IPC::unregisterForIPC(EventHandler* ev_handler) {
if (!ev_handler) {
return;
}
callbackMutex.lock();
for(auto it = callbacks.begin(); it != callbacks.end(); ) {
auto it_current = it;
it++;
auto& [eventname, handler] = *it_current;
if (handler == ev_handler) {
callbacks.erase(it_current);
}
}
callbackMutex.unlock(); callbackMutex.unlock();
} }
@ -168,4 +190,4 @@ std::string IPC::getSocket1Reply(const std::string& rq) {
return std::string(buffer); return std::string(buffer);
} }
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland

View File

@ -25,7 +25,13 @@ Language::Language(const std::string& id, const Bar& bar, const Json::Value& con
AButton::update(); AButton::update();
// register for hyprland ipc // register for hyprland ipc
gIPC->registerForIPC("activelayout", [&](const std::string& ev) { this->onEvent(ev); }); gIPC->registerForIPC("activelayout", this);
}
Language::~Language() {
gIPC->unregisterForIPC(this);
// wait for possible event handler to finish
std::lock_guard<std::mutex> lg(mutex_);
} }
auto Language::update() -> void { auto Language::update() -> void {

View File

@ -25,7 +25,13 @@ Window::Window(const std::string& id, const Bar& bar, const Json::Value& config)
ALabel::update(); ALabel::update();
// register for hyprland ipc // register for hyprland ipc
gIPC->registerForIPC("activewindow", [&](const std::string& ev) { this->onEvent(ev); }); gIPC->registerForIPC("activewindow", this);
}
Window::~Window() {
gIPC->unregisterForIPC(this);
// wait for possible event handler to finish
std::lock_guard<std::mutex> lg(mutex_);
} }
auto Window::update() -> void { auto Window::update() -> void {
@ -50,7 +56,9 @@ uint Window::getActiveWorkspaceID(std::string monitorName) {
assert(json.isArray()); assert(json.isArray());
auto monitor = std::find_if(json.begin(), json.end(), auto monitor = std::find_if(json.begin(), json.end(),
[&](Json::Value monitor) { return monitor["name"] == monitorName; }); [&](Json::Value monitor) { return monitor["name"] == monitorName; });
assert(monitor != std::end(json)); if(monitor == std::end(json)) {
return 0;
}
return (*monitor)["activeWorkspace"]["id"].as<uint>(); return (*monitor)["activeWorkspace"]["id"].as<uint>();
} }
@ -89,4 +97,4 @@ void Window::onEvent(const std::string& ev) {
dp.emit(); dp.emit();
} }
} // namespace waybar::modules::hyprland } // namespace waybar::modules::hyprland