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);
92 g_source_attach(source, NULL);
97 void obt_watch_ref(ObtWatch *w)
102 void obt_watch_unref(ObtWatch *w)
105 g_hash_table_destroy(w->targets_by_path);
106 g_source_destroy(w->source);
107 g_slice_free(ObtWatch, w);
111 static void target_free(ObtWatchTarget *t)
114 watch_sys_remove_target(t->w->source, t->key);
115 g_free(t->base_path);
116 g_slice_free(ObtWatchTarget, t);
119 gboolean obt_watch_add(ObtWatch *w, const gchar *path,
120 gboolean watch_hidden,
121 ObtWatchFunc func, gpointer data)
125 g_return_val_if_fail(w != NULL, FALSE);
126 g_return_val_if_fail(path != NULL, FALSE);
127 g_return_val_if_fail(func != NULL, FALSE);
128 g_return_val_if_fail(path[0] == G_DIR_SEPARATOR, FALSE);
130 t = g_slice_new0(ObtWatchTarget);
132 t->base_path = g_strdup(path);
135 g_hash_table_insert(w->targets_by_path, t->base_path, t);
137 t->key = watch_sys_add_target(w->source, path, watch_hidden, t);
139 g_hash_table_remove(w->targets_by_path, t->base_path);
146 void obt_watch_remove(ObtWatch *w, const gchar *path)
148 g_return_if_fail(w != NULL);
149 g_return_if_fail(path != NULL);
150 g_return_if_fail(path[0] == G_DIR_SEPARATOR);
152 /* this also calls target_free */
153 g_hash_table_remove(w->targets_by_path, path);
156 static void target_notify(const gchar *sub_path, const gchar *full_path,
157 gpointer target, ObtWatchNotifyType type)
159 ObtWatchTarget *t = target;
160 if (type == OBT_WATCH_SELF_REMOVED) {
161 /* this also calls target_free */
162 g_hash_table_remove(t->w->targets_by_path, t->base_path);
164 t->func(t->w, t->base_path, sub_path, full_path, type, t->data);