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