Krolyxon
Commit 6695cd4

Branches

Refs →

nsxivautoreload_inotify.c

126 lines · 3.1 KB · C

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
25#include "util.h"
26#include "autoreload.h"
27
28CLEANUP void arl_cleanup(arl_t *arl)
29{
30 if (arl->fd != -1)
31 close(arl->fd);
32}
33
34static void arl_setup_dir(arl_t *arl, const char *filepath)
35{
36 char *dntmp, *dn;
37
38 if (arl->fd == -1)
39 return;
40
41 /* get dirname */
42 dntmp = (char*) strdup(filepath);
43 dn = (char*) dirname(dntmp);
44
45 /* this is not one-shot as other stuff may be created too
46 * note: we won't handle deletion of the directory itself,
47 * this is a design decision
48 */
49 arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE);
50 if (arl->wd == -1)
51 error(0, 0, "%s: Error watching directory", dn);
52 else
53 arl->watching_dir = true;
54
55 free(dntmp);
56}
57
58bool arl_handle(arl_t *arl, const char *filepath)
59{
60 bool reload = false;
61 char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event))));
62 char *ptr;
63 const struct inotify_event *event;
64
65 ssize_t len = read(arl->fd, buf, sizeof(buf));
66
67 if (len == -1)
68 return false;
69
70 for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) {
71 event = (const struct inotify_event*) ptr;
72
73 /* events from watching the file itself */
74 if (event->mask & IN_CLOSE_WRITE) {
75 reload = true;
76 }
77 if (event->mask & IN_DELETE_SELF)
78 arl_setup_dir(arl, filepath);
79
80 /* events from watching the file's directory */
81 if (event->mask & IN_CREATE) {
82 char *fntmp = strdup(filepath);
83 char *fn = basename(fntmp);
84
85 if (STREQ(event->name, fn)) {
86 /* this is the file we're looking for */
87
88 /* cleanup, this has not been one-shot */
89 if (arl->watching_dir) {
90 inotify_rm_watch(arl->fd, arl->wd);
91 arl->watching_dir = false;
92 }
93 reload = true;
94 }
95 free(fntmp);
96 }
97 }
98 return reload;
99}
100
101void arl_init(arl_t *arl)
102{
103 /* this needs to be done only once */
104 arl->fd = inotify_init();
105 arl->watching_dir = false;
106 if (arl->fd == -1)
107 error(0, 0, "Could not initialize inotify, no automatic image reloading");
108}
109
110void arl_setup(arl_t *arl, const char *filepath)
111{
112 if (arl->fd == -1)
113 return;
114
115 /* may have switched from a deleted to another image */
116 if (arl->watching_dir) {
117 inotify_rm_watch(arl->fd, arl->wd);
118 arl->watching_dir = false;
119 }
120
121 arl->wd = inotify_add_watch(arl->fd, filepath,
122 IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
123 if (arl->wd == -1)
124 error(0, 0, "%s: Error watching file", filepath);
125}
126