summaryrefslogtreecommitdiff
path: root/callback.c
blob: ea98c50845305017c9f1f4ef610798f6e92d5b9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <glib.h>
#include "debug.h"
#include "callback.h"

struct callback {
	void (*func)();
	int pcount;
	void *p[0];
	
};

struct callback_list {
	GList *list;
};

struct callback_list *
callback_list_new(void)
{
	struct callback_list *ret=g_new0(struct callback_list, 1);
	
	return ret;
}

struct callback *
callback_new(void (*func)(), int pcount, void **p)
{
	struct callback *ret;
	int i;

	ret=g_malloc0(sizeof(struct callback)+pcount*sizeof(void *));
	ret->func=func;
	ret->pcount=pcount;
	for (i = 0 ; i < pcount ; i++) {
		ret->p[i]=p[i];
	}	
	return ret;
}

void
callback_list_add(struct callback_list *l, struct callback *cb)
{
	l->list=g_list_prepend(l->list, cb);
}


struct callback *
callback_list_add_new(struct callback_list *l, void (*func)(), int pcount, void **p)
{
	struct callback *ret;
	
	ret=callback_new(func, pcount, p);	
	callback_list_add(l, ret);
	return ret;
}

void
callback_list_remove(struct callback_list *l, struct callback *cb)
{
	l->list=g_list_remove(l->list, cb);
}

void
callback_list_remove_destroy(struct callback_list *l, struct callback *cb)
{
	callback_list_remove(l, cb);
	g_free(cb);
}

void
callback_call(struct callback *cb, int pcount, void **p)
{
	int i;
	void *pf[8];
	if (! cb)
		return;
	if (cb->pcount + pcount <= 8) {
		dbg(1,"cb->pcount=%d\n", cb->pcount);
		if (cb->pcount && cb->p) 
			dbg(1,"cb->p[0]=%p\n", cb->p[0]);
		dbg(1,"pcount=%d\n", pcount);
		if (pcount && p) 
			dbg(1,"p[0]=%p\n", p[0]);
		for (i = 0 ; i < cb->pcount ; i++) 
			pf[i]=cb->p[i];
		for (i = 0 ; i < pcount ; i++)
			pf[i+cb->pcount]=p[i];
		switch (cb->pcount+pcount) {
		case 8:
			cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6],pf[7]);
			break;
		case 7:
			cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6]);
			break;
		case 6:
			cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5]);
			break;
		case 5:
			cb->func(pf[0],pf[1],pf[2],pf[3],pf[4]);
			break;
		case 4:
			cb->func(pf[0],pf[1],pf[2],pf[3]);
			break;
		case 3:
			cb->func(pf[0],pf[1],pf[2]);
			break;
		case 2:
			cb->func(pf[0],pf[1]);
			break;
		case 1:
			cb->func(pf[0]);
			break;
		case 0:
			cb->func();
			break;
		}
	} else {
		g_warning("too many parameters for callback (%d+%d)\n", cb->pcount, pcount);
	}
}


void
callback_list_call(struct callback_list *l, int pcount, void **p)
{
	GList *cbi;
	struct callback *cb;

	cbi=l->list;
	while (cbi) {
		cb=cbi->data;
		callback_call(cb, pcount, p);
		cbi=g_list_next(cbi);
	}
	
}


void 
callback_list_destroy(struct callback_list *l)
{
	GList *cbi;
	cbi=l->list;
	while (cbi) {
		g_free(cbi->data);
		cbi=g_list_next(cbi);
	}
	g_list_free(l->list);
	g_free(l);
}