diff options
author | Jose Diez <jose.manuel.diez@gmail.com> | 2015-12-10 16:39:58 +0000 |
---|---|---|
committer | Jose Diez <jose.manuel.diez@gmail.com> | 2015-12-10 16:59:08 +0000 |
commit | 8684b7654d4f78f72f76de88b3f60ee146388d5b (patch) | |
tree | 0a092e10447e2580fba174654992aa4b58d58231 /src | |
parent | c2871c53235b57e90b666fe1ebea6f7b99225357 (diff) | |
download | imv-8684b7654d4f78f72f76de88b3f60ee146388d5b.tar.gz |
Add support for reloading files
Closes #8.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/reload.c | 49 | ||||
-rw-r--r-- | src/reload.h | 31 |
3 files changed, 87 insertions, 1 deletions
@@ -28,6 +28,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "navigator.h" #include "viewport.h" #include "util.h" +#include "reload.h" struct { int fullscreen; @@ -265,6 +266,9 @@ int main(int argc, char** argv) struct imv_viewport view; imv_init_viewport(&view, window); + struct imv_reload reload; + imv_init_reload(&reload); + /* put us in fullscren mode to begin with if requested */ if(g_options.fullscreen) { imv_viewport_toggle_fullscreen(&view); @@ -396,7 +400,7 @@ int main(int argc, char** argv) } /* if the user has changed image, start loading the new one */ - if(imv_navigator_poll_changed(&nav)) { + if(imv_navigator_poll_changed(&nav) || imv_reload_changed(&reload)) { const char *current_path = imv_navigator_selection(&nav); if(!current_path) { fprintf(stderr, "No input files left. Exiting.\n"); @@ -409,6 +413,7 @@ int main(int argc, char** argv) imv_viewport_set_title(&view, title); imv_loader_load_path(&ldr, current_path); + imv_reload_watch(&reload, current_path); view.playing = 1; } @@ -547,6 +552,7 @@ int main(int argc, char** argv) imv_destroy_texture(&tex); imv_navigator_destroy(&nav); imv_destroy_viewport(&view); + imv_destroy_reload(&reload); if(font) { TTF_CloseFont(font); 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); +} diff --git a/src/reload.h b/src/reload.h new file mode 100644 index 0000000..411647f --- /dev/null +++ b/src/reload.h @@ -0,0 +1,31 @@ +#ifndef IMV_RELOAD_H +#define IMV_RELOAD_H + +/* Copyright (c) 2015 Jose Diez + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +struct imv_reload { + int fd; // inotify file descriptor + int wd; // watch descriptor +}; + +void imv_init_reload(struct imv_reload *rld); +void imv_reload_watch(struct imv_reload *rld, const char *path); +int imv_reload_changed(struct imv_reload *rld); +void imv_destroy_reload(struct imv_reload *rld); + +#endif |