2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
34 struct callback_list {
38 struct callback_list *
39 callback_list_new(void)
41 struct callback_list *ret=g_new0(struct callback_list, 1);
47 callback_new_attr(void (*func)(void), enum attr_type type, int pcount, void **p)
52 ret=g_malloc0(sizeof(struct callback)+pcount*sizeof(void *));
56 for (i = 0 ; i < pcount ; i++) {
63 callback_new_attr_args(void (*func)(void), enum attr_type type, int count, ...)
69 for (i = 0 ; i < count ; i++)
70 p[i]=va_arg(ap, void *);
72 return callback_new_attr(func, type, count, p);
76 callback_new(void (*func)(void), int pcount, void **p)
78 return callback_new_attr(func, attr_none, pcount, p);
82 callback_new_args(void (*func)(void), int count, ...)
88 for (i = 0 ; i < count ; i++)
89 p[i]=va_arg(ap, void *);
91 return callback_new(func, count, p);
95 callback_destroy(struct callback *cb)
101 callback_set_arg(struct callback *cb, int arg, void *p)
103 if (arg < 0 || arg > cb->pcount)
109 callback_list_add(struct callback_list *l, struct callback *cb)
111 l->list=g_list_prepend(l->list, cb);
116 callback_list_add_new(struct callback_list *l, void (*func)(void), int pcount, void **p)
118 struct callback *ret;
120 ret=callback_new(func, pcount, p);
121 callback_list_add(l, ret);
126 callback_list_remove(struct callback_list *l, struct callback *cb)
128 l->list=g_list_remove(l->list, cb);
132 callback_list_remove_destroy(struct callback_list *l, struct callback *cb)
134 callback_list_remove(l, cb);
139 callback_call(struct callback *cb, int pcount, void **p)
145 if (cb->pcount + pcount <= 8) {
146 dbg(1,"cb->pcount=%d\n", cb->pcount);
147 if (cb->pcount && cb->p)
148 dbg(1,"cb->p[0]=%p\n", cb->p[0]);
149 dbg(1,"pcount=%d\n", pcount);
152 dbg(1,"p[0]=%p\n", p[0]);
154 for (i = 0 ; i < cb->pcount ; i++)
156 for (i = 0 ; i < pcount ; i++)
157 pf[i+cb->pcount]=p[i];
158 switch (cb->pcount+pcount) {
160 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6],pf[7]);
163 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6]);
166 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5]);
169 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4]);
172 cb->func(pf[0],pf[1],pf[2],pf[3]);
175 cb->func(pf[0],pf[1],pf[2]);
178 cb->func(pf[0],pf[1]);
188 dbg(0,"too many parameters for callback (%d+%d)\n", cb->pcount, pcount);
193 callback_call_args(struct callback *cb, int count, ...)
199 for (i = 0 ; i < count ; i++)
200 p[i]=va_arg(ap, void *);
202 callback_call(cb, count, p);
206 callback_list_call_attr(struct callback_list *l, enum attr_type type, int pcount, void **p)
218 if (type == attr_any || cb->type == attr_any || cb->type == type)
219 callback_call(cb, pcount, p);
220 cbi=g_list_next(cbi);
226 callback_list_call_attr_args(struct callback_list *cbl, enum attr_type type, int count, ...)
232 for (i = 0 ; i < count ; i++)
233 p[i]=va_arg(ap, void *);
235 callback_list_call_attr(cbl, type, count, p);
239 callback_list_call(struct callback_list *l, int pcount, void **p)
241 callback_list_call_attr(l, attr_any, pcount, p);
245 callback_list_call_args(struct callback_list *cbl, int count, ...)
251 for (i = 0 ; i < count ; i++)
252 p[i]=va_arg(ap, void *);
254 callback_list_call(cbl, count, p);
258 callback_list_destroy(struct callback_list *l)
264 cbi=g_list_next(cbi);
266 g_list_free(l->list);