From f9662efb1f522c1b554f789659d261a97a1918f5 Mon Sep 17 00:00:00 2001 From: explosion-mental Date: Tue, 26 Apr 2022 20:43:27 -0500 Subject: merge autoreload_{inotify,nop}.c into autoreload.c (#263) --- Makefile | 6 +-- autoreload.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ autoreload_inotify.c | 111 ---------------------------------------- autoreload_nop.c | 41 --------------- 4 files changed, 141 insertions(+), 156 deletions(-) create mode 100644 autoreload.c delete mode 100644 autoreload_inotify.c delete mode 100644 autoreload_nop.c diff --git a/Makefile b/Makefile index e33f0c9..5c08ed0 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ inc_fonts_1 = -I/usr/include/freetype2 -I$(PREFIX)/include/freetype2 CPPFLAGS = -D_XOPEN_SOURCE=700 \ -DHAVE_LIBGIF=$(HAVE_LIBGIF) -DHAVE_LIBEXIF=$(HAVE_LIBEXIF) \ -DHAVE_LIBWEBP=$(HAVE_LIBWEBP) -DHAVE_LIBFONTS=$(HAVE_LIBFONTS) \ - $(inc_fonts_$(HAVE_LIBFONTS)) + -DHAVE_INOTIFY=$(HAVE_INOTIFY) $(inc_fonts_$(HAVE_LIBFONTS)) lib_fonts_0 = lib_fonts_1 = -lXft -lfontconfig @@ -42,14 +42,12 @@ lib_gif_0 = lib_gif_1 = -lgif lib_webp_0 = lib_webp_1 = -lwebpdemux -lwebp -autoreload_0 = nop -autoreload_1 = inotify NSXIV_LDLIBS = -lImlib2 -lX11 \ $(lib_exif_$(HAVE_LIBEXIF)) $(lib_gif_$(HAVE_LIBGIF)) \ $(lib_webp_$(HAVE_LIBWEBP)) $(lib_fonts_$(HAVE_LIBFONTS)) -OBJS = autoreload_$(autoreload_$(HAVE_INOTIFY)).o commands.o image.o main.o options.o \ +OBJS = autoreload.o commands.o image.o main.o options.o \ thumbs.o util.o window.o .SUFFIXES: diff --git a/autoreload.c b/autoreload.c new file mode 100644 index 0000000..d51e008 --- /dev/null +++ b/autoreload.c @@ -0,0 +1,139 @@ +/* Copyright 2017 Max Voit, Bert Muennich + * + * This file is a part of nsxiv. + * + * nsxiv 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. + * + * nsxiv 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 nsxiv. If not, see . + */ + +#include "nsxiv.h" + +#if HAVE_INOTIFY + +#include +#include +#include +#include +#include + +static union { + char d[4096]; /* aligned buffer */ + struct inotify_event e; +} buf; + +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"); +} + +CLEANUP void arl_cleanup(arl_t *arl) +{ + if (arl->fd != -1) + close(arl->fd); + free(arl->filename); +} + +static void rm_watch(int fd, int *wd) +{ + 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, '/'); + + if (arl->fd == -1) + return; + + 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); + } +} + +bool arl_handle(arl_t *arl) +{ + bool reload = false; + char *ptr; + const struct inotify_event *e; + + while (true) { + ssize_t len = read(arl->fd, buf.d, sizeof(buf.d)); + + if (len == -1) { + if (errno == EINTR) + continue; + break; + } + for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) { + e = (const struct inotify_event*) ptr; + if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) { + reload = true; + } else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) { + rm_watch(arl->fd, &arl->wd_file); + } else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) { + if (STREQ(e->name, arl->filename)) + reload = true; + } + } + } + return reload; +} + +#else + +void arl_init(arl_t *arl) +{ + arl->fd = -1; +} + +void arl_cleanup(arl_t *arl) +{ + (void) arl; +} + +void arl_setup(arl_t *arl, const char *filepath) +{ + (void) arl; + (void) filepath; +} + +bool arl_handle(arl_t *arl) +{ + (void) arl; + return false; +} + +#endif /* HAVE_INOTIFY */ diff --git a/autoreload_inotify.c b/autoreload_inotify.c deleted file mode 100644 index 87ce369..0000000 --- a/autoreload_inotify.c +++ /dev/null @@ -1,111 +0,0 @@ -/* Copyright 2017 Max Voit, Bert Muennich - * - * This file is a part of nsxiv. - * - * nsxiv 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. - * - * nsxiv 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 nsxiv. If not, see . - */ - -#include "nsxiv.h" - -#include -#include -#include -#include -#include - -static union { - char d[4096]; /* aligned buffer */ - struct inotify_event e; -} buf; - -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"); -} - -CLEANUP void arl_cleanup(arl_t *arl) -{ - if (arl->fd != -1) - close(arl->fd); - free(arl->filename); -} - -static void rm_watch(int fd, int *wd) -{ - 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, '/'); - - if (arl->fd == -1) - return; - - 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); - } -} - -bool arl_handle(arl_t *arl) -{ - bool reload = false; - char *ptr; - const struct inotify_event *e; - - while (true) { - ssize_t len = read(arl->fd, buf.d, sizeof(buf.d)); - - if (len == -1) { - if (errno == EINTR) - continue; - break; - } - for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) { - e = (const struct inotify_event*) ptr; - if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) { - reload = true; - } else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) { - rm_watch(arl->fd, &arl->wd_file); - } else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) { - if (STREQ(e->name, arl->filename)) - reload = true; - } - } - } - return reload; -} diff --git a/autoreload_nop.c b/autoreload_nop.c deleted file mode 100644 index 4d17f54..0000000 --- a/autoreload_nop.c +++ /dev/null @@ -1,41 +0,0 @@ -/* Copyright 2017 Max Voit - * - * This file is a part of nsxiv. - * - * nsxiv 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. - * - * nsxiv 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 nsxiv. If not, see . - */ - -#include "nsxiv.h" - -void arl_init(arl_t *arl) -{ - arl->fd = -1; -} - -void arl_cleanup(arl_t *arl) -{ - (void) arl; -} - -void arl_setup(arl_t *arl, const char *filepath) -{ - (void) arl; - (void) filepath; -} - -bool arl_handle(arl_t *arl) -{ - (void) arl; - return false; -} -- cgit v1.2.3-54-g00ecf