Mercurial > pidgin
annotate src/signals.c @ 6527:aa86c48d77c7
[gaim-migrate @ 7044]
I'm sad to report that NULL pointers that should have data are also no
longer in style.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Wed, 20 Aug 2003 10:50:11 +0000 |
parents | e74e378e86bf |
children | adf168f002ad |
rev | line source |
---|---|
6485 | 1 /** |
6488
e5e8d21bd4d8
[gaim-migrate @ 7002]
Christian Hammond <chipx86@chipx86.com>
parents:
6485
diff
changeset
|
2 * @file signals.c Signal API |
6485 | 3 * @ingroup core |
4 * | |
5 * gaim | |
6 * | |
7 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
8 * | |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 */ | |
23 #include "internal.h" | |
24 | |
25 #include "debug.h" | |
26 #include "signals.h" | |
27 | |
28 typedef struct | |
29 { | |
30 void *instance; | |
31 | |
32 GHashTable *signals; | |
33 size_t signal_count; | |
34 | |
35 gulong next_signal_id; | |
36 | |
37 } GaimInstanceData; | |
38 | |
39 typedef struct | |
40 { | |
41 gulong id; | |
42 | |
43 GaimSignalMarshalFunc marshal; | |
44 | |
45 GList *handlers; | |
46 size_t handler_count; | |
47 | |
48 gulong next_handler_id; | |
49 | |
50 } GaimSignalData; | |
51 | |
52 typedef struct | |
53 { | |
54 gulong id; | |
55 GaimCallback cb; | |
56 void *handle; | |
57 void *data; | |
58 | |
59 } GaimSignalHandlerData; | |
60 | |
61 static GHashTable *instance_table = NULL; | |
62 | |
63 static void | |
64 destroy_instance_data(GaimInstanceData *instance_data) | |
65 { | |
66 g_hash_table_destroy(instance_data->signals); | |
67 | |
68 g_free(instance_data); | |
69 } | |
70 | |
71 static void | |
72 destroy_signal_data(GaimSignalData *signal_data) | |
73 { | |
74 GaimSignalHandlerData *handler_data; | |
75 GList *l; | |
76 | |
77 for (l = signal_data->handlers; l != NULL; l = l->next) | |
78 { | |
79 handler_data = (GaimSignalHandlerData *)l->data; | |
80 | |
81 g_free(l->data); | |
82 } | |
83 | |
84 g_list_free(signal_data->handlers); | |
85 | |
86 g_free(signal_data); | |
87 } | |
88 | |
89 gulong | |
90 gaim_signal_register(void *instance, const char *signal, | |
91 GaimSignalMarshalFunc marshal) | |
92 { | |
93 GaimInstanceData *instance_data; | |
94 GaimSignalData *signal_data; | |
95 | |
96 g_return_val_if_fail(instance != NULL, 0); | |
97 g_return_val_if_fail(signal != NULL, 0); | |
98 g_return_val_if_fail(marshal != NULL, 0); | |
99 | |
100 instance_data = | |
101 (GaimInstanceData *)g_hash_table_lookup(instance_table, instance); | |
102 | |
103 if (instance_data == NULL) | |
104 { | |
105 instance_data = g_new0(GaimInstanceData, 1); | |
106 | |
107 instance_data->instance = instance; | |
108 instance_data->next_signal_id = 1; | |
109 | |
110 instance_data->signals = | |
111 g_hash_table_new_full(g_str_hash, g_str_equal, g_free, | |
112 (GDestroyNotify)destroy_signal_data); | |
113 | |
114 g_hash_table_insert(instance_table, instance, instance_data); | |
115 } | |
116 | |
117 signal_data = g_new0(GaimSignalData, 1); | |
118 signal_data->id = instance_data->next_signal_id; | |
119 signal_data->marshal = marshal; | |
120 signal_data->next_handler_id = 1; | |
121 | |
122 g_hash_table_insert(instance_data->signals, | |
123 g_strdup(signal), signal_data); | |
124 | |
125 instance_data->next_signal_id++; | |
126 instance_data->signal_count++; | |
127 | |
128 return signal_data->id; | |
129 } | |
130 | |
131 void | |
132 gaim_signal_unregister(void *instance, const char *signal) | |
133 { | |
134 GaimInstanceData *instance_data; | |
135 | |
136 g_return_if_fail(instance != NULL); | |
137 g_return_if_fail(signal != NULL); | |
138 | |
139 instance_data = | |
140 (GaimInstanceData *)g_hash_table_lookup(instance_table, instance); | |
141 | |
142 g_return_if_fail(instance_data != NULL); | |
143 | |
144 g_hash_table_remove(instance_data->signals, signal); | |
145 | |
146 instance_data->signal_count--; | |
147 | |
148 if (instance_data->signal_count == 0) | |
149 { | |
150 /* Unregister the instance. */ | |
151 g_hash_table_remove(instance_table, instance); | |
152 } | |
153 } | |
154 | |
155 void | |
156 gaim_signals_unregister_by_instance(void *instance) | |
157 { | |
158 gboolean found; | |
159 | |
160 g_return_if_fail(instance != NULL); | |
161 | |
162 found = g_hash_table_remove(instance_table, instance); | |
163 | |
164 /* | |
165 * Makes things easier (more annoying?) for developers who don't have | |
166 * things registering and unregistering in the right order :) | |
167 */ | |
168 g_return_if_fail(found); | |
169 } | |
170 | |
171 gulong | |
172 gaim_signal_connect(void *instance, const char *signal, void *handle, | |
173 GaimCallback func, void *data) | |
174 { | |
175 GaimInstanceData *instance_data; | |
176 GaimSignalData *signal_data; | |
177 GaimSignalHandlerData *handler_data; | |
178 | |
179 g_return_val_if_fail(instance != NULL, 0); | |
180 g_return_val_if_fail(signal != NULL, 0); | |
181 g_return_val_if_fail(handle != NULL, 0); | |
182 g_return_val_if_fail(func != NULL, 0); | |
183 | |
184 /* Get the instance data */ | |
185 instance_data = | |
186 (GaimInstanceData *)g_hash_table_lookup(instance_table, instance); | |
187 | |
188 g_return_val_if_fail(instance_data != NULL, 0); | |
189 | |
190 /* Get the signal data */ | |
191 signal_data = | |
192 (GaimSignalData *)g_hash_table_lookup(instance_data->signals, signal); | |
193 | |
194 if (signal_data == NULL) | |
195 { | |
196 gaim_debug(GAIM_DEBUG_ERROR, "signals", | |
197 "Signal data for %s not found!\n", signal); | |
198 return 0; | |
199 } | |
200 | |
201 /* Create the signal handler data */ | |
202 handler_data = g_new0(GaimSignalHandlerData, 1); | |
203 handler_data->id = signal_data->next_handler_id; | |
204 handler_data->cb = func; | |
205 handler_data->handle = handle; | |
206 handler_data->data = data; | |
207 | |
208 signal_data->handlers = g_list_append(signal_data->handlers, handler_data); | |
209 signal_data->handler_count++; | |
210 signal_data->next_handler_id++; | |
211 | |
212 return handler_data->id; | |
213 } | |
214 | |
215 void | |
216 gaim_signal_disconnect(void *instance, const char *signal, | |
217 void *handle, GaimCallback func) | |
218 { | |
219 GaimInstanceData *instance_data; | |
220 GaimSignalData *signal_data; | |
221 GaimSignalHandlerData *handler_data; | |
222 GList *l; | |
223 gboolean found = FALSE; | |
224 | |
225 g_return_if_fail(instance != NULL); | |
226 g_return_if_fail(signal != NULL); | |
227 g_return_if_fail(handle != NULL); | |
228 g_return_if_fail(func != NULL); | |
229 | |
230 /* Get the instance data */ | |
231 instance_data = | |
232 (GaimInstanceData *)g_hash_table_lookup(instance_table, instance); | |
233 | |
234 g_return_if_fail(instance_data != NULL); | |
235 | |
236 /* Get the signal data */ | |
237 signal_data = | |
238 (GaimSignalData *)g_hash_table_lookup(instance_data->signals, signal); | |
239 | |
240 if (signal_data == NULL) | |
241 { | |
242 gaim_debug(GAIM_DEBUG_ERROR, "signals", | |
243 "Signal data for %s not found!\n", signal); | |
244 return; | |
245 } | |
246 | |
247 /* Find the handler data. */ | |
248 for (l = signal_data->handlers; l != NULL; l = l->next) | |
249 { | |
250 handler_data = (GaimSignalHandlerData *)l->data; | |
251 | |
252 if (handler_data->handle == handle && handler_data->cb == func) | |
253 { | |
254 g_free(handler_data); | |
255 | |
256 signal_data->handlers = g_list_remove(signal_data->handlers, | |
257 handler_data); | |
258 signal_data->handler_count--; | |
259 | |
260 found = TRUE; | |
261 | |
262 break; | |
263 } | |
264 } | |
265 | |
266 /* See note somewhere about this actually helping developers.. */ | |
267 g_return_if_fail(found); | |
268 } | |
269 | |
270 /* | |
271 * TODO: Make this all more efficient by storing a list of handlers, keyed | |
272 * to a handle. | |
273 */ | |
274 static void | |
275 disconnect_handle_from_signals(const char *signal, | |
276 GaimSignalData *signal_data, void *handle) | |
277 { | |
278 GList *l, *l_next; | |
279 GaimSignalHandlerData *handler_data; | |
280 | |
281 for (l = signal_data->handlers; l != NULL; l = l_next) | |
282 { | |
283 handler_data = (GaimSignalHandlerData *)l->data; | |
284 l_next = l->next; | |
285 | |
286 if (handler_data->handle == handle) | |
287 { | |
288 g_free(handler_data); | |
289 | |
290 signal_data->handler_count--; | |
291 signal_data->handlers = g_list_remove(signal_data->handlers, | |
292 handler_data); | |
293 } | |
294 } | |
295 } | |
296 | |
297 static void | |
298 disconnect_handle_from_instance(void *instance, | |
299 GaimInstanceData *instance_data, | |
300 void *handle) | |
301 { | |
302 g_hash_table_foreach(instance_data->signals, | |
303 (GHFunc)disconnect_handle_from_signals, handle); | |
304 } | |
305 | |
306 void | |
307 gaim_signals_disconnect_by_handle(void *handle) | |
308 { | |
309 g_return_if_fail(handle != NULL); | |
310 | |
311 g_hash_table_foreach(instance_table, | |
312 (GHFunc)disconnect_handle_from_instance, handle); | |
313 } | |
314 | |
315 void | |
316 gaim_signal_emit(void *instance, const char *signal, ...) | |
317 { | |
318 va_list args; | |
319 | |
320 va_start(args, signal); | |
321 gaim_signal_emit_vargs(instance, signal, args); | |
322 va_end(args); | |
323 } | |
324 | |
325 void | |
326 gaim_signal_emit_vargs(void *instance, const char *signal, va_list args) | |
327 { | |
328 GaimInstanceData *instance_data; | |
329 GaimSignalData *signal_data; | |
330 GaimSignalHandlerData *handler_data; | |
331 GList *l; | |
332 | |
333 g_return_if_fail(instance != NULL); | |
334 g_return_if_fail(signal != NULL); | |
335 | |
336 instance_data = | |
337 (GaimInstanceData *)g_hash_table_lookup(instance_table, instance); | |
338 | |
339 g_return_if_fail(instance_data != NULL); | |
340 | |
341 signal_data = | |
342 (GaimSignalData *)g_hash_table_lookup(instance_data->signals, signal); | |
343 | |
344 if (signal_data == NULL) | |
345 { | |
346 gaim_debug(GAIM_DEBUG_ERROR, "signals", | |
347 "Signal data for %s not found!\n", signal); | |
348 return; | |
349 } | |
350 | |
351 for (l = signal_data->handlers; l != NULL; l = l->next) | |
352 { | |
353 handler_data = (GaimSignalHandlerData *)l->data; | |
354 | |
355 signal_data->marshal(handler_data->cb, args, handler_data->data, NULL); | |
356 } | |
357 } | |
358 | |
359 void * | |
360 gaim_signal_emit_return_1(void *instance, const char *signal, ...) | |
361 { | |
362 void *ret_val; | |
363 va_list args; | |
364 | |
365 va_start(args, signal); | |
366 ret_val = gaim_signal_emit_vargs_return_1(instance, signal, args); | |
367 va_end(args); | |
368 | |
369 return ret_val; | |
370 } | |
371 | |
372 void * | |
373 gaim_signal_emit_vargs_return_1(void *instance, const char *signal, | |
374 va_list args) | |
375 { | |
376 GaimInstanceData *instance_data; | |
377 GaimSignalData *signal_data; | |
378 GaimSignalHandlerData *handler_data; | |
379 void *ret_val = NULL; | |
380 GList *l; | |
381 | |
382 g_return_val_if_fail(instance != NULL, NULL); | |
383 g_return_val_if_fail(signal != NULL, NULL); | |
384 | |
385 instance_data = | |
386 (GaimInstanceData *)g_hash_table_lookup(instance_table, instance); | |
387 | |
388 g_return_val_if_fail(instance_data != NULL, NULL); | |
389 | |
390 signal_data = | |
391 (GaimSignalData *)g_hash_table_lookup(instance_data->signals, signal); | |
392 | |
393 if (signal_data == NULL) | |
394 { | |
395 gaim_debug(GAIM_DEBUG_ERROR, "signals", | |
396 "Signal data for %s not found!\n", signal); | |
397 return 0; | |
398 } | |
399 | |
400 for (l = signal_data->handlers; l != NULL; l = l->next) | |
401 { | |
402 handler_data = (GaimSignalHandlerData *)l->data; | |
403 | |
404 signal_data->marshal(handler_data->cb, args, handler_data->data, | |
405 &ret_val); | |
406 } | |
407 | |
408 return ret_val; | |
409 } | |
410 | |
411 void | |
412 gaim_signals_init() | |
413 { | |
414 g_return_if_fail(instance_table == NULL); | |
415 | |
416 instance_table = | |
417 g_hash_table_new_full(g_direct_hash, g_direct_equal, | |
418 NULL, (GDestroyNotify)destroy_instance_data); | |
419 } | |
420 | |
421 void | |
422 gaim_signals_uninit() | |
423 { | |
424 g_return_if_fail(instance_table != NULL); | |
425 | |
426 g_hash_table_destroy(instance_table); | |
427 instance_table = NULL; | |
428 } | |
429 | |
430 /************************************************************************** | |
431 * Marshallers | |
432 **************************************************************************/ | |
433 void | |
434 gaim_marshal_VOID(GaimCallback cb, va_list args, void *data, | |
435 void **return_val) | |
436 { | |
437 ((void (*)(void *))cb)(data); | |
438 } | |
439 | |
440 void | |
441 gaim_marshal_VOID__POINTER(GaimCallback cb, va_list args, void *data, | |
442 void **return_val) | |
443 { | |
6505 | 444 void *arg1 = va_arg(args, void *); |
445 | |
446 ((void (*)(void *, void *))cb)(arg1, data); | |
6485 | 447 } |
448 | |
449 void | |
450 gaim_marshal_VOID__POINTER_POINTER(GaimCallback cb, va_list args, | |
451 void *data, void **return_val) | |
452 { | |
6505 | 453 void *arg1 = va_arg(args, void *); |
454 void *arg2 = va_arg(args, void *); | |
455 | |
456 ((void (*)(void *, void *, void *))cb)(arg1, arg2, data); | |
6485 | 457 } |
458 | |
459 void | |
460 gaim_marshal_VOID__POINTER_POINTER_UINT(GaimCallback cb, va_list args, | |
461 void *data, void **return_val) | |
462 { | |
6505 | 463 void *arg1 = va_arg(args, void *); |
464 void *arg2 = va_arg(args, void *); | |
465 guint arg3 = va_arg(args, guint); | |
466 | |
467 ((void (*)(void *, void *, guint, void *))cb)(arg1, arg2, arg3, data); | |
6485 | 468 } |
469 | |
470 void | |
471 gaim_marshal_VOID__POINTER_POINTER_POINTER(GaimCallback cb, va_list args, | |
472 void *data, void **return_val) | |
473 { | |
6505 | 474 void *arg1 = va_arg(args, void *); |
475 void *arg2 = va_arg(args, void *); | |
476 void *arg3 = va_arg(args, void *); | |
477 | |
478 ((void (*)(void *, void *, void *, void *))cb)(arg1, arg2, arg3, data); | |
6485 | 479 } |
480 | |
481 void | |
482 gaim_marshal_VOID__POINTER_POINTER_POINTER_POINTER(GaimCallback cb, | |
483 va_list args, | |
484 void *data, | |
485 void **return_val) | |
486 { | |
6505 | 487 void *arg1 = va_arg(args, void *); |
488 void *arg2 = va_arg(args, void *); | |
489 void *arg3 = va_arg(args, void *); | |
490 void *arg4 = va_arg(args, void *); | |
491 | |
492 ((void (*)(void *, void *, void *, void *, void *))cb)(arg1, arg2, arg3, arg4, data); | |
6485 | 493 } |
6509 | 494 |
495 void | |
496 gaim_marshal_VOID__POINTER_POINTER_POINTER_UINT(GaimCallback cb, | |
497 va_list args, | |
498 void *data, | |
499 void **return_val) | |
500 { | |
501 void *arg1 = va_arg(args, void *); | |
502 void *arg2 = va_arg(args, void *); | |
503 void *arg3 = va_arg(args, void *); | |
504 guint arg4 = va_arg(args, guint); | |
505 | |
506 ((void (*)(void *, void *, void *, guint, void *))cb)(arg1, arg2, arg3, arg4, data); | |
507 } | |
508 | |
6485 | 509 void |
510 gaim_marshal_VOID__POINTER_POINTER_POINTER_UINT_UINT(GaimCallback cb, | |
511 va_list args, | |
512 void *data, | |
513 void **return_val) | |
514 { | |
6505 | 515 void *arg1 = va_arg(args, void *); |
516 void *arg2 = va_arg(args, void *); | |
517 void *arg3 = va_arg(args, void *); | |
518 guint arg4 = va_arg(args, guint); | |
519 guint arg5 = va_arg(args, guint); | |
520 | |
6485 | 521 ((void (*)(void *, void *, void *, guint, guint, void *))cb)( |
6505 | 522 arg1, arg2, arg3, arg4, arg5, data); |
6485 | 523 } |
524 | |
525 void | |
526 gaim_marshal_BOOLEAN__POINTER(GaimCallback cb, va_list args, void *data, | |
527 void **return_val) | |
528 { | |
529 gboolean ret_val; | |
6505 | 530 void *arg1 = va_arg(args, void *); |
6485 | 531 |
6505 | 532 ret_val = ((gboolean (*)(void *, void *))cb)(arg1, data); |
6485 | 533 |
534 if (return_val != NULL) | |
535 *return_val = GINT_TO_POINTER(ret_val); | |
536 } | |
537 | |
538 void | |
539 gaim_marshal_BOOLEAN__POINTER_POINTER(GaimCallback cb, va_list args, | |
540 void *data, void **return_val) | |
541 { | |
542 gboolean ret_val; | |
6505 | 543 void *arg1 = va_arg(args, void *); |
544 void *arg2 = va_arg(args, void *); | |
6485 | 545 |
6505 | 546 ret_val = ((gboolean (*)(void *, void *, void *))cb)(arg1, arg2, data); |
6485 | 547 |
548 if (return_val != NULL) | |
549 *return_val = GINT_TO_POINTER(ret_val); | |
550 } | |
551 | |
552 void | |
6509 | 553 gaim_marshal_BOOLEAN__POINTER_POINTER_POINTER(GaimCallback cb, va_list args, |
554 void *data, void **return_val) | |
555 { | |
556 gboolean ret_val; | |
557 void *arg1 = va_arg(args, void *); | |
558 void *arg2 = va_arg(args, void *); | |
559 void *arg3 = va_arg(args, void *); | |
560 | |
561 ret_val = ((gboolean (*)(void *, void *, void *, void *))cb)(arg1, arg2, | |
562 arg3, data); | |
563 | |
564 if (return_val != NULL) | |
565 *return_val = GINT_TO_POINTER(ret_val); | |
566 } | |
567 | |
568 void | |
569 gaim_marshal_BOOLEAN__POINTER_POINTER_UINT(GaimCallback cb, | |
570 va_list args, | |
571 void *data, | |
572 void **return_val) | |
573 { | |
574 gboolean ret_val; | |
575 void *arg1 = va_arg(args, void *); | |
576 void *arg2 = va_arg(args, void *); | |
577 guint arg3 = va_arg(args, guint); | |
578 | |
579 ret_val = ((gboolean (*)(void *, void *, guint, void *))cb)( | |
580 arg1, arg2, arg3, data); | |
581 | |
582 if (return_val != NULL) | |
583 *return_val = GINT_TO_POINTER(ret_val); | |
584 } | |
585 | |
586 void | |
6485 | 587 gaim_marshal_BOOLEAN__POINTER_POINTER_POINTER_UINT(GaimCallback cb, |
588 va_list args, | |
589 void *data, | |
590 void **return_val) | |
591 { | |
592 gboolean ret_val; | |
6505 | 593 void *arg1 = va_arg(args, void *); |
594 void *arg2 = va_arg(args, void *); | |
595 void *arg3 = va_arg(args, void *); | |
596 guint arg4 = va_arg(args, guint); | |
6485 | 597 |
598 ret_val = ((gboolean (*)(void *, void *, void *, guint, void *))cb)( | |
6505 | 599 arg1, arg2, arg3, arg4, data); |
6485 | 600 |
601 if (return_val != NULL) | |
602 *return_val = GINT_TO_POINTER(ret_val); | |
603 } | |
604 | |
605 void | |
606 gaim_marshal_BOOLEAN__POINTER_POINTER_POINTER_POINTER(GaimCallback cb, | |
607 va_list args, | |
608 void *data, | |
609 void **return_val) | |
610 { | |
611 gboolean ret_val; | |
6505 | 612 void *arg1 = va_arg(args, void *); |
613 void *arg2 = va_arg(args, void *); | |
614 void *arg3 = va_arg(args, void *); | |
615 void *arg4 = va_arg(args, void *); | |
6485 | 616 |
617 ret_val = ((gboolean (*)(void *, void *, void *, void *, void *))cb)( | |
6505 | 618 arg1, arg2, arg3, arg4, data); |
6485 | 619 |
620 if (return_val != NULL) | |
621 *return_val = GINT_TO_POINTER(ret_val); | |
622 } | |
623 | |
624 void | |
625 gaim_marshal_BOOLEAN__POINTER_POINTER_POINTER_POINTER_POINTER( | |
626 GaimCallback cb, va_list args, void *data, void **return_val) | |
627 { | |
628 gboolean ret_val; | |
6505 | 629 void *arg1 = va_arg(args, void *); |
630 void *arg2 = va_arg(args, void *); | |
631 void *arg3 = va_arg(args, void *); | |
632 void *arg4 = va_arg(args, void *); | |
633 void *arg5 = va_arg(args, void *); | |
6485 | 634 |
635 ret_val = | |
636 ((gboolean (*)(void *, void *, void *, void *, void *, void *))cb)( | |
6505 | 637 arg1, arg2, arg3, arg4, arg5, data); |
6485 | 638 |
639 if (return_val != NULL) | |
640 *return_val = GINT_TO_POINTER(ret_val); | |
641 } |