diff src/audacious/hook.c @ 2457:b7f77224ea03 trunk

[svn] - now it's possible to pass user_data along with the hook function in hook_associate
author giacomo
date Wed, 31 Jan 2007 18:21:35 -0800
parents 6f4094cc3859
children d226b83fa329
line wrap: on
line diff
--- a/src/audacious/hook.c	Wed Jan 31 16:47:18 2007 -0800
+++ b/src/audacious/hook.c	Wed Jan 31 18:21:35 2007 -0800
@@ -48,17 +48,19 @@
 
     hook = g_new0(Hook, 1);
     hook->name = g_strdup(name);
+    hook->items = NULL;
 
     hook_list = g_slist_append(hook_list, hook);
 }
 
-void
-hook_associate(const gchar *name, HookFunction func)
+gint
+hook_associate(const gchar *name, HookFunction func, gpointer user_data)
 {
     Hook *hook;
+    HookItem *hookitem;
 
-    g_return_if_fail(name != NULL);
-    g_return_if_fail(func != NULL);
+    g_return_val_if_fail(name != NULL, -1);
+    g_return_val_if_fail(func != NULL, -1);
 
     hook = hook_find(name);
 
@@ -69,29 +71,47 @@
     }
 
     /* this *cant* happen */
-    g_return_if_fail(hook != NULL);
+    g_return_val_if_fail(hook != NULL, -1);
 
-    hook->funcs = g_slist_append(hook->funcs, func);
+    hookitem = g_new0(HookItem, 1);
+    hookitem->func = func;
+    hookitem->user_data = user_data;
+
+    hook->items = g_slist_append(hook->items, hookitem);
+    return 0;
 }
 
-void
+gint
 hook_dissociate(const gchar *name, HookFunction func)
 {
     Hook *hook;
+    GSList *iter;
 
-    g_return_if_fail(name != NULL);
-    g_return_if_fail(func != NULL);
+    g_return_val_if_fail(name != NULL, -1);
+    g_return_val_if_fail(func != NULL, -1);
 
     hook = hook_find(name);
 
     if (hook == NULL)
-        return;
+        return -1;
 
-    hook->funcs = g_slist_remove(hook->funcs, func);
+    iter = hook->items;
+    while (iter != NULL)
+    {
+        HookItem *hookitem = (HookItem*)iter->data;
+        if (hookitem->func == func)
+        {
+            hook->items = g_slist_delete_link(hook->items, iter);
+            g_free( hookitem );
+            return 0;
+        }
+        iter = g_slist_next(iter);
+    }
+    return -1;
 }
 
 void
-hook_call(const gchar *name, gpointer user_data)
+hook_call(const gchar *name, gpointer hook_data)
 {
     Hook *hook;
     GSList *iter;
@@ -103,12 +123,12 @@
     if (hook == NULL)
         return;
 
-    for (iter = hook->funcs; iter != NULL; iter = g_slist_next(iter))
+    for (iter = hook->items; iter != NULL; iter = g_slist_next(iter))
     {
-        HookFunction func = (HookFunction) iter->data;
+        HookItem *hookitem = (HookItem*)iter->data;
 
-        g_return_if_fail(func != NULL);
+        g_return_if_fail(hookitem->func != NULL);
 
-        func(user_data);
+        hookitem->func(hook_data, hookitem->user_data);
     }
 }