comparison src/audacious/hook.c @ 2404:60f1bc20c19c trunk

[svn] - hooking implementation. example: hook_register("playback begin"); hook_call("playback begin", <PlaylistEntry>);
author nenolod
date Thu, 25 Jan 2007 20:23:16 -0800
parents
children 6f4094cc3859
comparison
equal deleted inserted replaced
2403:55f92225cd20 2404:60f1bc20c19c
1 /* Audacious
2 * Copyright (c) 2006-2007 William Pitcock
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; under version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 */
17
18 #include <glib.h>
19 #include "hook.h"
20
21 static GSList *hook_list;
22
23 static Hook *
24 hook_find(const gchar *name)
25 {
26 GSList *list;
27
28 for (list = hook_list; list != NULL; list = g_slist_next(list))
29 {
30 Hook *hook = (Hook *) list->data;
31
32 if (!g_ascii_strcasecmp(hook->name, name))
33 return hook;
34 }
35
36 return NULL;
37 }
38
39 void
40 hook_register(const gchar *name)
41 {
42 Hook *hook;
43
44 g_return_if_fail(name != NULL);
45
46 if ((hook = hook_find(name)) != NULL)
47 return;
48
49 hook = g_new0(Hook, 1);
50 hook->name = g_strdup(name);
51
52 hook_list = g_slist_append(hook_list, hook);
53 }
54
55 void
56 hook_associate(const gchar *name, HookFunction func)
57 {
58 Hook *hook;
59
60 g_return_if_fail(name != NULL);
61 g_return_if_fail(func != NULL);
62
63 hook = hook_find(name);
64
65 if (hook == NULL)
66 return;
67
68 hook->funcs = g_slist_append(hook->funcs, func);
69 }
70
71 void
72 hook_dissociate(const gchar *name, HookFunction func)
73 {
74 Hook *hook;
75
76 g_return_if_fail(name != NULL);
77 g_return_if_fail(func != NULL);
78
79 hook = hook_find(name);
80
81 if (hook == NULL)
82 return;
83
84 hook->funcs = g_slist_remove(hook->funcs, func);
85 }
86
87 void
88 hook_call(const gchar *name, gpointer user_data)
89 {
90 Hook *hook;
91 GSList *iter;
92
93 g_return_if_fail(name != NULL);
94
95 hook = hook_find(name);
96
97 if (hook == NULL)
98 return;
99
100 for (iter = hook->funcs; iter != NULL; iter = g_slist_next(iter))
101 {
102 HookFunction func = (HookFunction) iter->data;
103
104 g_return_if_fail(func != NULL);
105
106 func(user_data);
107 }
108 }