Krolyxon
Commit a9d2b5b

Branches

Refs →

nsxivautoreload.c

139 lines · 3.0 KB · C

1/* Copyright 2017 Max Voit, Bert Muennich
2 *
3 * This file is a part of nsxiv.
4 *
5 * nsxiv 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 * nsxiv 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 nsxiv. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "nsxiv.h"
20
21#if HAVE_INOTIFY
22
23#include <errno.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <sys/inotify.h>
28
29static union {
30 char d[4096]; /* aligned buffer */
31 struct inotify_event e;
32} buf;
33
34void arl_init(arl_t *arl)
35{
36 arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
37 arl->wd_dir = arl->wd_file = -1;
38 if (arl->fd == -1)
39 error(0, 0, "Could not initialize inotify, no automatic image reloading");
40}
41
42CLEANUP void arl_cleanup(arl_t *arl)
43{
44 if (arl->fd != -1)
45 close(arl->fd);
46 free(arl->filename);
47}
48
49static void rm_watch(int fd, int *wd)
50{
51 if (*wd != -1) {
52 inotify_rm_watch(fd, *wd);
53 *wd = -1;
54 }
55}
56
57static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
58{
59 *wd = inotify_add_watch(fd, path, mask);
60 if (*wd == -1)
61 error(0, errno, "inotify: %s", path);
62}
63
64void arl_setup(arl_t *arl, const char *filepath)
65{
66 char *base = strrchr(filepath, '/');
67
68 if (arl->fd == -1)
69 return;
70
71 rm_watch(arl->fd, &arl->wd_dir);
72 rm_watch(arl->fd, &arl->wd_file);
73
74 add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
75
76 free(arl->filename);
77 arl->filename = estrdup(filepath);
78
79 if (base != NULL) {
80 arl->filename[++base - filepath] = '\0';
81 add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO);
82 strcpy(arl->filename, base); /* NOLINT: basename will always be shorter than fullpath */
83 }
84}
85
86bool arl_handle(arl_t *arl)
87{
88 bool reload = false;
89 char *ptr;
90 const struct inotify_event *e;
91
92 while (true) {
93 ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
94
95 if (len == -1) {
96 if (errno == EINTR)
97 continue;
98 break;
99 }
100 for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) {
101 e = (const struct inotify_event*) ptr;
102 if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) {
103 reload = true;
104 } else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) {
105 rm_watch(arl->fd, &arl->wd_file);
106 } else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) {
107 if (STREQ(e->name, arl->filename))
108 reload = true;
109 }
110 }
111 }
112 return reload;
113}
114
115#else
116
117void arl_init(arl_t *arl)
118{
119 arl->fd = -1;
120}
121
122void arl_cleanup(arl_t *arl)
123{
124 (void) arl;
125}
126
127void arl_setup(arl_t *arl, const char *filepath)
128{
129 (void) arl;
130 (void) filepath;
131}
132
133bool arl_handle(arl_t *arl)
134{
135 (void) arl;
136 return false;
137}
138
139#endif /* HAVE_INOTIFY */