58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
|
|
namespace Waylight {
|
|
|
|
struct FileWatchEvent {
|
|
std::filesystem::path path;
|
|
std::uint32_t mask;
|
|
};
|
|
|
|
class InotifyWatcher {
|
|
public:
|
|
using callback_t = std::function<void(FileWatchEvent const &)>;
|
|
|
|
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<bool> m_running { false };
|
|
callback_t m_callback;
|
|
|
|
std::mutex m_watch_mutex;
|
|
std::unordered_map<int, std::filesystem::path> m_watch_to_path;
|
|
std::unordered_map<std::string, int> m_path_to_watch;
|
|
};
|
|
|
|
} // namespace Waylight
|