Hyprland/src/helpers/WLListener.cpp

53 lines
1.5 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"
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
2022-07-26 21:41:29 +02:00
pWrap->m_pSelf->emit(data);
2022-03-28 22:31:39 +02:00
}
CHyprWLListener::CHyprWLListener(wl_signal* pSignal, std::function<void(void*, void*)> callback, void* pOwner) {
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()) {
2022-07-20 18:39:08 +02:00
Debug::log(LOG, "Callback %x -> %x, %s removed.", &m_pCallback, &m_pOwner, m_szAuthor.c_str());
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*)> 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);
}