2024-04-07 03:31:51 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <condition_variable>
|
2025-01-23 13:08:19 +02:00
|
|
|
#include <map>
|
2024-04-07 03:31:51 +01:00
|
|
|
#include <mutex>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <wayland-server.h>
|
2025-05-26 16:53:35 +02:00
|
|
|
#include "../../helpers/signal/Signal.hpp"
|
2025-01-30 12:30:12 +01:00
|
|
|
#include <hyprutils/os/FileDescriptor.hpp>
|
2024-04-07 03:31:51 +01:00
|
|
|
|
|
|
|
|
#include "EventLoopTimer.hpp"
|
|
|
|
|
|
2024-07-21 13:09:54 +02:00
|
|
|
namespace Aquamarine {
|
|
|
|
|
struct SPollFD;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-07 03:31:51 +01:00
|
|
|
class CEventLoopManager {
|
|
|
|
|
public:
|
2024-07-05 23:05:03 +02:00
|
|
|
CEventLoopManager(wl_display* display, wl_event_loop* wlEventLoop);
|
2024-06-02 18:42:54 +02:00
|
|
|
~CEventLoopManager();
|
2024-04-08 15:33:02 +01:00
|
|
|
|
2024-07-05 23:05:03 +02:00
|
|
|
void enterLoop();
|
2024-05-07 13:30:31 +01:00
|
|
|
|
|
|
|
|
// Note: will remove the timer if the ptr is lost.
|
2024-05-05 17:16:00 +01:00
|
|
|
void addTimer(SP<CEventLoopTimer> timer);
|
|
|
|
|
void removeTimer(SP<CEventLoopTimer> timer);
|
2024-04-07 03:31:51 +01:00
|
|
|
|
2024-04-07 21:55:14 +01:00
|
|
|
void onTimerFire();
|
|
|
|
|
|
2025-07-19 16:47:14 +02:00
|
|
|
// schedules a recalc of the timers
|
|
|
|
|
void scheduleRecalc();
|
2024-04-07 03:31:51 +01:00
|
|
|
|
2024-07-05 23:05:03 +02:00
|
|
|
// 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;
|
|
|
|
|
};
|
|
|
|
|
|
2025-04-15 18:02:31 -05:00
|
|
|
struct SReadableWaiter {
|
|
|
|
|
wl_event_source* source;
|
|
|
|
|
Hyprutils::OS::CFileDescriptor fd;
|
|
|
|
|
std::function<void()> fn;
|
2025-07-03 21:20:46 +02:00
|
|
|
|
|
|
|
|
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;
|
2025-04-15 18:02:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// schedule function to when fd is readable (WL_EVENT_READABLE / POLLIN),
|
|
|
|
|
// takes ownership of fd
|
2025-08-24 09:57:37 +02:00
|
|
|
void doOnReadable(Hyprutils::OS::CFileDescriptor fd, std::function<void()>&& fn);
|
2025-04-15 18:02:31 -05:00
|
|
|
void onFdReadable(SReadableWaiter* waiter);
|
|
|
|
|
|
2024-04-07 03:31:51 +01:00
|
|
|
private:
|
2025-01-23 13:08:19 +02:00
|
|
|
// Manages the event sources after AQ pollFDs change.
|
|
|
|
|
void syncPollFDs();
|
2025-07-19 16:47:14 +02:00
|
|
|
void nudgeTimers();
|
2025-01-23 13:08:19 +02:00
|
|
|
|
|
|
|
|
struct SEventSourceData {
|
|
|
|
|
SP<Aquamarine::SPollFD> pollFD;
|
|
|
|
|
wl_event_source* eventSource = nullptr;
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-07 03:31:51 +01:00
|
|
|
struct {
|
2025-01-23 13:08:19 +02:00
|
|
|
wl_event_loop* loop = nullptr;
|
|
|
|
|
wl_display* display = nullptr;
|
|
|
|
|
wl_event_source* eventSource = nullptr;
|
2025-05-01 23:57:11 +02:00
|
|
|
} m_wayland;
|
2024-04-07 03:31:51 +01:00
|
|
|
|
|
|
|
|
struct {
|
2024-05-05 17:16:00 +01:00
|
|
|
std::vector<SP<CEventLoopTimer>> timers;
|
2025-01-30 12:30:12 +01:00
|
|
|
Hyprutils::OS::CFileDescriptor timerfd;
|
2025-07-19 16:47:14 +02:00
|
|
|
bool recalcScheduled = false;
|
2025-05-01 23:57:11 +02:00
|
|
|
} m_timers;
|
2024-07-05 23:05:03 +02:00
|
|
|
|
2025-05-01 23:57:11 +02:00
|
|
|
SIdleData m_idle;
|
|
|
|
|
std::map<int, SEventSourceData> m_aqEventSources;
|
|
|
|
|
std::vector<UP<SReadableWaiter>> m_readableWaiters;
|
2025-01-23 13:08:19 +02:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
CHyprSignalListener pollFDsChanged;
|
2025-05-01 23:57:11 +02:00
|
|
|
} m_listeners;
|
2024-07-21 13:09:54 +02:00
|
|
|
|
2025-01-23 13:08:19 +02:00
|
|
|
wl_event_source* m_configWatcherInotifySource = nullptr;
|
2025-01-19 15:39:19 +01:00
|
|
|
|
2025-04-06 17:31:58 +02:00
|
|
|
friend class CAsyncDialogBox;
|
2025-09-18 22:10:30 +02:00
|
|
|
friend class CMainLoopExecutor;
|
2024-04-07 03:31:51 +01:00
|
|
|
};
|
|
|
|
|
|
2025-01-23 21:55:41 +01:00
|
|
|
inline UP<CEventLoopManager> g_pEventLoopManager;
|