1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 obt/watch.c for the Openbox window manager
4 Copyright (c) 2010 Dana Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
19 #include "obt/watch.h"
21 #ifdef HAVE_SYS_INOTIFY_H
22 # include <sys/inotify.h>
35 #ifdef HAVE_SYS_INOTIFY_H
36 GHashTable *targets_by_wd;
40 typedef struct _ObtWatchTarget {
43 #ifdef HAVE_SYS_INOTIFY_H
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,
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);
61 ObtWatch* obt_watch_new()
65 w = g_slice_new(ObtWatch);
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);
78 void obt_watch_ref(ObtWatch *w)
83 void obt_watch_unref(ObtWatch *w)
86 if (w->ino_fd >= 0 && w->ino_watch)
87 g_source_remove(w->ino_watch);
89 g_hash_table_destroy(w->targets);
90 #ifdef HAVE_SYS_INOTIFY_H
91 g_hash_table_destroy(w->targets_by_wd);
94 g_slice_free(ObtWatch, w);
98 static void init_inot(ObtWatch *w)
100 #ifdef HAVE_SYS_INOTIFY_H
101 if (w->ino_fd >= 0) return;
103 w->ino_fd = inotify_init();
104 if (w->ino_fd >= 0) {
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,
110 g_io_channel_unref(ch);
115 static gboolean read_inot(GIOChannel *src, GIOCondition cond, gpointer data)
117 #ifdef HAVE_SYS_INOTIFY_H
120 struct inotify_event s;
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 */
132 name = g_new(char, s.len);
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 */
141 t = g_hash_table_lookup(w->targets, &s.wd);
142 if (t) t->func(w, name, t->data);
146 return TRUE; /* repeat */
149 static gboolean add_inot(ObtWatch *w, ObtWatchTarget *t, const char *path,
152 #ifndef HAVE_SYS_INOTIFY_H
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);
164 static void rm_inot(ObtWatchTarget *t)
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);
173 static ObtWatchTarget* target_new(ObtWatch *w, const gchar *path,
174 ObtWatchFunc func, gpointer data)
178 t = g_slice_new0(ObtWatchTarget);
180 #ifdef HAVE_SYS_INOTIFY_H
183 t->path = g_strdup(path);
187 if (!add_inot(w, t, path, TRUE)) {
188 g_assert_not_reached(); /* XXX do something */
191 #ifndef HAVE_SYS_INOTIFY_H
192 #error need inotify for now
198 static void target_free(ObtWatchTarget *t)
203 g_slice_free(ObtWatchTarget, t);
206 void obt_paths_watch_dir(ObtWatch *w, const gchar *path,
207 ObtWatchFunc func, gpointer data)
211 g_return_if_fail(w != NULL);
212 g_return_if_fail(path != NULL);
213 g_return_if_fail(data != NULL);
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);
222 void obt_paths_unwatch_dir(ObtWatch *w, const gchar *path)
226 g_return_if_fail(w != NULL);
227 g_return_if_fail(path != NULL);
229 t = g_hash_table_lookup(w->targets, path);
232 #ifdef HAVE_SYS_INOTIFY_H
233 g_hash_table_remove(w->targets_by_wd, &t->wd);
235 g_hash_table_remove(w->targets, path);