Move usage of iconh into the branch that assigns it
[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         g_hash_table_destroy(w->targets_by_wd);
91
92         g_slice_free(ObtWatch, w);
93     }
94 }
95
96 static void init_inot(ObtWatch *w)
97 {
98 #ifdef HAVE_SYS_INOTIFY_H
99     if (w->ino_fd >= 0) return;
100
101     w->ino_fd = inotify_init();
102     if (w->ino_fd >= 0) {
103         GIOChannel *ch;
104
105         ch = g_io_channel_unix_new(w->ino_fd);
106         w->ino_watch = g_io_add_watch(ch, G_IO_IN | G_IO_HUP | G_IO_ERR,
107                                       read_inot, w);
108         g_io_channel_unref(ch);
109     }
110 #endif
111 }
112
113 static gboolean read_inot(GIOChannel *src, GIOCondition cond, gpointer data)
114 {
115 #ifdef HAVE_SYS_INOTIFY_H
116     ObtWatch *w = data;
117     ObtWatchTarget *t;
118     struct inotify_event s;
119     gint len;
120     guint ilen;
121     char *name;
122     
123     /* read the event */
124     for (ilen = 0; ilen < sizeof(s); ilen += len) {
125         len = read(w->ino_fd, ((char*)&s)+ilen, sizeof(s)-ilen);
126         if (len < 0 && errno != EINTR) return FALSE; /* error, don't repeat */
127         if (!len) return TRUE; /* nothing there */
128     }
129
130     name = g_new(char, s.len);
131
132     /* read the filename */
133     for (ilen = 0; ilen < s.len; ilen += len) {
134         len = read(w->ino_fd, name+ilen, s.len-ilen);
135         if (len < 0 && errno != EINTR) return FALSE; /* error, don't repeat */
136         if (!len) return TRUE; /* nothing there */
137     }
138
139     t = g_hash_table_lookup(w->targets, &s.wd);
140     if (t) t->func(w, name, t->data);
141
142     g_free(name);
143 #endif
144     return TRUE; /* repeat */
145 }
146
147 static gboolean add_inot(ObtWatch *w, ObtWatchTarget *t, const char *path,
148                          gboolean dir)
149 {
150 #ifndef HAVE_SYS_INOTIFY_H
151     return FALSE;
152 #else
153     gint mask;
154     if (w->ino_fd < 0) return FALSE;
155     if (dir) mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVE;
156     else g_assert_not_reached();
157     t->wd = inotify_add_watch(w->ino_fd, path, mask);
158     return TRUE;
159 #endif
160 }
161
162 static void rm_inot(ObtWatchTarget *t)
163 {
164 #ifdef HAVE_SYS_INOTIFY_H
165     if (t->w->ino_fd < 0) return;
166     if (t->wd < 0) return;
167     inotify_rm_watch(t->w->ino_fd, t->wd);
168 #endif
169 }
170
171 static ObtWatchTarget* target_new(ObtWatch *w, const gchar *path,
172                                   ObtWatchFunc func, gpointer data)
173 {
174     ObtWatchTarget *t;
175
176     t = g_slice_new0(ObtWatchTarget);
177     t->w = w;
178     t->wd = -1;
179     t->path = g_strdup(path);
180     t->func = func;
181     t->data = data;
182
183     if (!add_inot(w, t, path, TRUE)) {
184         g_assert_not_reached(); /* XXX do something */
185     }
186
187 #ifndef HAVE_SYS_INOTIFY_H
188 #error need inotify for now
189 #endif
190
191     return t;
192 }
193
194 static void target_free(ObtWatchTarget *t)
195 {
196     rm_inot(t);
197
198     g_free(t->path);
199     g_slice_free(ObtWatchTarget, t);
200 }
201
202 void obt_paths_watch_dir(ObtWatch *w, const gchar *path,
203                          ObtWatchFunc func, gpointer data)
204 {
205     ObtWatchTarget *t;
206
207     g_return_if_fail(w != NULL);
208     g_return_if_fail(path != NULL);
209     g_return_if_fail(data != NULL);
210
211     t = target_new(w, path, func, data);
212     g_hash_table_insert(w->targets, t->path, t);
213 #ifdef HAVE_SYS_INOTIFY_H
214     g_hash_table_insert(w->targets_by_wd, &t->wd, t);
215 #endif
216 }
217
218 void obt_paths_unwatch_dir(ObtWatch *w, const gchar *path)
219 {
220     ObtWatchTarget *t;
221     
222     g_return_if_fail(w != NULL);
223     g_return_if_fail(path != NULL);
224
225     t = g_hash_table_lookup(w->targets, path);
226
227     if (t) {
228 #ifdef HAVE_SYS_INOTIFY_H
229         g_hash_table_remove(w->targets_by_wd, &t->wd);
230 #endif
231         g_hash_table_remove(w->targets, path);
232     }
233 }