Krolyxon
Commit edb117e

Branches

Refs →

nsxivautoreload_inotify.c

154 lines · 3.7 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#include <time.h>
25
26#include "util.h"
27#include "autoreload.h"
28
29const struct timespec ten_ms = {0, 10000000};
30
31void 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
40void 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
99void 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
108void 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
130void 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}