]> icculus.org git repositories - dana/openbox.git/blob - obt/watch.c
some missing ifdef HAVE_SYS_INOTIFY
[dana/openbox.git] / obt / watch.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    obt/watch.c for the Openbox window manager
4    Copyright (c) 2010        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "obt/watch.h"
20
21 #ifdef HAVE_SYS_INOTIFY_H
22 #  include <sys/inotify.h>
23 #endif
24 #ifdef HAVE_UNISTD_H
25 #  include <unistd.h>
26 #endif
27 #include <errno.h>
28
29 struct _ObtWatch {
30     guint ref;
31     gint ino_fd;
32     guint ino_watch;
33     GHashTable *targets;
34
35 #ifdef HAVE_SYS_INOTIFY_H
36     GHashTable *targets_by_wd;
37 #endif
38 };
39
40 typedef struct _ObtWatchTarget {
41     ObtWatch *w;
42
43 #ifdef HAVE_SYS_INOTIFY_H
44     gint wd;
45 #endif
46
47     gchar *path;
48     ObtWatchFunc func;
49     gpointer data;
50 } ObtWatchTarget;
51
52 static void init_inot(ObtWatch *w);
53 static gboolean read_inot(GIOChannel *s, GIOCondition cond, gpointer data);
54 static gboolean add_inot(ObtWatch *w, ObtWatchTarget *t, const char *path,
55                          gboolean dir);
56 static void rm_inot(ObtWatchTarget *t);
57 static ObtWatchTarget* target_new(ObtWatch *w, const gchar *path,
58                                   ObtWatchFunc func, gpointer data);
59 static void target_free(ObtWatchTarget *t);
60
61 ObtWatch* obt_watch_new()
62 {
63     ObtWatch *w;
64
65     w = g_slice_new(ObtWatch);
66     w->ref = 1;
67     w->ino_fd = -1;
68     w->targets = g_hash_table_new_full(g_str_hash, g_str_equal,
69                                        NULL, (GDestroyNotify)target_free);
70 #ifdef HAVE_SYS_INOTIFY_H
71     w->targets_by_wd = g_hash_table_new(g_int_hash, g_int_equal);
72 #endif
73
74     init_inot(w);
75
76     return w;
77 }
78 void obt_watch_ref(ObtWatch *w)
79 {
80     ++w->ref;
81 }
82
83 void obt_watch_unref(ObtWatch *w)
84 {
85     if (--w->ref < 1) {
86         if (w->ino_fd >= 0 && w->ino_watch)
87             g_source_remove(w->ino_watch);
88
89         g_hash_table_destroy(w->targets);
90 #ifdef HAVE_SYS_INOTIFY_H
91         g_hash_table_destroy(w->targets_by_wd);
92 #endif
93
94         g_slice_free(ObtWatch, w);
95     }
96 }
97
98 static void init_inot(ObtWatch *w)
99 {
100 #ifdef HAVE_SYS_INOTIFY_H
101     if (w->ino_fd >= 0) return;
102
103     w->ino_fd = inotify_init();
104     if (w->ino_fd >= 0) {
105         GIOChannel *ch;
106
107         ch = g_io_channel_unix_new(w->ino_fd);
108         w->ino_watch = g_io_add_watch(ch, G_IO_IN | G_IO_HUP | G_IO_ERR,
109                                       read_inot, w);
110         g_io_channel_unref(ch);
111     }
112 #endif
113 }
114
115 static gboolean read_inot(GIOChannel *src, GIOCondition cond, gpointer data)
116 {
117 #ifdef HAVE_SYS_INOTIFY_H
118     ObtWatch *w = data;
119     ObtWatchTarget *t;
120     struct inotify_event s;
121     gint len;
122     guint ilen;
123     char *name;
124     
125     /* read the event */
126     for (ilen = 0; ilen < sizeof(s); ilen += len) {
127         len = read(w->ino_fd, ((char*)&s)+ilen, sizeof(s)-ilen);
128         if (len < 0 && errno != EINTR) return FALSE; /* error, don't repeat */
129         if (!len) return TRUE; /* nothing there */
130     }
131
132     name = g_new(char, s.len);
133
134     /* read the filename */
135     for (ilen = 0; ilen < s.len; ilen += len) {
136         len = read(w->ino_fd, name+ilen, s.len-ilen);
137         if (len < 0 && errno != EINTR) return FALSE; /* error, don't repeat */
138         if (!len) return TRUE; /* nothing there */
139     }
140
141     t = g_hash_table_lookup(w->targets, &s.wd);
142     if (t) t->func(w, name, t->data);
143
144     g_free(name);
145 #endif
146     return TRUE; /* repeat */
147 }
148
149 static gboolean add_inot(ObtWatch *w, ObtWatchTarget *t, const char *path,
150                          gboolean dir)
151 {
152 #ifndef HAVE_SYS_INOTIFY_H
153     return FALSE;
154 #else
155     gint mask;
156     if (w->ino_fd < 0) return FALSE;
157     if (dir) mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVE;
158     else g_assert_not_reached();
159     t->wd = inotify_add_watch(w->ino_fd, path, mask);
160     return TRUE;
161 #endif
162 }
163
164 static void rm_inot(ObtWatchTarget *t)
165 {
166 #ifdef HAVE_SYS_INOTIFY_H
167     if (t->w->ino_fd < 0) return;
168     if (t->wd < 0) return;
169     inotify_rm_watch(t->w->ino_fd, t->wd);
170 #endif
171 }
172
173 static ObtWatchTarget* target_new(ObtWatch *w, const gchar *path,
174                                   ObtWatchFunc func, gpointer data)
175 {
176     ObtWatchTarget *t;
177
178     t = g_slice_new0(ObtWatchTarget);
179     t->w = w;
180 #ifdef HAVE_SYS_INOTIFY_H
181     t->wd = -1;
182 #endif
183     t->path = g_strdup(path);
184     t->func = func;
185     t->data = data;
186
187     if (!add_inot(w, t, path, TRUE)) {
188         g_assert_not_reached(); /* XXX do something */
189     }
190
191 #ifndef HAVE_SYS_INOTIFY_H
192 #error need inotify for now
193 #endif
194
195     return t;
196 }
197
198 static void target_free(ObtWatchTarget *t)
199 {
200     rm_inot(t);
201
202     g_free(t->path);
203     g_slice_free(ObtWatchTarget, t);
204 }
205
206 void obt_paths_watch_dir(ObtWatch *w, const gchar *path,
207                          ObtWatchFunc func, gpointer data)
208 {
209     ObtWatchTarget *t;
210
211     g_return_if_fail(w != NULL);
212     g_return_if_fail(path != NULL);
213     g_return_if_fail(data != NULL);
214
215     t = target_new(w, path, func, data);
216     g_hash_table_insert(w->targets, t->path, t);
217 #ifdef HAVE_SYS_INOTIFY_H
218     g_hash_table_insert(w->targets_by_wd, &t->wd, t);
219 #endif
220 }
221
222 void obt_paths_unwatch_dir(ObtWatch *w, const gchar *path)
223 {
224     ObtWatchTarget *t;
225     
226     g_return_if_fail(w != NULL);
227     g_return_if_fail(path != NULL);
228
229     t = g_hash_table_lookup(w->targets, path);
230
231     if (t) {
232 #ifdef HAVE_SYS_INOTIFY_H
233         g_hash_table_remove(w->targets_by_wd, &t->wd);
234 #endif
235         g_hash_table_remove(w->targets, path);
236     }
237 }