aboutsummaryrefslogtreecommitdiff
path: root/src/reload.c
diff options
context:
space:
mode:
authorJose Diez <jose.manuel.diez@gmail.com>2015-12-10 16:39:58 +0000
committerJose Diez <jose.manuel.diez@gmail.com>2015-12-10 16:59:08 +0000
commit8684b7654d4f78f72f76de88b3f60ee146388d5b (patch)
tree0a092e10447e2580fba174654992aa4b58d58231 /src/reload.c
parentc2871c53235b57e90b666fe1ebea6f7b99225357 (diff)
downloadimv-8684b7654d4f78f72f76de88b3f60ee146388d5b.tar.gz
Add support for reloading files
Closes #8.
Diffstat (limited to 'src/reload.c')
-rw-r--r--src/reload.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/reload.c b/src/reload.c
new file mode 100644
index 0000000..a307d45
--- /dev/null
+++ b/src/reload.c
@@ -0,0 +1,49 @@
+#include <sys/inotify.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "reload.h"
+
+void imv_init_reload(struct imv_reload *rld)
+{
+ rld->fd = inotify_init1(IN_NONBLOCK);
+ if(rld->fd == -1) {
+ perror("imv_init_reload");
+ }
+
+ rld->wd = 0;
+}
+
+void imv_reload_watch(struct imv_reload *rld, const char *path)
+{
+ if(rld->wd != 0) {
+ inotify_rm_watch(rld->fd, rld->wd);
+ }
+
+ rld->wd = inotify_add_watch(rld->fd, path, IN_CLOSE_WRITE);
+ if(rld->wd == -1) {
+ perror("imv_reload_watch");
+ }
+}
+
+int imv_reload_changed(struct imv_reload *rld)
+{
+ struct inotify_event ev;
+ ssize_t len = read(rld->fd, &ev, sizeof(ev));
+
+ if(len < 0) {
+ if(errno != EAGAIN) {
+ perror("imv_reload_changed");
+ }
+ } else if(ev.mask & IN_CLOSE_WRITE) {
+ return 1;
+ }
+
+ return 0;
+}
+
+void imv_destroy_reload(struct imv_reload *rld)
+{
+ close(rld->fd);
+}