Hyprland/src/helpers/WLListener.cpp

63 lines
1.9 KiB
C++
Raw Normal View History

2022-03-28 22:31:39 +02:00
#include "WLListener.hpp"
#include "MiscFunctions.hpp"
#include <string>
2022-03-31 17:25:23 +02:00
#include "../debug/Log.hpp"
#include "Watchdog.hpp"
2022-03-28 22:31:39 +02:00
void handleWrapped(wl_listener* listener, void* data) {
2022-07-26 21:41:29 +02:00
CHyprWLListener::SWrapper* pWrap = wl_container_of(listener, pWrap, m_sListener);
2022-03-28 22:31:39 +02:00
2024-01-19 16:20:22 +01:00
if (g_pWatchdog)
g_pWatchdog->startWatching();
try {
pWrap->m_pSelf->emit(data);
} catch (std::exception& e) { Debug::log(ERR, "Listener {} threw or timed out and was killed by Watchdog!!! This is bad. what(): {}", (uintptr_t)listener, e.what()); }
2024-01-19 16:20:22 +01:00
if (g_pWatchdog)
g_pWatchdog->endWatching();
2022-03-28 22:31:39 +02:00
}
CHyprWLListener::CHyprWLListener(wl_signal* pSignal, std::function<void(void*, void*)> const& callback, void* pOwner) {
2022-03-28 22:31:39 +02:00
initCallback(pSignal, callback, pOwner);
}
CHyprWLListener::CHyprWLListener() {
m_swWrapper.m_pSelf = this;
2022-07-26 21:41:29 +02:00
m_swWrapper.m_sListener.notify = &handleWrapped;
wl_list_init(&m_swWrapper.m_sListener.link);
2022-03-28 22:31:39 +02:00
}
CHyprWLListener::~CHyprWLListener() {
removeCallback();
}
void CHyprWLListener::removeCallback() {
2022-07-26 21:41:29 +02:00
if (isConnected()) {
Debug::log(LOG, "Callback {:x} -> {:x}, {} removed.", (uintptr_t)&m_pCallback, (uintptr_t)&m_pOwner, m_szAuthor);
2022-07-26 21:41:29 +02:00
wl_list_remove(&m_swWrapper.m_sListener.link);
wl_list_init(&m_swWrapper.m_sListener.link);
2022-03-28 22:31:39 +02:00
}
}
bool CHyprWLListener::isConnected() {
2022-07-26 21:41:29 +02:00
return !wl_list_empty(&m_swWrapper.m_sListener.link);
2022-03-28 22:31:39 +02:00
}
void CHyprWLListener::initCallback(wl_signal* pSignal, std::function<void(void*, void*)> const& callback, void* pOwner, std::string author) {
2022-07-26 21:41:29 +02:00
if (isConnected()) {
Debug::log(ERR, "Tried to connect a listener twice?!");
return;
}
2022-04-10 14:32:18 +02:00
m_pOwner = pOwner;
2022-03-28 22:31:39 +02:00
m_pCallback = callback;
m_szAuthor = author;
2022-03-28 22:31:39 +02:00
2022-07-26 21:41:29 +02:00
addWLSignal(pSignal, &m_swWrapper.m_sListener, pOwner, m_szAuthor);
2022-03-28 22:31:39 +02:00
}
void CHyprWLListener::emit(void* data) {
m_pCallback(m_pOwner, data);
}