]> icculus.org git repositories - dana/openbox.git/blob - obt/watch.c
Add/fix inotify support for watching filesystem changes.
[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_UNISTD_H
22 #  include <unistd.h>
23 #endif
24
25 #include <glib.h>
26
27 typedef struct _ObtWatchTarget ObtWatchTarget;
28
29 /*! Callback function for the system-specific GSource to alert us to changes.
30 */
31 typedef void (*ObtWatchNotifyFunc)(const gchar *path, gpointer target,
32                                    ObtWatchNotifyType type);
33
34
35 /* Interface for system-specific stuff (e.g. inotify). the functions are
36    defined in in watch_<system>.c
37 */
38
39 /*! Initializes the watch subsystem, and returns a GSource for it.
40   @param notify The GSource will call @notify when a watched file is changed.
41   @return Returns a GSource* on success, and a NULL if an error occurred.
42 */
43 GSource* watch_sys_create_source(ObtWatchNotifyFunc notify);
44 /*! Add a target to the watch subsystem.
45   @return Returns an integer key that is used to uniquely identify the target
46     within this subsystem.  A negative value indicates an error.
47 */
48 gint watch_sys_add_target(GSource *source, const char *path,
49                           gboolean watch_hidden, gpointer target);
50 /*! Remove a target from the watch system, by its key.
51   Use the key returned from watch_sys_add_target() to remove the target.
52 */
53 void watch_sys_remove_target(GSource *source, gint key);
54
55
56 /* General system which uses the watch_sys_* stuff
57 */
58
59 struct _ObtWatch {
60     guint ref;
61     GHashTable *targets_by_path;
62     GSource *source;
63 };
64
65 struct _ObtWatchTarget {
66     ObtWatch *w;
67     gchar *base_path;
68     ObtWatchFunc func;
69     gpointer data;
70     gint key;
71 };
72
73 static void target_free(ObtWatchTarget *t);
74 static void target_notify(const gchar *const path, gpointer target,
75                           ObtWatchNotifyType type);
76
77 ObtWatch* obt_watch_new()
78 {
79     ObtWatch *w;
80     GSource *source;
81
82     w = NULL;
83     source = watch_sys_create_source(target_notify);
84     if (source) {
85         w = g_slice_new(ObtWatch);
86         w->ref = 1;
87         w->targets_by_path = g_hash_table_new_full(
88             g_str_hash, g_str_equal, NULL, (GDestroyNotify)target_free);
89         w->source = source;
90     }
91     return w;
92 }
93
94 void obt_watch_ref(ObtWatch *w)
95 {
96     ++w->ref;
97 }
98
99 void obt_watch_unref(ObtWatch *w)
100 {
101     if (--w->ref < 1) {
102         g_hash_table_destroy(w->targets_by_path);
103         g_source_destroy(w->source);
104         g_slice_free(ObtWatch, w);
105     }
106 }
107
108 static void target_free(ObtWatchTarget *t)
109 {
110     if (t->key >= 0)
111         watch_sys_remove_target(t->w->source, t->key);
112     g_free(t->base_path);
113     g_slice_free(ObtWatchTarget, t);
114 }
115
116 gboolean obt_watch_add(ObtWatch *w, const gchar *path,
117                        gboolean watch_hidden,
118                        ObtWatchFunc func, gpointer data)
119 {
120     ObtWatchTarget *t;
121
122     g_return_val_if_fail(w != NULL, FALSE);
123     g_return_val_if_fail(path != NULL, FALSE);
124     g_return_val_if_fail(func != NULL, FALSE);
125     g_return_val_if_fail(path[0] == G_DIR_SEPARATOR, FALSE);
126
127     t = g_slice_new0(ObtWatchTarget);
128     t->w = w;
129     t->base_path = g_strdup(path);
130     t->func = func;
131     t->data = data;
132     g_hash_table_insert(w->targets_by_path, t->base_path, t);
133
134     t->key = watch_sys_add_target(w->source, path, watch_hidden, t);
135     if (t->key < 0) {
136         g_hash_table_remove(w->targets_by_path, t->base_path);
137         return FALSE;
138     }
139
140     return TRUE;
141 }
142
143 void obt_watch_remove(ObtWatch *w, const gchar *path)
144 {
145     g_return_if_fail(w != NULL);
146     g_return_if_fail(path != NULL);
147     g_return_if_fail(path[0] == G_DIR_SEPARATOR);
148
149     /* this also calls target_free */
150     g_hash_table_remove(w->targets_by_path, path);
151 }
152
153 static void target_notify(const gchar *const path, gpointer target,
154                           ObtWatchNotifyType type)
155 {
156     ObtWatchTarget *t = target;
157     if (type == OBT_WATCH_SELF_REMOVED) {
158         /* this also calls target_free */
159         g_hash_table_remove(t->w->targets_by_path, t->base_path);
160     }
161     t->func(t->w, path, type, t->data);
162 }