Merge pull request #51 from eXeC64/imv_reload

Add support for reloading files
This commit is contained in:
Harry Jeffery 2015-12-10 17:56:25 +00:00
commit edad12139b
3 changed files with 87 additions and 1 deletions

View file

@ -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);

49
src/reload.c Normal file
View file

@ -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);
}

31
src/reload.h Normal file
View file

@ -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