| 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 <errno.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <unistd.h> |
| 23 | #include <sys/inotify.h> |
| 24 | |
| 25 | #include "util.h" |
| 26 | #include "autoreload.h" |
| 27 | |
| 28 | void arl_init(arl_t *arl) |
| 29 | { |
| 30 | arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); |
| 31 | arl->wd_dir = arl->wd_file = -1; |
| 32 | if (arl->fd == -1) |
| 33 | error(0, 0, "Could not initialize inotify, no automatic image reloading"); |
| 34 | } |
| 35 | |
| 36 | CLEANUP void arl_cleanup(arl_t *arl) |
| 37 | { |
| 38 | if (arl->fd != -1) |
| 39 | close(arl->fd); |
| 40 | free(arl->filename); |
| 41 | } |
| 42 | |
| 43 | static void rm_watch(int fd, int *wd) |
| 44 | { |
| 45 | if (*wd != -1) { |
| 46 | inotify_rm_watch(fd, *wd); |
| 47 | *wd = -1; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static void add_watch(int fd, int *wd, const char *path, uint32_t mask) |
| 52 | { |
| 53 | *wd = inotify_add_watch(fd, path, mask); |
| 54 | if (*wd == -1) |
| 55 | error(0, errno, "inotify: %s", path); |
| 56 | } |
| 57 | |
| 58 | void arl_setup(arl_t *arl, const char *filepath) |
| 59 | { |
| 60 | char *base = strrchr(filepath, '/'); |
| 61 | |
| 62 | if (arl->fd == -1) |
| 63 | return; |
| 64 | |
| 65 | rm_watch(arl->fd, &arl->wd_dir); |
| 66 | rm_watch(arl->fd, &arl->wd_file); |
| 67 | |
| 68 | add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF); |
| 69 | |
| 70 | free(arl->filename); |
| 71 | arl->filename = estrdup(filepath); |
| 72 | |
| 73 | if (base != NULL) { |
| 74 | arl->filename[++base - filepath] = '\0'; |
| 75 | add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO); |
| 76 | strcpy(arl->filename, base); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | union { |
| 81 | char d[4096]; /* aligned buffer */ |
| 82 | struct inotify_event e; |
| 83 | } buf; |
| 84 | |
| 85 | bool arl_handle(arl_t *arl) |
| 86 | { |
| 87 | bool reload = false; |
| 88 | char *ptr; |
| 89 | const struct inotify_event *event; |
| 90 | |
| 91 | for (;;) { |
| 92 | ssize_t len = read(arl->fd, buf.d, sizeof(buf.d)); |
| 93 | |
| 94 | if (len == -1) { |
| 95 | if (errno == EINTR) |
| 96 | continue; |
| 97 | break; |
| 98 | } |
| 99 | for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) { |
| 100 | event = (const struct inotify_event*) ptr; |
| 101 | if (event->mask & IN_CLOSE_WRITE) { |
| 102 | reload = true; |
| 103 | } else if (event->mask & IN_DELETE_SELF) { |
| 104 | rm_watch(arl->fd, &arl->wd_file); |
| 105 | } else if (event->mask & (IN_CREATE | IN_MOVED_TO)) { |
| 106 | if (STREQ(event->name, arl->filename)) |
| 107 | reload = true; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | return reload; |
| 112 | } |
| 113 | |