diff --git a/src/notify.h b/src/notify.h new file mode 100644 index 0000000..5c58646 --- /dev/null +++ b/src/notify.h @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include "project.h" + +#define MAX_WDS 16 + +typedef void (Project::*ProjectReloadFn)(); + +class NotifyService { +public: + NotifyService() { + this->inotify_fd = inotify_init1(IN_NONBLOCK); + if (inotify_fd == -1) { + perror("inotify_init1"); + exit(EXIT_FAILURE); + } + + std::cout << "inotify fd: " << inotify_fd << '\n'; + + this->poll_fd = {.fd = inotify_fd, .events = POLLIN, 0}; + } + + void watch(Project &project) { + int wd = inotify_add_watch(this->inotify_fd, project.basepath.str().c_str(), + IN_MODIFY); + + if (wd == -1) { + std::cerr << "Failed to watch project base directory: " << project.basepath.str().c_str() << '\n'; + perror("inotify_add_watch"); + exit(EXIT_FAILURE); + } + + std::cerr << "Watching " << project.basepath << " with wd: " << wd << '\n'; + + this->watch_ids.push_back(wd); + } + + void _poll() { + int rc = poll(&this->poll_fd, 1, 0); + std::cout << "poll() -> " << rc << '\n'; + if (rc == -1) { + if (errno == EINTR) + return; + perror("poll"); + exit(EXIT_FAILURE); + } + + if (rc > 0) { + std::cout << "poll data available!\n"; + } + } +private: + int inotify_fd; + struct pollfd poll_fd; + std::vector watch_ids; +};