2017-01-26 22:18:32 +01:00
|
|
|
/* Copyright 2017 Max Voit
|
|
|
|
*
|
|
|
|
* This file is part of sxiv.
|
|
|
|
*
|
|
|
|
* sxiv 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.
|
|
|
|
*
|
|
|
|
* sxiv 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 sxiv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "autoreload.h"
|
|
|
|
|
2017-05-17 20:07:32 +02:00
|
|
|
CLEANUP void arl_cleanup(arl_t *arl)
|
2017-01-26 22:18:32 +01:00
|
|
|
{
|
2017-05-17 20:14:20 +02:00
|
|
|
if (arl->fd != -1)
|
|
|
|
close(arl->fd);
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 20:07:32 +02:00
|
|
|
static void arl_setup_dir(arl_t *arl, const char *filepath)
|
|
|
|
{
|
|
|
|
char *dntmp, *dn;
|
|
|
|
|
2017-05-17 20:13:32 +02:00
|
|
|
if (arl->fd == -1)
|
2017-05-17 20:07:32 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* get dirname */
|
|
|
|
dntmp = (char*) strdup(filepath);
|
|
|
|
dn = (char*) dirname(dntmp);
|
|
|
|
|
|
|
|
/* this is not one-shot as other stuff may be created too
|
2017-05-17 20:12:22 +02:00
|
|
|
* note: we won't handle deletion of the directory itself,
|
|
|
|
* this is a design decision
|
|
|
|
*/
|
|
|
|
arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE);
|
2017-05-17 20:07:32 +02:00
|
|
|
if (arl->wd == -1)
|
2017-05-17 20:13:32 +02:00
|
|
|
error(0, 0, "%s: Error watching directory", dn);
|
2017-05-17 20:07:32 +02:00
|
|
|
else
|
|
|
|
arl->watching_dir = true;
|
|
|
|
|
|
|
|
free(dntmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool arl_handle(arl_t *arl, const char *filepath)
|
2017-01-26 22:18:32 +01:00
|
|
|
{
|
2017-05-17 20:07:32 +02:00
|
|
|
bool reload = false;
|
2017-01-26 22:18:32 +01:00
|
|
|
char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
|
|
|
|
char *ptr;
|
2017-05-17 20:12:22 +02:00
|
|
|
const struct inotify_event *event;
|
|
|
|
|
|
|
|
ssize_t len = read(arl->fd, buf, sizeof(buf));
|
2017-01-26 22:18:32 +01:00
|
|
|
|
2017-05-17 20:13:32 +02:00
|
|
|
if (len == -1)
|
2017-05-17 20:07:32 +02:00
|
|
|
return false;
|
2017-05-17 20:13:32 +02:00
|
|
|
|
2017-05-17 20:12:22 +02:00
|
|
|
for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
|
|
|
|
event = (const struct inotify_event*) ptr;
|
2017-01-26 22:18:32 +01:00
|
|
|
|
|
|
|
/* events from watching the file itself */
|
2017-05-17 20:12:22 +02:00
|
|
|
if (event->mask & IN_CLOSE_WRITE) {
|
2017-05-17 20:07:32 +02:00
|
|
|
reload = true;
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
if (event->mask & IN_DELETE_SELF)
|
2017-05-17 20:07:32 +02:00
|
|
|
arl_setup_dir(arl, filepath);
|
2017-01-26 22:18:32 +01:00
|
|
|
|
|
|
|
/* events from watching the file's directory */
|
2017-05-17 20:12:22 +02:00
|
|
|
if (event->mask & IN_CREATE) {
|
|
|
|
char *fntmp = strdup(filepath);
|
|
|
|
char *fn = basename(fntmp);
|
2017-01-26 22:18:32 +01:00
|
|
|
|
2017-05-17 20:12:22 +02:00
|
|
|
if (STREQ(event->name, fn)) {
|
2017-01-26 22:18:32 +01:00
|
|
|
/* this is the file we're looking for */
|
|
|
|
|
|
|
|
/* cleanup, this has not been one-shot */
|
2017-05-17 20:12:22 +02:00
|
|
|
if (arl->watching_dir) {
|
2017-05-17 20:13:32 +02:00
|
|
|
inotify_rm_watch(arl->fd, arl->wd);
|
2017-05-17 20:07:32 +02:00
|
|
|
arl->watching_dir = false;
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
2017-05-17 20:07:32 +02:00
|
|
|
reload = true;
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
free(fntmp);
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 20:07:32 +02:00
|
|
|
return reload;
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 20:07:32 +02:00
|
|
|
void arl_init(arl_t *arl)
|
2017-01-26 22:18:32 +01:00
|
|
|
{
|
|
|
|
/* this needs to be done only once */
|
2017-05-17 20:07:32 +02:00
|
|
|
arl->fd = inotify_init();
|
|
|
|
arl->watching_dir = false;
|
|
|
|
if (arl->fd == -1)
|
2017-05-17 20:13:32 +02:00
|
|
|
error(0, 0, "Could not initialize inotify, no automatic image reloading");
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 20:07:32 +02:00
|
|
|
void arl_setup(arl_t *arl, const char *filepath)
|
2017-01-26 22:18:32 +01:00
|
|
|
{
|
2017-05-17 20:13:32 +02:00
|
|
|
if (arl->fd == -1)
|
2017-01-26 22:18:32 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* may have switched from a deleted to another image */
|
2017-05-17 20:12:22 +02:00
|
|
|
if (arl->watching_dir) {
|
2017-05-17 20:13:32 +02:00
|
|
|
inotify_rm_watch(arl->fd, arl->wd);
|
2017-05-17 20:07:32 +02:00
|
|
|
arl->watching_dir = false;
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-17 20:07:32 +02:00
|
|
|
arl->wd = inotify_add_watch(arl->fd, filepath,
|
2017-05-17 20:12:22 +02:00
|
|
|
IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
|
2017-05-17 20:07:32 +02:00
|
|
|
if (arl->wd == -1)
|
2017-05-17 20:13:32 +02:00
|
|
|
error(0, 0, "%s: Error watching file", filepath);
|
2017-01-26 22:18:32 +01:00
|
|
|
}
|
|
|
|
|