/[zanavi_public1]/navit/navit/callback.c
ZANavi

Contents of /navit/navit/callback.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 40 - (show annotations) (download)
Wed Mar 4 14:00:54 2015 UTC (9 years, 1 month ago) by zoff99
File MIME type: text/plain
File size: 13057 byte(s)
new market version, lots of fixes
1 /**
2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
4 *
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.
8 *
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.
13 *
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.
18 */
19
20 #include <glib.h>
21 #include <string.h>
22 #include "item.h"
23 #include "debug.h"
24 #include "callback.h"
25 #include "navit.h"
26
27 struct callback_list *
28 callback_list_new(char *name)
29 {
30 struct callback_list *ret=g_new0(struct callback_list, 1);
31
32 if (name != NULL)
33 {
34 snprintf(ret->cb_name, 398, "%s", name);
35 ret->cb_name[397] = '\0';
36 ret->cb_name[398] = '\0';
37 ret->cb_name[399] = '\0';
38 }
39 else
40 {
41 snprintf(ret->cb_name, 398, "*unknown*");
42 // ret->cb_name[397] = '\0';
43 // ret->cb_name[398] = '\0';
44 // ret->cb_name[399] = '\0';
45 }
46
47 //dbg(0, "cbl:new:cbl=%p, name=%s, glob=%p\n", ret, name, global_all_cbs);
48 global_all_cbs = g_list_append(global_all_cbs, ret);
49
50 return ret;
51 }
52
53 void callback_dump_callbacks()
54 {
55
56 #ifndef NAVIT_DEBUG_BAREMETAL
57
58 struct callback_list *cl1;
59 struct callback *cb;
60 GList *cbi;
61 GList *all_cbl;
62
63 if (!global_all_cbs)
64 {
65 return;
66 }
67
68 all_cbl = global_all_cbs;
69
70 dbg(0,"cbl:=================================================\n");
71 dbg(0,"cbl:=================================================\n");
72
73 while (all_cbl)
74 {
75 if ((all_cbl) && (all_cbl->data))
76 {
77 cl1 = all_cbl->data;
78 dbg(0,"\n");
79 dbg(0,"cbl:%s(%p)\n", cl1->cb_name, cl1);
80
81 if ((cl1) && (cl1->list))
82 {
83 cbi = cl1->list;
84 while (cbi)
85 {
86 if (cbi == NULL)
87 {
88 break;
89 }
90
91 cb = cbi->data;
92 if (cb)
93 {
94 dbg(0," cb:%s:%s(%p) [f:%p type=%s]\n", cb->setup_func_name, cb->func_name, cb, cb->func, attr_to_name(cb->type));
95 }
96
97 cbi = g_list_next(cbi);
98 }
99 }
100 }
101
102 all_cbl = g_list_next(all_cbl);
103 }
104
105 dbg(0,"cbl:=================================================\n");
106 dbg(0,"cbl:=================================================\n");
107
108 #endif
109
110 }
111
112 struct callback *
113 callback_new_attr(void (*func)(void), enum attr_type type, int pcount, void **p)
114 {
115 struct callback *ret;
116 int i;
117
118 ret=g_malloc0(sizeof(struct callback)+pcount*sizeof(void *));
119 ret->func=func;
120 ret->pcount=pcount;
121 ret->type=type;
122 for (i = 0 ; i < pcount ; i++)
123 {
124 ret->p[i]=p[i];
125 }
126
127 ret->func_name[0] = 'e';
128 ret->func_name[1] = 'm';
129 ret->func_name[2] = 'p';
130 ret->func_name[3] = 't';
131 ret->func_name[4] = 'y';
132 ret->func_name[5] = '\0';
133
134 ret->setup_func_name[0] = 'e';
135 ret->setup_func_name[1] = 'm';
136 ret->setup_func_name[2] = 'p';
137 ret->setup_func_name[3] = 't';
138 ret->setup_func_name[4] = 'y';
139 ret->setup_func_name[5] = '\0';
140
141 return ret;
142 }
143
144 static void callback_print_names(struct callback *cb, const char *cb_func)
145 {
146 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
147 if (cb == NULL)
148 {
149 dbg(0,"%p CB_f=%s:CB=NULL!\n", cb, cb_func);
150 }
151 else
152 {
153 dbg(0,"%p CB_f=%s, parent=%s, func=%s\n", cb, cb_func, cb->setup_func_name, cb->func_name);
154 }
155 #endif
156 }
157
158 static void callback_print_names2(struct callback *cb, const char *cb_func, const char *module, const int mlen,const char *function)
159 {
160 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
161 if (cb == NULL)
162 {
163 dbg(0,"%p CB_f=%s:CB=NULL! file=%s line=%d func=%s\n", cb, cb_func, module, mlen, function);
164 }
165 else
166 {
167 dbg(0,"%p CB_f=%s, parent=%s, func=%s -- file=%s line=%d func=%s\n", cb, cb_func, cb->setup_func_name, cb->func_name, module, mlen, function);
168 }
169 #endif
170 }
171
172 void callback_add_names(struct callback *cb, const char *parent_name, const char *func_name)
173 {
174 if (cb == NULL)
175 {
176 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
177 dbg(0,"callback_add_names:CB=NULL!\n");
178 #endif
179 return;
180 }
181
182 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
183 dbg(0,"%p CB_I=%s, %s\n", cb, parent_name, func_name);
184 #endif
185
186 snprintf(cb->func_name, 398, "%s", func_name);
187 cb->func_name[397] = '\0';
188 cb->func_name[398] = '\0';
189 cb->func_name[399] = '\0';
190
191 snprintf(cb->setup_func_name, 398, "%s", parent_name);
192 cb->setup_func_name[397] = '\0';
193 cb->setup_func_name[398] = '\0';
194 cb->setup_func_name[399] = '\0';
195
196 }
197
198 struct callback *
199 callback_new_attr_args(const char *module, const int mlen,const char *function, void (*func)(void), enum attr_type type, int count, ...)
200 {
201 int i;
202 void **p=g_alloca(sizeof(void*)*count);
203 va_list ap;
204 va_start(ap, count);
205 for (i = 0 ; i < count ; i++)
206 {
207 p[i]=va_arg(ap, void *);
208 }
209
210 va_end(ap);
211
212 struct callback *ret = callback_new_attr(func, type, count, p);
213 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
214 dbg(0,"%p callback_new_attr_%d -- file=%s line=%d func=%s type=%s\n", ret, count, module, mlen, function, attr_to_name(type));
215 #endif
216 return ret;
217 }
218
219 struct callback *
220 callback_new(void (*func)(void), int pcount, void **p)
221 {
222 return callback_new_attr(func, attr_none, pcount, p);
223 }
224
225 struct callback *
226 callback_new_args(const char *module, const int mlen,const char *function, void (*func)(void), int count, ...)
227 {
228 int i;
229 void **p=g_alloca(sizeof(void*)*count);
230 va_list ap;
231 va_start(ap, count);
232 for (i = 0 ; i < count ; i++)
233 {
234 p[i]=va_arg(ap, void *);
235 }
236 va_end(ap);
237
238 struct callback *ret = callback_new(func, count, p);
239
240 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
241 dbg(0,"%p callback_new_%d -- file=%s line=%d func=%s\n", ret, count, module, mlen, function);
242 #endif
243 return ret;
244 }
245
246 void
247 callback_destroy_real(const char *module, const int mlen,const char *function, struct callback *cb)
248 {
249 if (cb == NULL)
250 {
251 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
252 dbg(0,"%p callback_destroy_real:CB=NULL!! -- file=%s line=%d func=%s\n", cb, module, mlen, function);
253 #endif
254 return;
255 }
256
257 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
258 callback_print_names2(cb, "callback_destroy", module, mlen, function);
259 #endif
260
261 g_free(cb);
262 }
263
264 void
265 callback_set_arg(struct callback *cb, int arg, void *p)
266 {
267 if (arg < 0 || arg > cb->pcount)
268 {
269 return;
270 }
271 cb->p[arg]=p;
272 }
273
274 void callback_list_add_2(const char *module, const int mlen,const char *function, struct callback_list *l, struct callback *cb)
275 {
276 if (cb == NULL)
277 {
278 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
279 dbg(0,"callback_list_add_2:CB=NULL!!\n");
280 #endif
281 return;
282 }
283
284 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
285 dbg(0,"callback_list_add_2:cbl=%p cb=%p file=%s line=%d func=%s\n", l, cb, module, mlen, function);
286 #endif
287
288 l->list=g_list_prepend(l->list, cb);
289 }
290
291
292 void
293 callback_list_add_internal(struct callback_list *l, struct callback *cb)
294 {
295 if (cb == NULL)
296 {
297 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
298 dbg(0,"callback_list_add:CB=NULL!!\n");
299 #endif
300 return;
301 }
302
303 l->list=g_list_prepend(l->list, cb);
304 }
305
306
307
308 struct callback *
309 callback_list_add_new(struct callback_list *l, void (*func)(void), int pcount, void **p)
310 {
311 struct callback *ret;
312
313 ret=callback_new(func, pcount, p);
314 callback_list_add_internal(l, ret);
315 return ret;
316 }
317
318 void callback_list_remove_2(const char *module, const int mlen,const char *function, struct callback_list *l, struct callback *cb)
319 {
320 //dbg(0,"callback_list_remove_2:******************************************\n");
321
322 if (l == NULL)
323 {
324 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
325 dbg(0,"callback_list_remove_2:CBL=NULL!!\n");
326 #endif
327 return;
328 }
329
330 if (cb == NULL)
331 {
332 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
333 dbg(0,"callback_list_remove_2:CB=NULL!!\n");
334 #endif
335 return;
336 }
337
338 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
339 dbg(0,"callback_list_remove_2:cbl=%p cb=%p file=%s line=%d func=%s\n", l, cb, module, mlen, function);
340 #endif
341
342 l->list = g_list_remove(l->list, cb);
343
344 //dbg(0,"callback_list_remove_2:******************************************RRRRRRRRRR\n");
345 }
346
347
348 void
349 callback_list_remove_internal(struct callback_list *l, struct callback *cb)
350 {
351 l->list = g_list_remove(l->list, cb);
352 }
353
354
355 void
356 callback_list_remove_destroy(struct callback_list *l, struct callback *cb)
357 {
358 if (l == NULL)
359 {
360 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
361 dbg(0,"callback_list_remove_destroy:CBL=NULL!!\n");
362 #endif
363 return;
364 }
365
366 if (cb == NULL)
367 {
368 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
369 dbg(0,"callback_list_remove_destroy:CB=NULL!!\n");
370 #endif
371 return;
372 }
373
374 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
375 callback_print_names(cb, "callback_list_remove_destroy");
376 #endif
377
378 callback_list_remove_internal(l, cb);
379 g_free(cb);
380 }
381
382 void
383 callback_call(struct callback *cb, int pcount, void **p)
384 {
385 int i;
386 void *pf[8];
387
388 #ifndef NAVIT_DEBUG_BAREMETAL
389
390 if (! cb)
391 {
392 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
393 dbg(0,"callback_call:CB=NULL!!\n");
394 #endif
395 return;
396 }
397
398 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
399 callback_print_names(cb, "callback_call");
400 #endif
401
402 if (cb->pcount + pcount <= 8)
403 {
404 //dbg(1,"cb->pcount=%d\n", cb->pcount);
405 if (cb->pcount && cb->p)
406 {
407 //dbg(1,"cb->p[0]=%p\n", cb->p[0]);
408 }
409 //dbg(1,"pcount=%d\n", pcount);
410 if (pcount)
411 {
412 dbg_assert(p!=NULL);
413 //dbg(1,"p[0]=%p\n", p[0]);
414 }
415
416 for (i = 0 ; i < cb->pcount ; i++)
417 pf[i]=cb->p[i];
418
419 for (i = 0 ; i < pcount ; i++)
420 pf[i+cb->pcount]=p[i];
421
422 switch (cb->pcount+pcount)
423 {
424 case 8:
425 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6],pf[7]);
426 break;
427 case 7:
428 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6]);
429 break;
430 case 6:
431 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5]);
432 break;
433 case 5:
434 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4]);
435 break;
436 case 4:
437 cb->func(pf[0],pf[1],pf[2],pf[3]);
438 break;
439 case 3:
440 cb->func(pf[0],pf[1],pf[2]);
441 break;
442 case 2:
443 cb->func(pf[0],pf[1]);
444 break;
445 case 1:
446 cb->func(pf[0]);
447 break;
448 case 0:
449 cb->func();
450 break;
451 }
452 }
453 else
454 {
455 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
456 dbg(0,"too many parameters for callback (%d+%d)\n", cb->pcount, pcount);
457 #endif
458 }
459
460 #endif
461 }
462
463 void
464 callback_call_args_real(const char *module, const int mlen,const char *function, struct callback *cb, int count, ...)
465 {
466
467 #ifndef NAVIT_DEBUG_BAREMETAL
468
469 if (cb == NULL)
470 {
471 // callback struct is NULL
472 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
473 dbg(0,"callback_call:CB=NULL! file=%s line=%d func=%s\n", module, mlen, function);
474 #endif
475 return;
476 }
477
478 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
479 callback_print_names2(cb, "callback_call", module, mlen, function);
480 #endif
481
482 int i;
483 void **p=g_alloca(sizeof(void*)*count);
484 va_list ap;
485 va_start(ap, count);
486
487 for (i = 0 ; i < count ; i++)
488 {
489 p[i]=va_arg(ap, void *);
490 }
491
492 va_end(ap);
493 callback_call(cb, count, p);
494
495 #endif
496 }
497
498
499 void
500 callback_list_call_attr(struct callback_list *l, enum attr_type type, int pcount, void **p)
501 {
502
503 #ifndef NAVIT_DEBUG_BAREMETAL
504
505 GList *cbi;
506 struct callback *cb;
507
508 if (!l)
509 {
510 return;
511 }
512
513 cbi=l->list;
514 while (cbi)
515 {
516 cb=cbi->data;
517 if (type == attr_any || cb->type == attr_any || cb->type == type)
518 {
519 callback_call(cb, pcount, p);
520 }
521 cbi=g_list_next(cbi);
522 }
523
524 #endif
525 }
526
527 void
528 callback_list_call_attr_args(const char *module, const int mlen,const char *function, struct callback_list *cbl, enum attr_type type, int count, ...)
529 {
530
531 #ifndef NAVIT_DEBUG_BAREMETAL
532
533 int i;
534 void **p=g_alloca(sizeof(void*)*count);
535 va_list ap;
536 va_start(ap, count);
537
538 for (i = 0 ; i < count ; i++)
539 p[i]=va_arg(ap, void *);
540
541 va_end(ap);
542
543 if (cbl == NULL)
544 {
545 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
546 dbg(0,"%p callback_list_call_attr_args:CBL=NULL! file=%s line=%d func=%s type=%s\n", cbl, module, mlen, function, attr_to_name(type));
547 #endif
548 return;
549 }
550 else
551 {
552 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
553 dbg(0,"%p callback_list_call_attr_args:file=%s line=%d func=%s type=%s\n", cbl, module, mlen, function, attr_to_name(type));
554 #endif
555 }
556
557 callback_list_call_attr(cbl, type, count, p);
558
559 #endif
560 }
561
562 void
563 callback_list_call(struct callback_list *l, int pcount, void **p)
564 {
565 #ifndef NAVIT_DEBUG_BAREMETAL
566 callback_list_call_attr(l, attr_any, pcount, p);
567 #endif
568 }
569
570 void
571 callback_list_call_args(const char *module, const int mlen,const char *function, struct callback_list *cbl, int count, ...)
572 {
573
574 #ifndef NAVIT_DEBUG_BAREMETAL
575
576 if (cbl == NULL)
577 {
578 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
579 dbg(0,"%p callback_list_call_args:CBL=NULL! file=%s line=%d func=%s\n", cbl, module, mlen, function);
580 #endif
581 return;
582 }
583 else
584 {
585 #ifdef NAVIT_CALLBACK_DEBUG_PRINT
586 dbg(0,"%p callback_list_call_args:file=%s line=%d func=%s\n", cbl, module, mlen, function);
587 #endif
588 }
589
590 int i;
591 void **p=g_alloca(sizeof(void*)*count);
592 va_list ap;
593 va_start(ap, count);
594
595 for (i = 0 ; i < count ; i++)
596 {
597 p[i]=va_arg(ap, void *);
598 }
599
600 va_end(ap);
601
602 callback_list_call(cbl, count, p);
603
604 #endif
605 }
606
607 void
608 callback_list_destroy(struct callback_list *l)
609 {
610 GList *cbi;
611
612 if (!l)
613 {
614 return;
615 }
616
617 //dbg(0, "cbl:destroy:cbl=%p, name=%s, glob=%p\n", l, l->cb_name, global_all_cbs);
618 global_all_cbs = g_list_remove(global_all_cbs, l);
619
620 cbi=l->list;
621
622 if (!cbi)
623 {
624 g_free(l);
625 return;
626 }
627
628 while (cbi)
629 {
630 if (cbi == NULL)
631 {
632 break;
633 }
634
635 if (cbi->data)
636 {
637 g_free(cbi->data);
638 }
639 cbi=g_list_next(cbi);
640 }
641
642 g_list_free(l->list);
643
644 g_free(l);
645
646 }
647
648

   
Visit the ZANavi Wiki