Hyprland/src/managers/eventLoop/EventLoopManager.hpp

106 lines
3 KiB
C++
Raw Normal View History

#pragma once
#include <condition_variable>
#include <map>
#include <mutex>
#include <thread>
#include <wayland-server.h>
2025-05-26 16:53:35 +02:00
#include "../../helpers/signal/Signal.hpp"
core: begin using CFileDescriptor from hyprutils (#9122) * config: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * hyprctl: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * ikeyboard: make fd use CFileDescriptor make use of the new CFileDescriptor instead of manual FD handling, also in sendKeymap remove dead code, it already early returns if keyboard isnt valid, and dont try to close the FD that ikeyboard owns. * core: make SHMFile functions use CFileDescriptor make SHMFile misc functions use CFileDescriptor and its associated usage in dmabuf and keyboard. * core: make explicit sync use CFileDescriptor begin using CFileDescriptor in explicit sync and its timelines and eglsync usage in opengl, there is still a bit left with manual handling that requires future aquamarine change aswell. * eventmgr: make fd and sockets use CFileDescriptor make use of the hyprutils CFileDescriptor instead of manual FD and socket handling and closing. * eventloopmgr: make timerfd use CFileDescriptor make the timerfd use CFileDescriptor instead of manual fd handling * opengl: make gbm fd use CFileDescriptor make the gbm rendernode fd use CFileDescriptor instead of manual fd handling * core: make selection source/offer use CFileDescriptor make data selection source and offers use CFileDescriptor and its associated use in xwm and protocols * protocols: convert protocols fd to CFileDescriptor make most fd handling use CFileDescriptor in protocols * shm: make SHMPool use CfileDescriptor make SHMPool use CFileDescriptor instead of manual fd handling. * opengl: duplicate fd with CFileDescriptor duplicate fenceFD with CFileDescriptor duplicate instead. * xwayland: make sockets and fds use CFileDescriptor instead of manual opening/closing make sockets and fds use CFileDescriptor * keybindmgr: make sockets and fds use CFileDescriptor make sockets and fds use CFileDescriptor instead of manual handling.
2025-01-30 12:30:12 +01:00
#include <hyprutils/os/FileDescriptor.hpp>
#include "EventLoopTimer.hpp"
namespace Aquamarine {
struct SPollFD;
};
class CEventLoopManager {
public:
CEventLoopManager(wl_display* display, wl_event_loop* wlEventLoop);
~CEventLoopManager();
2024-04-08 15:33:02 +01:00
void enterLoop();
2024-05-07 13:30:31 +01:00
// Note: will remove the timer if the ptr is lost.
void addTimer(SP<CEventLoopTimer> timer);
void removeTimer(SP<CEventLoopTimer> timer);
void onTimerFire();
// schedules a recalc of the timers
void scheduleRecalc();
// schedules a function to run later, aka in a wayland idle event.
void doLater(const std::function<void()>& fn);
struct SIdleData {
wl_event_source* eventSource = nullptr;
std::vector<std::function<void()>> fns;
};
struct SReadableWaiter {
wl_event_source* source;
Hyprutils::OS::CFileDescriptor fd;
std::function<void()> fn;
SReadableWaiter(wl_event_source* src, Hyprutils::OS::CFileDescriptor f, std::function<void()> func) : source(src), fd(std::move(f)), fn(std::move(func)) {}
~SReadableWaiter() {
if (source) {
wl_event_source_remove(source);
source = nullptr;
}
}
// copy
SReadableWaiter(const SReadableWaiter&) = delete;
SReadableWaiter& operator=(const SReadableWaiter&) = delete;
// move
SReadableWaiter(SReadableWaiter&& other) noexcept = default;
SReadableWaiter& operator=(SReadableWaiter&& other) noexcept = default;
};
// schedule function to when fd is readable (WL_EVENT_READABLE / POLLIN),
// takes ownership of fd
void doOnReadable(Hyprutils::OS::CFileDescriptor fd, std::function<void()>&& fn);
void onFdReadable(SReadableWaiter* waiter);
private:
// Manages the event sources after AQ pollFDs change.
void syncPollFDs();
void nudgeTimers();
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;
} m_wayland;
struct {
std::vector<SP<CEventLoopTimer>> timers;
core: begin using CFileDescriptor from hyprutils (#9122) * config: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * hyprctl: make fd use CFileDescriptor make use of the new hyprutils CFileDescriptor instead of manual FD handling. * ikeyboard: make fd use CFileDescriptor make use of the new CFileDescriptor instead of manual FD handling, also in sendKeymap remove dead code, it already early returns if keyboard isnt valid, and dont try to close the FD that ikeyboard owns. * core: make SHMFile functions use CFileDescriptor make SHMFile misc functions use CFileDescriptor and its associated usage in dmabuf and keyboard. * core: make explicit sync use CFileDescriptor begin using CFileDescriptor in explicit sync and its timelines and eglsync usage in opengl, there is still a bit left with manual handling that requires future aquamarine change aswell. * eventmgr: make fd and sockets use CFileDescriptor make use of the hyprutils CFileDescriptor instead of manual FD and socket handling and closing. * eventloopmgr: make timerfd use CFileDescriptor make the timerfd use CFileDescriptor instead of manual fd handling * opengl: make gbm fd use CFileDescriptor make the gbm rendernode fd use CFileDescriptor instead of manual fd handling * core: make selection source/offer use CFileDescriptor make data selection source and offers use CFileDescriptor and its associated use in xwm and protocols * protocols: convert protocols fd to CFileDescriptor make most fd handling use CFileDescriptor in protocols * shm: make SHMPool use CfileDescriptor make SHMPool use CFileDescriptor instead of manual fd handling. * opengl: duplicate fd with CFileDescriptor duplicate fenceFD with CFileDescriptor duplicate instead. * xwayland: make sockets and fds use CFileDescriptor instead of manual opening/closing make sockets and fds use CFileDescriptor * keybindmgr: make sockets and fds use CFileDescriptor make sockets and fds use CFileDescriptor instead of manual handling.
2025-01-30 12:30:12 +01:00
Hyprutils::OS::CFileDescriptor timerfd;
bool recalcScheduled = false;
} m_timers;
SIdleData m_idle;
std::map<int, SEventSourceData> m_aqEventSources;
std::vector<UP<SReadableWaiter>> m_readableWaiters;
struct {
CHyprSignalListener pollFDsChanged;
} m_listeners;
wl_event_source* m_configWatcherInotifySource = nullptr;
friend class CAsyncDialogBox;
friend class CMainLoopExecutor;
};
inline UP<CEventLoopManager> g_pEventLoopManager;