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);
151 dbg(1,"p[0]=%p\n", p[0]);
152 for (i = 0 ; i < cb->pcount ; i++)
154 for (i = 0 ; i < pcount ; i++)
155 pf[i+cb->pcount]=p[i];
156 switch (cb->pcount+pcount) {
158 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6],pf[7]);
161 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6]);
164 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5]);
167 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4]);
170 cb->func(pf[0],pf[1],pf[2],pf[3]);
173 cb->func(pf[0],pf[1],pf[2]);
176 cb->func(pf[0],pf[1]);
186 dbg(0,"too many parameters for callback (%d+%d)\n", cb->pcount, pcount);
191 callback_call_args(struct callback *cb, int count, ...)
197 for (i = 0 ; i < count ; i++)
198 p[i]=va_arg(ap, void *);
200 callback_call(cb, count, p);
204 callback_list_call_attr(struct callback_list *l, enum attr_type type, int pcount, void **p)
216 if (type == attr_any || cb->type == attr_any || cb->type == type)
217 callback_call(cb, pcount, p);
218 cbi=g_list_next(cbi);
224 callback_list_call_attr_args(struct callback_list *cbl, enum attr_type type, int count, ...)
230 for (i = 0 ; i < count ; i++)
231 p[i]=va_arg(ap, void *);
233 callback_list_call_attr(cbl, type, count, p);
237 callback_list_call(struct callback_list *l, int pcount, void **p)
239 callback_list_call_attr(l, attr_any, pcount, p);
243 callback_list_call_args(struct callback_list *cbl, int count, ...)
249 for (i = 0 ; i < count ; i++)
250 p[i]=va_arg(ap, void *);
252 callback_list_call(cbl, count, p);
256 callback_list_destroy(struct callback_list *l)
262 cbi=g_list_next(cbi);
264 g_list_free(l->list);