Mercurial > audlegacy
comparison src/audlegacy/hook.c @ 4811:7bf7f83a217e
rename src/audacious src/audlegacy so that both audlegacy and audacious can coexist.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Wed, 26 Nov 2008 00:44:56 +0900 |
parents | src/audacious/hook.c@f1c756f39e6c |
children | b2ee645f3e59 |
comparison
equal
deleted
inserted
replaced
4810:c10e53092037 | 4811:7bf7f83a217e |
---|---|
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 3 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, see <http://www.gnu.org/licenses>. | |
15 * | |
16 * The Audacious team does not consider modular code linking to | |
17 * Audacious or using our public API to be a derived work. | |
18 */ | |
19 | |
20 #include <glib.h> | |
21 #include "hook.h" | |
22 | |
23 static GSList *hook_list; | |
24 | |
25 static Hook * | |
26 hook_find(const gchar *name) | |
27 { | |
28 GSList *list; | |
29 | |
30 for (list = hook_list; list != NULL; list = g_slist_next(list)) | |
31 { | |
32 Hook *hook = (Hook *) list->data; | |
33 | |
34 if (!g_ascii_strcasecmp(hook->name, name)) | |
35 return hook; | |
36 } | |
37 | |
38 return NULL; | |
39 } | |
40 | |
41 void | |
42 hook_register(const gchar *name) | |
43 { | |
44 Hook *hook; | |
45 | |
46 g_return_if_fail(name != NULL); | |
47 | |
48 if ((hook = hook_find(name)) != NULL) | |
49 return; | |
50 | |
51 hook = g_new0(Hook, 1); | |
52 hook->name = g_strdup(name); | |
53 hook->items = NULL; | |
54 | |
55 hook_list = g_slist_append(hook_list, hook); | |
56 } | |
57 | |
58 gint | |
59 hook_associate(const gchar *name, HookFunction func, gpointer user_data) | |
60 { | |
61 Hook *hook; | |
62 HookItem *hookitem; | |
63 | |
64 g_return_val_if_fail(name != NULL, -1); | |
65 g_return_val_if_fail(func != NULL, -1); | |
66 | |
67 hook = hook_find(name); | |
68 | |
69 if (hook == NULL) | |
70 { | |
71 hook_register(name); | |
72 hook = hook_find(name); | |
73 } | |
74 | |
75 /* this *cant* happen */ | |
76 g_return_val_if_fail(hook != NULL, -1); | |
77 | |
78 hookitem = g_new0(HookItem, 1); | |
79 hookitem->func = func; | |
80 hookitem->user_data = user_data; | |
81 | |
82 hook->items = g_slist_append(hook->items, hookitem); | |
83 return 0; | |
84 } | |
85 | |
86 gint | |
87 hook_dissociate(const gchar *name, HookFunction func) | |
88 { | |
89 Hook *hook; | |
90 GSList *iter; | |
91 | |
92 g_return_val_if_fail(name != NULL, -1); | |
93 g_return_val_if_fail(func != NULL, -1); | |
94 | |
95 hook = hook_find(name); | |
96 | |
97 if (hook == NULL) | |
98 return -1; | |
99 | |
100 iter = hook->items; | |
101 while (iter != NULL) | |
102 { | |
103 HookItem *hookitem = (HookItem*)iter->data; | |
104 if (hookitem->func == func) | |
105 { | |
106 hook->items = g_slist_delete_link(hook->items, iter); | |
107 g_free( hookitem ); | |
108 return 0; | |
109 } | |
110 iter = g_slist_next(iter); | |
111 } | |
112 return -1; | |
113 } | |
114 | |
115 void | |
116 hook_call(const gchar *name, gpointer hook_data) | |
117 { | |
118 Hook *hook; | |
119 GSList *iter; | |
120 | |
121 g_return_if_fail(name != NULL); | |
122 | |
123 hook = hook_find(name); | |
124 | |
125 if (hook == NULL) | |
126 return; | |
127 | |
128 for (iter = hook->items; iter != NULL; iter = g_slist_next(iter)) | |
129 { | |
130 HookItem *hookitem = (HookItem*)iter->data; | |
131 | |
132 g_return_if_fail(hookitem->func != NULL); | |
133 | |
134 hookitem->func(hook_data, hookitem->user_data); | |
135 } | |
136 } |