#pragma once #include #include #include #include #include #include #include #include namespace Waylight { struct FileWatchEvent { std::filesystem::path path; std::uint32_t mask; }; class InotifyWatcher { public: using callback_t = std::function; InotifyWatcher(); ~InotifyWatcher(); InotifyWatcher(InotifyWatcher const &) = delete; InotifyWatcher &operator=(InotifyWatcher const &) = delete; InotifyWatcher(InotifyWatcher &&) = delete; InotifyWatcher &operator=(InotifyWatcher &&) = delete; void watch_path_recursively(std::filesystem::path const &path); void set_callback(callback_t cb); void run(); void stop(); private: void add_watch_for_path(std::filesystem::path const &path); void add_watch_recursively(std::filesystem::path const &path); void remove_watch(int wd); void dispatch_event(int wd, std::uint32_t mask, std::string_view name); static std::filesystem::path normalize_path( std::filesystem::path const &path); int m_fd; std::atomic m_running { false }; callback_t m_callback; std::mutex m_watch_mutex; std::unordered_map m_watch_to_path; std::unordered_map m_path_to_watch; }; } // namespace Waylight