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 *path, gpointer target,
32 ObtWatchNotifyType type);
35 /* Interface for system-specific stuff (e.g. inotify). the functions are
36 defined in in watch_<system>.c
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.
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.
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.
53 void watch_sys_remove_target(GSource *source, gint key);
56 /* General system which uses the watch_sys_* stuff
61 GHashTable *targets_by_path;
65 struct _ObtWatchTarget {
73 static void target_free(ObtWatchTarget *t);
74 static void target_notify(const gchar *const path, gpointer target,
75 ObtWatchNotifyType type);
77 ObtWatch* obt_watch_new()
83 source = watch_sys_create_source(target_notify);
85 w = g_slice_new(ObtWatch);
87 w->targets_by_path = g_hash_table_new_full(
88 g_str_hash, g_str_equal, NULL, (GDestroyNotify)target_free);
94 void obt_watch_ref(ObtWatch *w)
99 void obt_watch_unref(ObtWatch *w)
102 g_hash_table_destroy(w->targets_by_path);
103 g_source_destroy(w->source);
104 g_slice_free(ObtWatch, w);
108 static void target_free(ObtWatchTarget *t)
111 watch_sys_remove_target(t->w->source, t->key);
112 g_free(t->base_path);
113 g_slice_free(ObtWatchTarget, t);
116 gboolean obt_watch_add(ObtWatch *w, const gchar *path,
117 gboolean watch_hidden,
118 ObtWatchFunc func, gpointer data)
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);
127 t = g_slice_new0(ObtWatchTarget);
129 t->base_path = g_strdup(path);
132 g_hash_table_insert(w->targets_by_path, t->base_path, t);
134 t->key = watch_sys_add_target(w->source, path, watch_hidden, t);
136 g_hash_table_remove(w->targets_by_path, t->base_path);
143 void obt_watch_remove(ObtWatch *w, const gchar *path)
145 g_return_if_fail(w != NULL);
146 g_return_if_fail(path != NULL);
147 g_return_if_fail(path[0] == G_DIR_SEPARATOR);
149 /* this also calls target_free */
150 g_hash_table_remove(w->targets_by_path, path);
153 static void target_notify(const gchar *const path, gpointer target,
154 ObtWatchNotifyType type)
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);
161 t->func(t->w, path, type, t->data);