mega modif by Mathieu Desnoyers. Independant main windows, multiple tracesets, contro...
[lttv.git] / ltt / branches / poly / lttv / hook.c
1
2 #include <lttv/hook.h>
3
4
5 typedef struct _LttvHookClosure {
6 LttvHook hook;
7 void *hook_data;
8 } LttvHookClosure;
9
10
11 LttvHooks *lttv_hooks_new()
12 {
13 return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure));
14 }
15
16
17 void lttv_hooks_destroy(LttvHooks *h)
18 {
19 g_critical("lttv_hooks_destroy()");
20 g_array_free(h, TRUE);
21 }
22
23
24 void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data)
25 {
26 LttvHookClosure c;
27
28 if(h == NULL)g_error("Null hook added");
29
30 c.hook = f;
31 c.hook_data = hook_data;
32 g_array_append_val(h,c);
33 }
34
35
36 void lttv_hooks_add_list(LttvHooks *h, LttvHooks *list)
37 {
38 guint i;
39
40 if(list == NULL) return;
41 for(i = 0 ; i < list->len; i++) {
42 g_array_append_val(h,g_array_index(list, LttvHookClosure, i));
43 }
44 }
45
46
47 void *lttv_hooks_remove(LttvHooks *h, LttvHook f)
48 {
49 unsigned i;
50
51 void *hook_data;
52
53 LttvHookClosure *c;
54
55 for(i = 0 ; i < h->len ; i++) {
56 c = &g_array_index(h, LttvHookClosure, i);
57 if(c->hook == f) {
58 hook_data = c->hook_data;
59 lttv_hooks_remove_by_position(h, i);
60 return hook_data;
61 }
62 }
63 return NULL;
64 }
65
66
67 void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data)
68 {
69 unsigned i;
70
71 LttvHookClosure *c;
72
73 for(i = 0 ; i < h->len ; i++) {
74 c = &g_array_index(h, LttvHookClosure, i);
75 if(c->hook == f && c->hook_data == hook_data) {
76 lttv_hooks_remove_by_position(h, i);
77 return;
78 }
79 }
80 }
81
82
83 void lttv_hooks_remove_list(LttvHooks *h, LttvHooks *list)
84 {
85 guint i, j;
86
87 LttvHookClosure *c, *c_list;
88
89 if(list == NULL) return;
90 for(i = 0, j = 0 ; i < h->len && j < list->len ;) {
91 c = &g_array_index(h, LttvHookClosure, i);
92 c_list = &g_array_index(list, LttvHookClosure, j);
93 if(c->hook == c_list->hook && c->hook_data == c_list->hook_data) {
94 lttv_hooks_remove_by_position(h, i);
95 j++;
96 }
97 else i++;
98 }
99
100 /* Normally the hooks in h are ordered as in list. If this is not the case,
101 try harder here. */
102
103 if(j < list->len) {
104 for(; j < list->len ; j++) {
105 c_list = &g_array_index(list, LttvHookClosure, j);
106 lttv_hooks_remove_data(h, c_list->hook, c_list->hook_data);
107 }
108 }
109 }
110
111
112 unsigned lttv_hooks_number(LttvHooks *h)
113 {
114 return h->len;
115 }
116
117
118 void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data)
119 {
120 LttvHookClosure *c;
121
122 c = &g_array_index(h, LttvHookClosure, i);
123 *f = c->hook;
124 *hook_data = c->hook_data;
125 }
126
127
128 void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i)
129 {
130 g_array_remove_index(h, i);
131 }
132
133
134 gboolean lttv_hooks_call(LttvHooks *h, void *call_data)
135 {
136 gboolean ret, sum_ret = FALSE;
137
138 LttvHookClosure *c;
139
140 guint i;
141
142 if(h != NULL) {
143 for(i = 0 ; i < h->len ; i++) {
144 c = &g_array_index(h, LttvHookClosure, i);
145 ret = c->hook(c->hook_data,call_data);
146 sum_ret = sum_ret || ret;
147 }
148 }
149 return sum_ret;
150 }
151
152
153 gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data)
154 {
155 LttvHookClosure *c;
156
157 guint i;
158
159 for(i = 0 ; i < h->len ; i++) {
160 c = &g_array_index(h, LttvHookClosure, i);
161 if(c->hook(c->hook_data,call_data)) return TRUE;
162 }
163 return FALSE;
164 }
165
166
167 LttvHooksById *lttv_hooks_by_id_new()
168 {
169 return g_ptr_array_new();
170 }
171
172
173 void lttv_hooks_by_id_destroy(LttvHooksById *h)
174 {
175 guint i;
176
177 for(i = 0 ; i < h->len ; i++) {
178 if(h->pdata[i] != NULL) lttv_hooks_destroy((LttvHooks *)(h->pdata[i]));
179 }
180 g_ptr_array_free(h, TRUE);
181 }
182
183
184 LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id)
185 {
186 if(h->len <= id) g_ptr_array_set_size(h, id + 1);
187 if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new();
188 return h->pdata[id];
189 }
190
191
192 unsigned lttv_hooks_by_id_max_id(LttvHooksById *h)
193 {
194 return h->len;
195 }
196
197
198 LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id)
199 {
200 if(id < h->len) return h->pdata[id];
201 return NULL;
202 }
203
204
205 void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id)
206 {
207 if(id < h->len && h->pdata[id] != NULL) {
208 lttv_hooks_destroy((LttvHooks *)h->pdata[id]);
209 h->pdata[id] = NULL;
210 }
211 }
212
This page took 0.043161 seconds and 4 git commands to generate.