2017-01-26 21:18:32 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
2017-05-17 18:14:36 +00:00
|
|
|
#include <errno.h>
|
2017-01-26 21:18:32 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "autoreload.h"
|
|
|
|
|
2017-05-17 18:16:16 +00:00
|
|
|
void arl_init(arl_t *arl)
|
|
|
|
{
|
|
|
|
arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
|
|
|
|
arl->wd_dir = arl->wd_file = -1;
|
|
|
|
if (arl->fd == -1)
|
|
|
|
error(0, 0, "Could not initialize inotify, no automatic image reloading");
|
|
|
|
}
|
|
|
|
|
2017-05-17 18:07:32 +00:00
|
|
|
CLEANUP void arl_cleanup(arl_t *arl)
|
2017-01-26 21:18:32 +00:00
|
|
|
{
|
2017-05-17 18:14:20 +00:00
|
|
|
if (arl->fd != -1)
|
|
|
|
close(arl->fd);
|
2017-05-17 18:16:16 +00:00
|
|
|
free(arl->filename);
|
2017-01-26 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:16:16 +00:00
|
|
|
static void rm_watch(int fd, int *wd)
|
2017-05-17 18:07:32 +00:00
|
|
|
{
|
2017-05-17 18:16:16 +00:00
|
|
|
if (*wd != -1) {
|
|
|
|
inotify_rm_watch(fd, *wd);
|
|
|
|
*wd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
|
|
|
|
{
|
|
|
|
*wd = inotify_add_watch(fd, path, mask);
|
|
|
|
if (*wd == -1)
|
|
|
|
error(0, errno, "inotify: %s", path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void arl_setup(arl_t *arl, const char *filepath)
|
|
|
|
{
|
|
|
|
char *base = strrchr(filepath, '/');
|
2017-05-17 18:07:32 +00:00
|
|
|
|
2017-05-17 18:13:32 +00:00
|
|
|
if (arl->fd == -1)
|
2017-05-17 18:07:32 +00:00
|
|
|
return;
|
|
|
|
|
2017-05-17 18:16:16 +00:00
|
|
|
rm_watch(arl->fd, &arl->wd_dir);
|
|
|
|
rm_watch(arl->fd, &arl->wd_file);
|
|
|
|
|
|
|
|
add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
|
|
|
|
|
|
|
|
free(arl->filename);
|
|
|
|
arl->filename = estrdup(filepath);
|
|
|
|
|
|
|
|
if (base != NULL) {
|
|
|
|
arl->filename[++base - filepath] = '\0';
|
|
|
|
add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO);
|
|
|
|
strcpy(arl->filename, base);
|
|
|
|
}
|
2017-05-17 18:07:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 18:15:35 +00:00
|
|
|
union {
|
|
|
|
char d[4096]; /* aligned buffer */
|
|
|
|
struct inotify_event e;
|
|
|
|
} buf;
|
|
|
|
|
2017-05-17 18:16:16 +00:00
|
|
|
bool arl_handle(arl_t *arl)
|
2017-01-26 21:18:32 +00:00
|
|
|
{
|
2017-05-17 18:07:32 +00:00
|
|
|
bool reload = false;
|
2017-01-26 21:18:32 +00:00
|
|
|
char *ptr;
|
2017-05-17 18:12:22 +00:00
|
|
|
const struct inotify_event *event;
|
|
|
|
|
2017-05-17 18:14:36 +00:00
|
|
|
for (;;) {
|
2017-05-17 18:15:35 +00:00
|
|
|
ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
|
2017-01-26 21:18:32 +00:00
|
|
|
|
2017-05-17 18:14:36 +00:00
|
|
|
if (len == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
break;
|
2017-01-26 21:18:32 +00:00
|
|
|
}
|
2017-05-17 18:15:35 +00:00
|
|
|
for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) {
|
2017-05-17 18:14:36 +00:00
|
|
|
event = (const struct inotify_event*) ptr;
|
|
|
|
if (event->mask & IN_CLOSE_WRITE) {
|
2017-05-17 18:07:32 +00:00
|
|
|
reload = true;
|
2017-05-17 18:16:16 +00:00
|
|
|
} else if (event->mask & IN_DELETE_SELF) {
|
|
|
|
rm_watch(arl->fd, &arl->wd_file);
|
|
|
|
} else if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
|
|
|
|
if (STREQ(event->name, arl->filename))
|
2017-05-17 18:14:36 +00:00
|
|
|
reload = true;
|
|
|
|
}
|
2017-01-26 21:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 18:07:32 +00:00
|
|
|
return reload;
|
2017-01-26 21:18:32 +00:00
|
|
|
}
|
|
|
|
|