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"
2023-10-01 13:26:10 +02:00
# 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 ( ) ;
2023-09-29 17:38:13 +02:00
try {
pWrap - > m_pSelf - > emit ( data ) ;
2024-04-27 13:43:12 +02:00
} 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 ( ) ) ; }
2023-09-29 17:38:13 +02:00
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 * ) > callback , void * pOwner ) {
initCallback ( pSignal , callback , pOwner ) ;
}
CHyprWLListener : : CHyprWLListener ( ) {
2022-12-16 18:17:31 +01:00
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 ( ) ) {
2023-09-06 21:45:37 +02:00
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 * ) > 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
2022-12-16 18:17:31 +01:00
m_pOwner = pOwner ;
2022-03-28 22:31:39 +02:00
m_pCallback = callback ;
2022-12-16 18:17:31 +01:00
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 ) ;
2023-10-01 13:26:10 +02:00
}