From c2fcaa4c6d05fb02006419506234d1eb1f4a7e4b Mon Sep 17 00:00:00 2001 From: gradient Date: Sun, 10 Sep 2017 21:45:37 -0500 Subject: [PATCH] port notify.h over to inotify-tools --- CMakeLists.txt | 1 + src/notify.h | 45 +++++++++++++++------------------------------ 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 72e21af..dbd8a9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,4 +33,5 @@ target_link_libraries(zinnia ${OPENGL_LIBRARIES}) target_link_libraries(zinnia ${GLFW_LIBRARIES}) target_link_libraries(zinnia gl3w) target_link_libraries(zinnia ${GTK3_LIBRARIES}) +target_link_libraries(zinnia inotifytools) #target_link_libraries(zinnia ${CMAKE_SOURCE_DIR}/deps/nfd/build/lib/Release/x64/libnfd.a) diff --git a/src/notify.h b/src/notify.h index 5c58646..1e3de9a 100644 --- a/src/notify.h +++ b/src/notify.h @@ -11,48 +11,33 @@ typedef void (Project::*ProjectReloadFn)(); class NotifyService { public: NotifyService() { - this->inotify_fd = inotify_init1(IN_NONBLOCK); - if (inotify_fd == -1) { - perror("inotify_init1"); + if (!inotifytools_initialize()) { + fprintf(stderr, "inotifytools init error: %s\n", strerror(inotifytools_error())); 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); + inotifytools_watch_recursively(project.basepath.str().c_str(), + IN_MODIFY | IN_MOVED_TO); } void _poll() { - int rc = poll(&this->poll_fd, 1, 0); - std::cout << "poll() -> " << rc << '\n'; - if (rc == -1) { - if (errno == EINTR) - return; - perror("poll"); + struct inotify_event *event = inotifytools_next_event(0); + + if (inotifytools_error() == EINTR) + return; + + if (inotifytools_error() != 0) { + fprintf(stderr, "inotifytools event error: %s\n", strerror(inotifytools_error())); exit(EXIT_FAILURE); } - if (rc > 0) { - std::cout << "poll data available!\n"; + + if (event != NULL) { + std::cout << "new inotify event! file: " << event->name << '\n'; + std::cout << "filename: " << inotifytools_filename_from_wd(event->wd) << '\n'; } } private: - int inotify_fd; - struct pollfd poll_fd; - std::vector watch_ids; };