annotate src/audacious/hook.c @ 2865:d226b83fa329 trunk

BSD relicensing (pass 6).
author William Pitcock <nenolod@atheme.org>
date Sat, 23 Jun 2007 22:51:13 -0500
parents b7f77224ea03
children 4c758281fe8f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
1 /*
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
2 * audacious: Cross-platform multimedia player.
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
3 * hook.c: Hooking.
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
4 *
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
5 * Copyright (c) 2006-2007 William Pitcock
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
6 *
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
7 * Redistribution and use in source and binary forms, with or without
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
8 * modification, are permitted provided that the following conditions are
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
9 * met:
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
10 *
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
11 * 1. Redistributions of source code must retain the above copyright notice,
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
12 * this list of conditions and the following disclaimer.
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
13 *
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
14 * 2. Redistributions in binary form must reproduce the above copyright
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
15 * notice, this list of conditions and the following disclaimer in the
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
16 * documentation and/or other materials provided with the distribution.
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
17 *
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
18 * 3. The name of the author may not be used to endorse or promote products
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
19 * derived from this software without specific prior written permission.
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
20 *
2865
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
d226b83fa329 BSD relicensing (pass 6).
William Pitcock <nenolod@atheme.org>
parents: 2457
diff changeset
31 * POSSIBILITY OF SUCH DAMAGE.
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
32 */
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
33
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
34 #include <glib.h>
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
35 #include "hook.h"
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
36
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
37 static GSList *hook_list;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
38
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
39 static Hook *
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
40 hook_find(const gchar *name)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
41 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
42 GSList *list;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
43
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
44 for (list = hook_list; list != NULL; list = g_slist_next(list))
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
45 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
46 Hook *hook = (Hook *) list->data;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
47
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
48 if (!g_ascii_strcasecmp(hook->name, name))
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
49 return hook;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
50 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
51
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
52 return NULL;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
53 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
54
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
55 void
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
56 hook_register(const gchar *name)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
57 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
58 Hook *hook;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
59
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
60 g_return_if_fail(name != NULL);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
61
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
62 if ((hook = hook_find(name)) != NULL)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
63 return;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
64
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
65 hook = g_new0(Hook, 1);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
66 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
67 hook->items = NULL;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
68
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
69 hook_list = g_slist_append(hook_list, hook);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
70 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
71
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
72 gint
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
73 hook_associate(const gchar *name, HookFunction func, gpointer user_data)
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
74 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
75 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
76 HookItem *hookitem;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
77
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
78 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
79 g_return_val_if_fail(func != NULL, -1);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
80
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
81 hook = hook_find(name);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
82
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
83 if (hook == NULL)
2406
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
84 {
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
85 hook_register(name);
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
86 hook = hook_find(name);
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
87 }
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
88
6f4094cc3859 [svn] - allow for hooks to be automatically registered if they are needed
nenolod
parents: 2404
diff changeset
89 /* 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
90 g_return_val_if_fail(hook != NULL, -1);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
91
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
92 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
93 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
94 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
95
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
96 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
97 return 0;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
98 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
99
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
100 gint
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
101 hook_dissociate(const gchar *name, HookFunction func)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
102 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
103 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
104 GSList *iter;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
105
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
106 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
107 g_return_val_if_fail(func != NULL, -1);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
108
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
109 hook = hook_find(name);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
110
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
111 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
112 return -1;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
113
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 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
115 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
116 {
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
117 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
118 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
119 {
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
120 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
121 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
122 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
123 }
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
124 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
125 }
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
126 return -1;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
127 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
128
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
129 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
130 hook_call(const gchar *name, gpointer hook_data)
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
131 {
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
132 Hook *hook;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
133 GSList *iter;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
134
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
135 g_return_if_fail(name != NULL);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
136
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
137 hook = hook_find(name);
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
138
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
139 if (hook == NULL)
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
140 return;
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
141
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
142 for (iter = hook->items; iter != NULL; iter = g_slist_next(iter))
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
143 {
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
144 HookItem *hookitem = (HookItem*)iter->data;
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
145
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
146 g_return_if_fail(hookitem->func != NULL);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
147
2457
b7f77224ea03 [svn] - now it's possible to pass user_data along with the hook function in hook_associate
giacomo
parents: 2406
diff changeset
148 hookitem->func(hook_data, hookitem->user_data);
2404
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
149 }
60f1bc20c19c [svn] - hooking implementation.
nenolod
parents:
diff changeset
150 }