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"
27 typedef struct _ObtWatchTarget ObtWatchTarget;
29 /*! Callback function for the system-specific GSource to alert us to changes.
31 typedef void (*ObtWatchNotifyFunc)(const gchar *sub_path,
32 const gchar *full_path, gpointer target,
33 ObtWatchNotifyType type);
36 /* Interface for system-specific stuff (e.g. inotify). the functions are
37 defined in in watch_<system>.c
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.
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.
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.
54 void watch_sys_remove_target(GSource *source, gint key);
57 /* General system which uses the watch_sys_* stuff
62 GHashTable *targets_by_path;
66 struct _ObtWatchTarget {
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);
78 ObtWatch* obt_watch_new()
84 source = watch_sys_create_source(target_notify);
86 w = g_slice_new(ObtWatch);
88 w->targets_by_path = g_hash_table_new_full(
89 g_str_hash, g_str_equal, NULL, (GDestroyNotify)target_free);
95 void obt_watch_ref(ObtWatch *w)
100 void obt_watch_unref(ObtWatch *w)
103 g_hash_table_destroy(w->targets_by_path);
104 g_source_destroy(w->source);
105 g_slice_free(ObtWatch, w);
109 static void target_free(ObtWatchTarget *t)
112 watch_sys_remove_target(t->w->source, t->key);
113 g_free(t->base_path);
114 g_slice_free(ObtWatchTarget, t);
117 gboolean obt_watch_add(ObtWatch *w, const gchar *path,
118 gboolean watch_hidden,
119 ObtWatchFunc func, gpointer data)
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);
128 t = g_slice_new0(ObtWatchTarget);
130 t->base_path = g_strdup(path);
133 g_hash_table_insert(w->targets_by_path, t->base_path, t);
135 t->key = watch_sys_add_target(w->source, path, watch_hidden, t);
137 g_hash_table_remove(w->targets_by_path, t->base_path);
144 void obt_watch_remove(ObtWatch *w, const gchar *path)
146 g_return_if_fail(w != NULL);
147 g_return_if_fail(path != NULL);
148 g_return_if_fail(path[0] == G_DIR_SEPARATOR);
150 /* this also calls target_free */
151 g_hash_table_remove(w->targets_by_path, path);
154 static void target_notify(const gchar *sub_path, const gchar *full_path,
155 gpointer target, ObtWatchNotifyType type)
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);
162 t->func(t->w, t->base_path, sub_path, full_path, type, t->data);