Skip to content

Commit

Permalink
GPU hotplug support
Browse files Browse the repository at this point in the history
  • Loading branch information
maround95 committed Jan 7, 2025
1 parent b9f110e commit 6f030e6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
30 changes: 24 additions & 6 deletions src/managers/eventLoop/EventLoopManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <algorithm>
#include <limits>

#include <map>
#include <sys/timerfd.h>
#include <ctime>

Expand All @@ -16,11 +17,12 @@ CEventLoopManager::CEventLoopManager(wl_display* display, wl_event_loop* wlEvent
m_sTimers.timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
m_sWayland.loop = wlEventLoop;
m_sWayland.display = display;
aqEventSources = std::map<int, SEventSourceData>{};
}

CEventLoopManager::~CEventLoopManager() {
for (auto const& eventSource : m_sWayland.aqEventSources) {
wl_event_source_remove(eventSource);
for (auto const& [_, eventSourceData] : aqEventSources) {
wl_event_source_remove(eventSourceData.eventSource);
}

if (m_sWayland.eventSource)
Expand All @@ -45,10 +47,8 @@ static int aquamarineFDWrite(int fd, uint32_t mask, void* data) {
void CEventLoopManager::enterLoop() {
m_sWayland.eventSource = wl_event_loop_add_fd(m_sWayland.loop, m_sTimers.timerfd, WL_EVENT_READABLE, timerWrite, nullptr);

aqPollFDs = g_pCompositor->m_pAqBackend->getPollFDs();
for (auto const& fd : aqPollFDs) {
m_sWayland.aqEventSources.emplace_back(wl_event_loop_add_fd(m_sWayland.loop, fd->fd, WL_EVENT_READABLE, aquamarineFDWrite, fd.get()));
}
syncPollFDs();
m_sListeners.pollFDsChanged = g_pCompositor->m_pAqBackend->events.pollFDsChanged.registerListener([this](std::any d) { syncPollFDs(); });

// if we have a session, dispatch it to get the pending input devices
if (g_pCompositor->m_pAqBackend->hasSession())
Expand Down Expand Up @@ -133,3 +133,21 @@ void CEventLoopManager::doLater(const std::function<void()>& fn) {
},
&m_sIdle);
}

void CEventLoopManager::syncPollFDs() {
auto aqPollFDs = g_pCompositor->m_pAqBackend->getPollFDs();

for (auto it = aqEventSources.begin(); it != aqEventSources.end();) {
if (std::ranges::all_of(aqPollFDs, [&](SP<Aquamarine::SPollFD> fd) { return fd->fd != it->first; })) {
wl_event_source_remove(it->second.eventSource);
it = aqEventSources.erase(it);
} else {
++it;
}
}

for (auto& fd : aqPollFDs | std::views::filter([&](SP<Aquamarine::SPollFD> fd) { return !aqEventSources.contains(fd->fd); })) {
auto eventSource = wl_event_loop_add_fd(m_sWayland.loop, fd->fd, WL_EVENT_READABLE, aquamarineFDWrite, fd.get());
aqEventSources[fd->fd] = {.pollFD = fd, .eventSource = eventSource};
}
}
22 changes: 17 additions & 5 deletions src/managers/eventLoop/EventLoopManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <mutex>
#include <thread>
#include <wayland-server.h>
#include "helpers/signal/Signal.hpp"

#include "EventLoopTimer.hpp"

Expand Down Expand Up @@ -36,11 +37,18 @@ class CEventLoopManager {
};

private:
// Manages the event sources after AQ pollFDs change.
void syncPollFDs();

struct SEventSourceData {
SP<Aquamarine::SPollFD> pollFD;
wl_event_source* eventSource = nullptr;
};

struct {
wl_event_loop* loop = nullptr;
wl_display* display = nullptr;
wl_event_source* eventSource = nullptr;
std::vector<wl_event_source*> aqEventSources;
wl_event_loop* loop = nullptr;
wl_display* display = nullptr;
wl_event_source* eventSource = nullptr;
} m_sWayland;

struct {
Expand All @@ -49,7 +57,11 @@ class CEventLoopManager {
} m_sTimers;

SIdleData m_sIdle;
std::vector<SP<Aquamarine::SPollFD>> aqPollFDs;
std::map<int, SEventSourceData> aqEventSources;

struct {
CHyprSignalListener pollFDsChanged;
} m_sListeners;

friend class CSyncTimeline;
};
Expand Down

0 comments on commit 6f030e6

Please sign in to comment.