nsxivedb117e
Add autoreload support by inotify (and dummy backend nop)
+247 −1
5 files changed
- M Makefile +7 −0
- A autoreload.h +43 −0
- A autoreload_inotify.c +154 −0
- A autoreload_nop.c +31 −0
- M main.c +12 −1
| @@ -24,6 +24,13 @@ endif | |||
| 24 | 24 | .PHONY: clean install uninstall | |
| 25 | 25 | ||
| 26 | 26 | SRC := commands.c image.c main.c options.c thumbs.c util.c window.c | |
| 27 | + | # conditionally compile in autoreload-backend; usage: `make AUTORELOAD=nop` | |
| 28 | + | ifeq ($(AUTORELOAD),nop) | |
| 29 | + | SRC += autoreload_nop.c | |
| 30 | + | else | |
| 31 | + | SRC += autoreload_inotify.c | |
| 32 | + | endif | |
| 33 | + | ||
| 27 | 34 | DEP := $(SRC:.c=.d) | |
| 28 | 35 | OBJ := $(SRC:.c=.o) | |
| 29 | 36 | ||
| @@ -0,0 +1,43 @@ | |||
| 1 | + | /* Copyright 2017 Max Voit | |
| 2 | + | * | |
| 3 | + | * This file is part of sxiv. | |
| 4 | + | * | |
| 5 | + | * sxiv is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published | |
| 7 | + | * by the Free Software Foundation; either version 2 of the License, | |
| 8 | + | * or (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * sxiv is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License | |
| 16 | + | * along with sxiv. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | + | */ | |
| 18 | + | ||
| 19 | + | #ifndef AUTORELOAD_H | |
| 20 | + | #define AUTORELOAD_H | |
| 21 | + | ||
| 22 | + | #include "types.h" | |
| 23 | + | ||
| 24 | + | void arl_cleanup(void); | |
| 25 | + | void arl_handle(void); | |
| 26 | + | void arl_init(void); | |
| 27 | + | void arl_setup(void); | |
| 28 | + | void arl_setup_dir(void); | |
| 29 | + | ||
| 30 | + | typedef struct { | |
| 31 | + | int fd; | |
| 32 | + | int wd; | |
| 33 | + | bool watching_dir; | |
| 34 | + | } autoreload_t; | |
| 35 | + | ||
| 36 | + | extern autoreload_t autoreload; | |
| 37 | + | extern int fileidx; | |
| 38 | + | extern fileinfo_t *files; | |
| 39 | + | ||
| 40 | + | void load_image(int); | |
| 41 | + | void redraw(void); | |
| 42 | + | ||
| 43 | + | #endif /* AUTORELOAD_H */ | |
| @@ -0,0 +1,154 @@ | |||
| 1 | + | /* Copyright 2017 Max Voit | |
| 2 | + | * | |
| 3 | + | * This file is part of sxiv. | |
| 4 | + | * | |
| 5 | + | * sxiv is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published | |
| 7 | + | * by the Free Software Foundation; either version 2 of the License, | |
| 8 | + | * or (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * sxiv is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License | |
| 16 | + | * along with sxiv. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | + | */ | |
| 18 | + | ||
| 19 | + | #include <stdlib.h> | |
| 20 | + | #include <string.h> | |
| 21 | + | #include <unistd.h> | |
| 22 | + | #include <sys/inotify.h> | |
| 23 | + | #include <libgen.h> | |
| 24 | + | #include <time.h> | |
| 25 | + | ||
| 26 | + | #include "util.h" | |
| 27 | + | #include "autoreload.h" | |
| 28 | + | ||
| 29 | + | const struct timespec ten_ms = {0, 10000000}; | |
| 30 | + | ||
| 31 | + | void arl_cleanup(void) | |
| 32 | + | { | |
| 33 | + | if (autoreload.fd != -1 && autoreload.wd != -1) | |
| 34 | + | { | |
| 35 | + | if(inotify_rm_watch(autoreload.fd, autoreload.wd)) | |
| 36 | + | error(0, 0, "Failed to remove inotify watch."); | |
| 37 | + | } | |
| 38 | + | } | |
| 39 | + | ||
| 40 | + | void arl_handle(void) | |
| 41 | + | { | |
| 42 | + | ssize_t len; | |
| 43 | + | char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); | |
| 44 | + | const struct inotify_event *event; | |
| 45 | + | char *ptr; | |
| 46 | + | char *fntmp, *fn; | |
| 47 | + | ||
| 48 | + | len = read(autoreload.fd, buf, sizeof buf); | |
| 49 | + | if (len == -1) | |
| 50 | + | { | |
| 51 | + | error(0, 0, "Failed to read inotify events."); | |
| 52 | + | return; | |
| 53 | + | } | |
| 54 | + | ||
| 55 | + | for (ptr = buf; ptr < buf + len; | |
| 56 | + | ptr += sizeof(struct inotify_event) + event->len) | |
| 57 | + | { | |
| 58 | + | ||
| 59 | + | event = (const struct inotify_event *) ptr; | |
| 60 | + | ||
| 61 | + | /* events from watching the file itself */ | |
| 62 | + | if (event->mask & IN_CLOSE_WRITE) | |
| 63 | + | { | |
| 64 | + | load_image(fileidx); | |
| 65 | + | redraw(); | |
| 66 | + | } | |
| 67 | + | ||
| 68 | + | if (event->mask & IN_DELETE_SELF) | |
| 69 | + | arl_setup_dir(); | |
| 70 | + | ||
| 71 | + | /* events from watching the file's directory */ | |
| 72 | + | if (event->mask & IN_CREATE) | |
| 73 | + | { | |
| 74 | + | fntmp = strdup(files[fileidx].path); | |
| 75 | + | fn = basename(fntmp); | |
| 76 | + | ||
| 77 | + | if (0 == strcmp(event->name, fn)) | |
| 78 | + | { | |
| 79 | + | /* this is the file we're looking for */ | |
| 80 | + | ||
| 81 | + | /* cleanup, this has not been one-shot */ | |
| 82 | + | if (autoreload.watching_dir) | |
| 83 | + | { | |
| 84 | + | if(inotify_rm_watch(autoreload.fd, autoreload.wd)) | |
| 85 | + | error(0, 0, "Failed to remove inotify watch."); | |
| 86 | + | autoreload.watching_dir = false; | |
| 87 | + | } | |
| 88 | + | ||
| 89 | + | /* when too fast, imlib2 can't load the image */ | |
| 90 | + | nanosleep(&ten_ms, NULL); | |
| 91 | + | load_image(fileidx); | |
| 92 | + | redraw(); | |
| 93 | + | } | |
| 94 | + | free(fntmp); | |
| 95 | + | } | |
| 96 | + | } | |
| 97 | + | } | |
| 98 | + | ||
| 99 | + | void arl_init(void) | |
| 100 | + | { | |
| 101 | + | /* this needs to be done only once */ | |
| 102 | + | autoreload.fd = inotify_init(); | |
| 103 | + | autoreload.watching_dir = false; | |
| 104 | + | if (autoreload.fd == -1) | |
| 105 | + | error(0, 0, "Could not initialize inotify."); | |
| 106 | + | } | |
| 107 | + | ||
| 108 | + | void arl_setup(void) | |
| 109 | + | { | |
| 110 | + | if (autoreload.fd == -1) | |
| 111 | + | { | |
| 112 | + | error(0, 0, "Uninitialized, could not add inotify watch."); | |
| 113 | + | return; | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | /* may have switched from a deleted to another image */ | |
| 117 | + | if (autoreload.watching_dir) | |
| 118 | + | { | |
| 119 | + | if (inotify_rm_watch(autoreload.fd, autoreload.wd)) | |
| 120 | + | error(0, 0, "Failed to remove inotify watch."); | |
| 121 | + | autoreload.watching_dir = false; | |
| 122 | + | } | |
| 123 | + | ||
| 124 | + | autoreload.wd = inotify_add_watch(autoreload.fd, files[fileidx].path, | |
| 125 | + | IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF); | |
| 126 | + | if (autoreload.wd == -1) | |
| 127 | + | error(0, 0, "Failed to add inotify watch on file '%s'.", files[fileidx].path); | |
| 128 | + | } | |
| 129 | + | ||
| 130 | + | void arl_setup_dir(void) | |
| 131 | + | { | |
| 132 | + | char *dntmp, *dn; | |
| 133 | + | ||
| 134 | + | if (autoreload.fd == -1) | |
| 135 | + | { | |
| 136 | + | error(0, 0, "Uninitialized, could not add inotify watch on directory."); | |
| 137 | + | return; | |
| 138 | + | } | |
| 139 | + | ||
| 140 | + | /* get dirname */ | |
| 141 | + | dntmp = (char*) strdup(files[fileidx].path); | |
| 142 | + | dn = (char*) dirname(dntmp); | |
| 143 | + | ||
| 144 | + | /* this is not one-shot as other stuff may be created too | |
| 145 | + | note: we won't handle deletion of the directory itself, | |
| 146 | + | this is a design decision */ | |
| 147 | + | autoreload.wd = inotify_add_watch(autoreload.fd, dn,IN_CREATE); | |
| 148 | + | if (autoreload.wd == -1) | |
| 149 | + | error(0, 0, "Failed to add inotify watch on directory '%s'.", dn); | |
| 150 | + | else | |
| 151 | + | autoreload.watching_dir = true; | |
| 152 | + | ||
| 153 | + | free(dntmp); | |
| 154 | + | } | |
| @@ -0,0 +1,31 @@ | |||
| 1 | + | /* Copyright 2017 Max Voit | |
| 2 | + | * | |
| 3 | + | * This file is part of sxiv. | |
| 4 | + | * | |
| 5 | + | * sxiv is free software; you can redistribute it and/or modify | |
| 6 | + | * it under the terms of the GNU General Public License as published | |
| 7 | + | * by the Free Software Foundation; either version 2 of the License, | |
| 8 | + | * or (at your option) any later version. | |
| 9 | + | * | |
| 10 | + | * sxiv is distributed in the hope that it will be useful, | |
| 11 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | + | * GNU General Public License for more details. | |
| 14 | + | * | |
| 15 | + | * You should have received a copy of the GNU General Public License | |
| 16 | + | * along with sxiv. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | + | */ | |
| 18 | + | ||
| 19 | + | #include "autoreload.h" | |
| 20 | + | ||
| 21 | + | void arl_cleanup(void) { } | |
| 22 | + | ||
| 23 | + | void arl_handle(void) { } | |
| 24 | + | ||
| 25 | + | void arl_init(void) { } | |
| 26 | + | ||
| 27 | + | void arl_setup(void) { } | |
| 28 | + | ||
| 29 | + | void arl_setup_dir(void) { } | |
| 30 | + | ||
| 31 | + | ||
| @@ -38,6 +38,7 @@ | |||
| 38 | 38 | #include "thumbs.h" | |
| 39 | 39 | #include "util.h" | |
| 40 | 40 | #include "window.h" | |
| 41 | + | #include "autoreload.h" | |
| 41 | 42 | ||
| 42 | 43 | #define _MAPPINGS_CONFIG | |
| 43 | 44 | #include "config.h" | |
| @@ -64,6 +65,7 @@ fileinfo_t *files; | |||
| 64 | 65 | int filecnt, fileidx; | |
| 65 | 66 | int alternate; | |
| 66 | 67 | int markcnt; | |
| 68 | + | autoreload_t autoreload; | |
| 67 | 69 | ||
| 68 | 70 | int prefix; | |
| 69 | 71 | bool extprefix; | |
| @@ -98,6 +100,7 @@ timeout_t timeouts[] = { | |||
| 98 | 100 | void cleanup(void) | |
| 99 | 101 | { | |
| 100 | 102 | img_close(&img, false); | |
| 103 | + | arl_cleanup(); | |
| 101 | 104 | tns_free(&tns); | |
| 102 | 105 | win_close(&win); | |
| 103 | 106 | } | |
| @@ -317,6 +320,7 @@ void load_image(int new) | |||
| 317 | 320 | ||
| 318 | 321 | info.open = false; | |
| 319 | 322 | open_info(); | |
| 323 | + | arl_setup(); | |
| 320 | 324 | ||
| 321 | 325 | if (img.multi.cnt > 0 && img.multi.animate) | |
| 322 | 326 | set_timeout(animate, img.multi.frames[img.multi.sel].delay, true); | |
| @@ -685,7 +689,7 @@ void run(void) | |||
| 685 | 689 | init_thumb = mode == MODE_THUMB && tns.initnext < filecnt; | |
| 686 | 690 | load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end; | |
| 687 | 691 | ||
| 688 | − | if ((init_thumb || load_thumb || to_set || info.fd != -1) && | |
| 692 | + | if ((init_thumb || load_thumb || to_set || info.fd != -1 || autoreload.fd != -1) && | |
| 689 | 693 | XPending(win.env.dpy) == 0) | |
| 690 | 694 | { | |
| 691 | 695 | if (load_thumb) { | |
| @@ -708,9 +712,15 @@ void run(void) | |||
| 708 | 712 | FD_SET(info.fd, &fds); | |
| 709 | 713 | xfd = MAX(xfd, info.fd); | |
| 710 | 714 | } | |
| 715 | + | if (autoreload.fd != -1) { | |
| 716 | + | FD_SET(autoreload.fd, &fds); | |
| 717 | + | xfd = MAX(xfd, autoreload.fd); | |
| 718 | + | } | |
| 711 | 719 | select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL); | |
| 712 | 720 | if (info.fd != -1 && FD_ISSET(info.fd, &fds)) | |
| 713 | 721 | read_info(); | |
| 722 | + | if (autoreload.fd != -1 && FD_ISSET(autoreload.fd, &fds)) | |
| 723 | + | arl_handle(); | |
| 714 | 724 | } | |
| 715 | 725 | continue; | |
| 716 | 726 | } | |
| @@ -854,6 +864,7 @@ int main(int argc, char **argv) | |||
| 854 | 864 | ||
| 855 | 865 | win_init(&win); | |
| 856 | 866 | img_init(&img, &win); | |
| 867 | + | arl_init(); | |
| 857 | 868 | ||
| 858 | 869 | if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') { | |
| 859 | 870 | homedir = getenv("HOME"); | |