annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
1 /* Audacious
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
2 * Copyright (c) 2006-2007 William Pitcock
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
3 *
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
6 * the Free Software Foundation; under version 2 of the License.
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
7 *
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
11 * GNU General Public License for more details.
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
12 *
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
13 * You should have received a copy of the GNU General Public License
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
14 * along with this program; if not, write to the Free Software
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
16 */
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
17
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
18 #include <glib.h>
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
19 #include "hook.h"
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
20
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
21 static GSList *hook_list;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
22
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
23 static Hook *
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
24 hook_find(const gchar *name)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
25 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
26 GSList *list;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
27
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
28 for (list = hook_list; list != NULL; list = g_slist_next(list))
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
29 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
30 Hook *hook = (Hook *) list->data;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
31
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
32 if (!g_ascii_strcasecmp(hook->name, name))
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
33 return hook;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
34 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
35
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
36 return NULL;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
37 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
38
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
39 void
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
40 hook_register(const gchar *name)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
41 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
42 Hook *hook;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
43
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
44 g_return_if_fail(name != NULL);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
45
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
46 if ((hook = hook_find(name)) != NULL)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
47 return;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
48
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
49 hook = g_new0(Hook, 1);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
50 hook->name = g_strdup(name);
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
51 hook->items = NULL;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
52
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
53 hook_list = g_slist_append(hook_list, hook);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
54 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
55
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
56 gint
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
57 hook_associate(const gchar *name, HookFunction func, gpointer user_data)
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
58 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
59 Hook *hook;
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
60 HookItem *hookitem;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
61
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
62 g_return_val_if_fail(name != NULL, -1);
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
63 g_return_val_if_fail(func != NULL, -1);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
64
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
65 hook = hook_find(name);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
66
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
67 if (hook == NULL)
2406
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
68 {
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
69 hook_register(name);
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
70 hook = hook_find(name);
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
71 }
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
72
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
73 /* this *cant* happen */
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
74 g_return_val_if_fail(hook != NULL, -1);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
75
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
76 hookitem = g_new0(HookItem, 1);
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
77 hookitem->func = func;
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
78 hookitem->user_data = user_data;
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
79
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
80 hook->items = g_slist_append(hook->items, hookitem);
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
81 return 0;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
82 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
83
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
84 gint
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
85 hook_dissociate(const gchar *name, HookFunction func)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
86 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
87 Hook *hook;
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
88 GSList *iter;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
89
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
90 g_return_val_if_fail(name != NULL, -1);
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
91 g_return_val_if_fail(func != NULL, -1);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
92
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
93 hook = hook_find(name);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
94
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
95 if (hook == NULL)
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
96 return -1;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
97
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
98 iter = hook->items;
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
99 while (iter != NULL)
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
100 {
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
101 HookItem *hookitem = (HookItem*)iter->data;
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
102 if (hookitem->func == func)
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
103 {
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
104 hook->items = g_slist_delete_link(hook->items, iter);
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
105 g_free( hookitem );
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
106 return 0;
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
107 }
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
108 iter = g_slist_next(iter);
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
109 }
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
110 return -1;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
111 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
112
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
113 void
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
114 hook_call(const gchar *name, gpointer hook_data)
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
115 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
116 Hook *hook;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
117 GSList *iter;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
118
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
119 g_return_if_fail(name != NULL);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
120
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
121 hook = hook_find(name);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
122
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
123 if (hook == NULL)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
124 return;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
125
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
126 for (iter = hook->items; iter != NULL; iter = g_slist_next(iter))
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
127 {
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
128 HookItem *hookitem = (HookItem*)iter->data;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
129
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
130 g_return_if_fail(hookitem->func != NULL);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
131
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
132 hookitem->func(hook_data, hookitem->user_data);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
133 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
134 }