5
5
6
6
#include < algorithm>
7
7
#include < limits>
8
+ #include < ranges>
8
9
9
10
#include < sys/timerfd.h>
10
11
#include < ctime>
@@ -20,8 +21,8 @@ CEventLoopManager::CEventLoopManager(wl_display* display, wl_event_loop* wlEvent
20
21
}
21
22
22
23
CEventLoopManager::~CEventLoopManager () {
23
- for (auto const & eventSource : m_sWayland. aqEventSources ) {
24
- wl_event_source_remove (eventSource);
24
+ for (auto const & [_, eventSourceData] : aqEventSources) {
25
+ wl_event_source_remove (eventSourceData. eventSource );
25
26
}
26
27
27
28
if (m_sWayland.eventSource )
@@ -56,10 +57,8 @@ void CEventLoopManager::enterLoop() {
56
57
if (const auto FD = g_pConfigWatcher->getInotifyFD (); FD >= 0 )
57
58
m_configWatcherInotifySource = wl_event_loop_add_fd (m_sWayland.loop , FD, WL_EVENT_READABLE, configWatcherWrite, nullptr );
58
59
59
- aqPollFDs = g_pCompositor->m_pAqBackend ->getPollFDs ();
60
- for (auto const & fd : aqPollFDs) {
61
- m_sWayland.aqEventSources .emplace_back (wl_event_loop_add_fd (m_sWayland.loop , fd->fd , WL_EVENT_READABLE, aquamarineFDWrite, fd.get ()));
62
- }
60
+ syncPollFDs ();
61
+ m_sListeners.pollFDsChanged = g_pCompositor->m_pAqBackend ->events .pollFDsChanged .registerListener ([this ](std::any d) { syncPollFDs (); });
63
62
64
63
// if we have a session, dispatch it to get the pending input devices
65
64
if (g_pCompositor->m_pAqBackend ->hasSession ())
@@ -144,3 +143,24 @@ void CEventLoopManager::doLater(const std::function<void()>& fn) {
144
143
},
145
144
&m_sIdle);
146
145
}
146
+
147
+ void CEventLoopManager::syncPollFDs () {
148
+ auto aqPollFDs = g_pCompositor->m_pAqBackend ->getPollFDs ();
149
+
150
+ std::erase_if (aqEventSources, [&](const auto & item) {
151
+ auto const & [fd, eventSourceData] = item;
152
+
153
+ // If no pollFD has the same fd, remove this event source
154
+ const bool shouldRemove = std::ranges::none_of (aqPollFDs, [&](const auto & pollFD) { return pollFD->fd == fd; });
155
+
156
+ if (shouldRemove)
157
+ wl_event_source_remove (eventSourceData.eventSource );
158
+
159
+ return shouldRemove;
160
+ });
161
+
162
+ for (auto & fd : aqPollFDs | std::views::filter ([&](SP<Aquamarine::SPollFD> fd) { return !aqEventSources.contains (fd->fd ); })) {
163
+ auto eventSource = wl_event_loop_add_fd (m_sWayland.loop , fd->fd , WL_EVENT_READABLE, aquamarineFDWrite, fd.get ());
164
+ aqEventSources[fd->fd ] = {.pollFD = fd, .eventSource = eventSource};
165
+ }
166
+ }
0 commit comments