1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
31 #include "gbsearcharray.h"
32 #include "gvaluecollector.h"
33 #include "gvaluetypes.h"
36 #include "gobjectalias.h"
42 /* pre allocation configurations
44 #define MAX_STACK_VALUES (16)
46 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
48 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
49 static volatile gpointer g_trace_instance_signals = NULL;
50 static volatile gpointer g_trap_instance_signals = NULL;
51 #endif /* G_ENABLE_DEBUG */
54 /* --- typedefs --- */
55 typedef struct _SignalNode SignalNode;
56 typedef struct _SignalKey SignalKey;
57 typedef struct _Emission Emission;
58 typedef struct _Handler Handler;
59 typedef struct _HandlerList HandlerList;
60 typedef struct _HandlerMatch HandlerMatch;
70 /* --- prototypes --- */
71 static inline guint signal_id_lookup (GQuark quark,
73 static void signal_destroy_R (SignalNode *signal_node);
74 static inline HandlerList* handler_list_ensure (guint signal_id,
76 static inline HandlerList* handler_list_lookup (guint signal_id,
78 static inline Handler* handler_new (gboolean after);
79 static void handler_insert (guint signal_id,
82 static Handler* handler_lookup (gpointer instance,
85 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
88 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
90 static HandlerMatch* handlers_find (gpointer instance,
91 GSignalMatchType mask,
97 gboolean one_and_only);
98 static inline void handler_ref (Handler *handler);
99 static inline void handler_unref_R (guint signal_id,
102 static gint handler_lists_cmp (gconstpointer node1,
103 gconstpointer node2);
104 static inline void emission_push (Emission **emission_list_p,
106 static inline void emission_pop (Emission **emission_list_p,
108 static inline Emission* emission_find (Emission *emission_list,
112 static gint class_closures_cmp (gconstpointer node1,
113 gconstpointer node2);
114 static gint signal_key_cmp (gconstpointer node1,
115 gconstpointer node2);
116 static gboolean signal_emit_unlocked_R (SignalNode *node,
119 GValue *return_value,
120 const GValue *instance_and_params);
121 static const gchar * type_debug_name (GType type);
124 /* --- structures --- */
127 GSignalAccumulator func;
135 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
139 /* permanent portion */
145 /* reinitializable portion */
146 guint test_class_offset : 12;
149 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
150 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
151 GBSearchArray *class_closure_bsa;
152 SignalAccumulator *accumulator;
153 GSignalCMarshaller c_marshaller;
154 GHookList *emission_hooks;
156 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
157 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
170 GSignalInvocationHint ihint;
179 Handler *tail_before; /* normal signal handlers are appended here */
180 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
185 gulong sequential_number;
190 guint block_count : 16;
191 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
204 GType instance_type; /* 0 for default closure */
209 /* --- variables --- */
210 static GBSearchArray *g_signal_key_bsa = NULL;
211 static const GBSearchConfig g_signal_key_bconfig = {
214 G_BSEARCH_ARRAY_ALIGN_POWER2,
216 static GBSearchConfig g_signal_hlbsa_bconfig = {
217 sizeof (HandlerList),
221 static GBSearchConfig g_class_closure_bconfig = {
222 sizeof (ClassClosure),
226 static GHashTable *g_handler_list_bsa_ht = NULL;
227 static Emission *g_recursive_emissions = NULL;
228 static Emission *g_restart_emissions = NULL;
229 #ifndef DISABLE_MEM_POOLS
230 static GTrashStack *g_handler_ts = NULL;
232 static gulong g_handler_sequential_number = 1;
233 G_LOCK_DEFINE_STATIC (g_signal_mutex);
234 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
235 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
238 /* --- signal nodes --- */
239 static guint g_n_signal_nodes = 0;
240 static SignalNode **g_signal_nodes = NULL;
242 static inline SignalNode*
243 LOOKUP_SIGNAL_NODE (register guint signal_id)
245 if (signal_id < g_n_signal_nodes)
246 return g_signal_nodes[signal_id];
252 /* --- functions --- */
254 signal_id_lookup (GQuark quark,
257 GType *ifaces, type = itype;
263 /* try looking up signals for this type and its ancestors */
266 SignalKey *signal_key;
269 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
272 return signal_key->signal_id;
274 type = g_type_parent (type);
278 /* no luck, try interfaces it exports */
279 ifaces = g_type_interfaces (itype, &n_ifaces);
282 SignalKey *signal_key;
284 key.itype = ifaces[n_ifaces];
285 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
290 return signal_key->signal_id;
299 class_closures_cmp (gconstpointer node1,
302 const ClassClosure *c1 = node1, *c2 = node2;
304 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
308 handler_lists_cmp (gconstpointer node1,
311 const HandlerList *hlist1 = node1, *hlist2 = node2;
313 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
316 static inline HandlerList*
317 handler_list_ensure (guint signal_id,
320 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
323 key.signal_id = signal_id;
325 key.tail_before = NULL;
326 key.tail_after = NULL;
329 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
330 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
331 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
335 GBSearchArray *o = hlbsa;
337 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
339 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
341 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
344 static inline HandlerList*
345 handler_list_lookup (guint signal_id,
348 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
351 key.signal_id = signal_id;
353 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
357 handler_lookup (gpointer instance,
361 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
367 for (i = 0; i < hlbsa->n_nodes; i++)
369 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
372 for (handler = hlist->handlers; handler; handler = handler->next)
373 if (handler->sequential_number == handler_id)
376 *signal_id_p = hlist->signal_id;
386 static inline HandlerMatch*
387 handler_match_prepend (HandlerMatch *list,
393 /* yeah, we could use our own memchunk here, introducing yet more
394 * rarely used cached nodes and extra allocation overhead.
395 * instead, we use GList* nodes, since they are exactly the size
396 * we need and are already cached. g_signal_init() asserts this.
398 node = g_slice_new (HandlerMatch);
399 node->handler = handler;
401 node->signal_id = signal_id;
402 handler_ref (handler);
406 static inline HandlerMatch*
407 handler_match_free1_R (HandlerMatch *node,
410 HandlerMatch *next = node->next;
412 handler_unref_R (node->signal_id, instance, node->handler);
413 g_slice_free (HandlerMatch, node);
419 handlers_find (gpointer instance,
420 GSignalMatchType mask,
426 gboolean one_and_only)
428 HandlerMatch *mlist = NULL;
430 if (mask & G_SIGNAL_MATCH_ID)
432 HandlerList *hlist = handler_list_lookup (signal_id, instance);
434 SignalNode *node = NULL;
436 if (mask & G_SIGNAL_MATCH_FUNC)
438 node = LOOKUP_SIGNAL_NODE (signal_id);
439 if (!node || !node->c_marshaller)
444 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
445 if (handler->sequential_number &&
446 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
447 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
448 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
449 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
450 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
451 handler->closure->meta_marshal == 0 &&
452 ((GCClosure*) handler->closure)->callback == func)))
454 mlist = handler_match_prepend (mlist, handler, signal_id);
461 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
468 for (i = 0; i < hlbsa->n_nodes; i++)
470 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
471 SignalNode *node = NULL;
474 if (!(mask & G_SIGNAL_MATCH_FUNC))
476 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
477 if (!node->c_marshaller)
481 for (handler = hlist->handlers; handler; handler = handler->next)
482 if (handler->sequential_number &&
483 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
484 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
485 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
486 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
487 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
488 handler->closure->meta_marshal == 0 &&
489 ((GCClosure*) handler->closure)->callback == func)))
491 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
502 static inline Handler*
503 handler_new (gboolean after)
505 Handler *handler = g_slice_new (Handler);
506 #ifndef G_DISABLE_CHECKS
507 if (g_handler_sequential_number < 1)
508 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
511 handler->sequential_number = g_handler_sequential_number++;
512 handler->prev = NULL;
513 handler->next = NULL;
515 handler->ref_count = 1;
516 handler->block_count = 0;
517 handler->after = after != FALSE;
518 handler->closure = NULL;
524 handler_ref (Handler *handler)
526 g_return_if_fail (handler->ref_count > 0);
528 g_atomic_int_inc (&handler->ref_count);
532 handler_unref_R (guint signal_id,
538 g_return_if_fail (handler->ref_count > 0);
540 is_zero = g_atomic_int_dec_and_test (&handler->ref_count);
542 if (G_UNLIKELY (is_zero))
544 HandlerList *hlist = NULL;
547 handler->next->prev = handler->prev;
548 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
549 handler->prev->next = handler->next;
552 hlist = handler_list_lookup (signal_id, instance);
553 hlist->handlers = handler->next;
558 /* check if we are removing the handler pointed to by tail_before */
559 if (!handler->after && (!handler->next || handler->next->after))
562 hlist = handler_list_lookup (signal_id, instance);
565 g_assert (hlist->tail_before == handler); /* paranoid */
566 hlist->tail_before = handler->prev;
570 /* check if we are removing the handler pointed to by tail_after */
574 hlist = handler_list_lookup (signal_id, instance);
577 g_assert (hlist->tail_after == handler); /* paranoid */
578 hlist->tail_after = handler->prev;
584 g_closure_unref (handler->closure);
586 g_slice_free (Handler, handler);
591 handler_insert (guint signal_id,
597 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
599 hlist = handler_list_ensure (signal_id, instance);
600 if (!hlist->handlers)
602 hlist->handlers = handler;
604 hlist->tail_before = handler;
606 else if (handler->after)
608 handler->prev = hlist->tail_after;
609 hlist->tail_after->next = handler;
613 if (hlist->tail_before)
615 handler->next = hlist->tail_before->next;
617 handler->next->prev = handler;
618 handler->prev = hlist->tail_before;
619 hlist->tail_before->next = handler;
621 else /* insert !after handler into a list of only after handlers */
623 handler->next = hlist->handlers;
625 handler->next->prev = handler;
626 hlist->handlers = handler;
628 hlist->tail_before = handler;
632 hlist->tail_after = handler;
636 emission_push (Emission **emission_list_p,
639 emission->next = *emission_list_p;
640 *emission_list_p = emission;
644 emission_pop (Emission **emission_list_p,
647 Emission *node, *last = NULL;
649 for (node = *emission_list_p; node; last = node, node = last->next)
650 if (node == emission)
653 last->next = node->next;
655 *emission_list_p = node->next;
658 g_assert_not_reached ();
661 static inline Emission*
662 emission_find (Emission *emission_list,
669 for (emission = emission_list; emission; emission = emission->next)
670 if (emission->instance == instance &&
671 emission->ihint.signal_id == signal_id &&
672 emission->ihint.detail == detail)
677 static inline Emission*
678 emission_find_innermost (gpointer instance)
680 Emission *emission, *s = NULL, *c = NULL;
682 for (emission = g_restart_emissions; emission; emission = emission->next)
683 if (emission->instance == instance)
688 for (emission = g_recursive_emissions; emission; emission = emission->next)
689 if (emission->instance == instance)
699 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
703 signal_key_cmp (gconstpointer node1,
706 const SignalKey *key1 = node1, *key2 = node2;
708 if (key1->itype == key2->itype)
709 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
711 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
718 if (!g_n_signal_nodes)
720 /* handler_id_node_prepend() requires this */
721 g_assert (sizeof (GList) == sizeof (HandlerMatch));
723 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
724 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
725 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
727 /* invalid (0) signal_id */
728 g_n_signal_nodes = 1;
729 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
730 g_signal_nodes[0] = NULL;
736 _g_signals_destroy (GType itype)
741 for (i = 1; i < g_n_signal_nodes; i++)
743 SignalNode *node = g_signal_nodes[i];
745 if (node->itype == itype)
748 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
750 type_debug_name (node->itype));
752 signal_destroy_R (node);
759 g_signal_stop_emission (gpointer instance,
765 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
766 g_return_if_fail (signal_id > 0);
769 node = LOOKUP_SIGNAL_NODE (signal_id);
770 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
772 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
776 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
778 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
779 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
783 if (emission->state == EMISSION_HOOK)
784 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
785 node->name, instance);
786 else if (emission->state == EMISSION_RUN)
787 emission->state = EMISSION_STOP;
790 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
791 node->name, instance);
794 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
799 signal_finalize_hook (GHookList *hook_list,
802 GDestroyNotify destroy = hook->destroy;
806 hook->destroy = NULL;
808 destroy (hook->data);
814 g_signal_add_emission_hook (guint signal_id,
816 GSignalEmissionHook hook_func,
818 GDestroyNotify data_destroy)
820 static gulong seq_hook_id = 1;
823 SignalHook *signal_hook;
825 g_return_val_if_fail (signal_id > 0, 0);
826 g_return_val_if_fail (hook_func != NULL, 0);
829 node = LOOKUP_SIGNAL_NODE (signal_id);
830 if (!node || node->destroyed)
832 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
836 if (node->flags & G_SIGNAL_NO_HOOKS)
838 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
842 if (detail && !(node->flags & G_SIGNAL_DETAILED))
844 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
848 if (!node->emission_hooks)
850 node->emission_hooks = g_new (GHookList, 1);
851 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
852 node->emission_hooks->finalize_hook = signal_finalize_hook;
854 hook = g_hook_alloc (node->emission_hooks);
855 hook->data = hook_data;
856 hook->func = (gpointer) hook_func;
857 hook->destroy = data_destroy;
858 signal_hook = SIGNAL_HOOK (hook);
859 signal_hook->detail = detail;
860 node->emission_hooks->seq_id = seq_hook_id;
861 g_hook_append (node->emission_hooks, hook);
862 seq_hook_id = node->emission_hooks->seq_id;
865 return hook->hook_id;
869 g_signal_remove_emission_hook (guint signal_id,
874 g_return_if_fail (signal_id > 0);
875 g_return_if_fail (hook_id > 0);
878 node = LOOKUP_SIGNAL_NODE (signal_id);
879 if (!node || node->destroyed)
880 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
881 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
882 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
887 signal_parse_name (const gchar *name,
890 gboolean force_quark)
892 const gchar *colon = strchr (name, ':');
897 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
898 if (signal_id && detail_p)
901 else if (colon[1] == ':')
904 guint l = colon - name;
908 memcpy (buffer, name, l);
910 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
914 gchar *signal = g_new (gchar, l + 1);
916 memcpy (signal, name, l);
918 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
922 if (signal_id && detail_p)
923 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
931 g_signal_parse_name (const gchar *detailed_signal,
935 gboolean force_detail_quark)
941 g_return_val_if_fail (detailed_signal != NULL, FALSE);
942 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
945 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
948 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
949 if (!node || node->destroyed ||
950 (detail && !(node->flags & G_SIGNAL_DETAILED)))
954 *signal_id_p = signal_id;
962 g_signal_stop_emission_by_name (gpointer instance,
963 const gchar *detailed_signal)
969 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
970 g_return_if_fail (detailed_signal != NULL);
973 itype = G_TYPE_FROM_INSTANCE (instance);
974 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
977 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
979 if (detail && !(node->flags & G_SIGNAL_DETAILED))
980 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
981 else if (!g_type_is_a (itype, node->itype))
982 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
985 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
986 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
990 if (emission->state == EMISSION_HOOK)
991 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
992 node->name, instance);
993 else if (emission->state == EMISSION_RUN)
994 emission->state = EMISSION_STOP;
997 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
998 node->name, instance);
1002 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1007 g_signal_lookup (const gchar *name,
1011 g_return_val_if_fail (name != NULL, 0);
1012 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1015 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1019 /* give elaborate warnings */
1020 if (!g_type_name (itype))
1021 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1023 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1024 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1025 name, g_type_name (itype));
1026 else if (!g_type_class_peek (itype))
1027 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1028 name, g_type_name (itype));
1035 g_signal_list_ids (GType itype,
1043 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1044 g_return_val_if_fail (n_ids != NULL, NULL);
1047 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1048 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1049 result = g_array_new (FALSE, FALSE, sizeof (guint));
1051 for (i = 0; i < n_nodes; i++)
1052 if (keys[i].itype == itype)
1054 const gchar *name = g_quark_to_string (keys[i].quark);
1056 /* Signal names with "_" in them are aliases to the same
1057 * name with "-" instead of "_".
1059 if (!strchr (name, '_'))
1060 g_array_append_val (result, keys[i].signal_id);
1062 *n_ids = result->len;
1066 /* give elaborate warnings */
1067 if (!g_type_name (itype))
1068 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1070 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1071 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1072 g_type_name (itype));
1073 else if (!g_type_class_peek (itype))
1074 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1075 g_type_name (itype));
1078 return (guint*) g_array_free (result, FALSE);
1081 G_CONST_RETURN gchar*
1082 g_signal_name (guint signal_id)
1088 node = LOOKUP_SIGNAL_NODE (signal_id);
1089 name = node ? node->name : NULL;
1092 return (char*) name;
1096 g_signal_query (guint signal_id,
1097 GSignalQuery *query)
1101 g_return_if_fail (query != NULL);
1104 node = LOOKUP_SIGNAL_NODE (signal_id);
1105 if (!node || node->destroyed)
1106 query->signal_id = 0;
1109 query->signal_id = node->signal_id;
1110 query->signal_name = node->name;
1111 query->itype = node->itype;
1112 query->signal_flags = node->flags;
1113 query->return_type = node->return_type;
1114 query->n_params = node->n_params;
1115 query->param_types = node->param_types;
1121 g_signal_new (const gchar *signal_name,
1123 GSignalFlags signal_flags,
1125 GSignalAccumulator accumulator,
1127 GSignalCMarshaller c_marshaller,
1135 g_return_val_if_fail (signal_name != NULL, 0);
1137 va_start (args, n_params);
1139 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1140 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1141 accumulator, accu_data, c_marshaller,
1142 return_type, n_params, args);
1146 /* optimize NOP emissions with NULL class handlers */
1147 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1148 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1153 node = LOOKUP_SIGNAL_NODE (signal_id);
1154 node->test_class_offset = class_offset;
1161 static inline ClassClosure*
1162 signal_find_class_closure (SignalNode *node,
1165 GBSearchArray *bsa = node->class_closure_bsa;
1172 /* cc->instance_type is 0 for default closure */
1174 key.instance_type = itype;
1175 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1176 while (!cc && key.instance_type)
1178 key.instance_type = g_type_parent (key.instance_type);
1179 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1187 static inline GClosure*
1188 signal_lookup_closure (SignalNode *node,
1189 GTypeInstance *instance)
1193 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1194 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1196 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1197 return cc ? cc->closure : NULL;
1201 signal_add_class_closure (SignalNode *node,
1207 /* can't optimize NOP emissions with overridden class closures */
1208 node->test_class_offset = 0;
1210 if (!node->class_closure_bsa)
1211 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1212 key.instance_type = itype;
1213 key.closure = g_closure_ref (closure);
1214 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1215 &g_class_closure_bconfig,
1217 g_closure_sink (closure);
1218 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1219 g_closure_set_marshal (closure, node->c_marshaller);
1223 g_signal_newv (const gchar *signal_name,
1225 GSignalFlags signal_flags,
1226 GClosure *class_closure,
1227 GSignalAccumulator accumulator,
1229 GSignalCMarshaller c_marshaller,
1238 g_return_val_if_fail (signal_name != NULL, 0);
1239 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1241 g_return_val_if_fail (param_types != NULL, 0);
1242 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1243 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1244 g_return_val_if_fail (accumulator == NULL, 0);
1246 g_return_val_if_fail (accu_data == NULL, 0);
1248 name = g_strdup (signal_name);
1249 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1253 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1254 node = LOOKUP_SIGNAL_NODE (signal_id);
1255 if (node && !node->destroyed)
1257 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1259 type_debug_name (node->itype),
1260 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1265 if (node && node->itype != itype)
1267 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1269 type_debug_name (itype),
1270 type_debug_name (node->itype));
1275 for (i = 0; i < n_params; i++)
1276 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1278 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1279 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1284 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1286 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1287 type_debug_name (return_type), type_debug_name (itype), name);
1292 if (return_type != G_TYPE_NONE &&
1293 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1295 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1296 type_debug_name (itype), name, type_debug_name (return_type));
1302 /* setup permanent portion of signal node */
1307 signal_id = g_n_signal_nodes++;
1308 node = g_new (SignalNode, 1);
1309 node->signal_id = signal_id;
1310 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1311 g_signal_nodes[signal_id] = node;
1312 node->itype = itype;
1315 key.quark = g_quark_from_string (node->name);
1316 key.signal_id = signal_id;
1317 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1318 g_strdelimit (name, "_", '-');
1319 node->name = g_intern_string (name);
1320 key.quark = g_quark_from_string (name);
1321 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1323 node->destroyed = FALSE;
1324 node->test_class_offset = 0;
1326 /* setup reinitializable portion */
1327 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1328 node->n_params = n_params;
1329 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1330 node->return_type = return_type;
1331 node->class_closure_bsa = NULL;
1334 node->accumulator = g_new (SignalAccumulator, 1);
1335 node->accumulator->func = accumulator;
1336 node->accumulator->data = accu_data;
1339 node->accumulator = NULL;
1340 node->c_marshaller = c_marshaller;
1341 node->emission_hooks = NULL;
1343 signal_add_class_closure (node, 0, class_closure);
1344 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1346 /* optimize NOP emissions */
1347 node->test_class_offset = TEST_CLASS_MAGIC;
1357 g_signal_new_valist (const gchar *signal_name,
1359 GSignalFlags signal_flags,
1360 GClosure *class_closure,
1361 GSignalAccumulator accumulator,
1363 GSignalCMarshaller c_marshaller,
1374 param_types = g_new (GType, n_params);
1376 for (i = 0; i < n_params; i++)
1377 param_types[i] = va_arg (args, GType);
1382 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1383 class_closure, accumulator, accu_data, c_marshaller,
1384 return_type, n_params, param_types);
1385 g_free (param_types);
1391 signal_destroy_R (SignalNode *signal_node)
1393 SignalNode node = *signal_node;
1395 signal_node->destroyed = TRUE;
1397 /* reentrancy caution, zero out real contents first */
1398 signal_node->test_class_offset = 0;
1399 signal_node->n_params = 0;
1400 signal_node->param_types = NULL;
1401 signal_node->return_type = 0;
1402 signal_node->class_closure_bsa = NULL;
1403 signal_node->accumulator = NULL;
1404 signal_node->c_marshaller = NULL;
1405 signal_node->emission_hooks = NULL;
1407 #ifdef G_ENABLE_DEBUG
1408 /* check current emissions */
1412 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1413 emission; emission = emission->next)
1414 if (emission->ihint.signal_id == node.signal_id)
1415 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1416 node.name, emission->instance);
1420 /* free contents that need to
1423 g_free (node.param_types);
1424 if (node.class_closure_bsa)
1428 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1430 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1432 g_closure_unref (cc->closure);
1434 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1436 g_free (node.accumulator);
1437 if (node.emission_hooks)
1439 g_hook_list_clear (node.emission_hooks);
1440 g_free (node.emission_hooks);
1446 g_signal_override_class_closure (guint signal_id,
1447 GType instance_type,
1448 GClosure *class_closure)
1452 g_return_if_fail (signal_id > 0);
1453 g_return_if_fail (class_closure != NULL);
1456 node = LOOKUP_SIGNAL_NODE (signal_id);
1457 if (!g_type_is_a (instance_type, node->itype))
1458 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1461 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1463 if (cc && cc->instance_type == instance_type)
1464 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1466 signal_add_class_closure (node, instance_type, class_closure);
1472 g_signal_chain_from_overridden (const GValue *instance_and_params,
1473 GValue *return_value)
1475 GType chain_type = 0, restore_type = 0;
1476 Emission *emission = NULL;
1477 GClosure *closure = NULL;
1481 g_return_if_fail (instance_and_params != NULL);
1482 instance = g_value_peek_pointer (instance_and_params);
1483 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1486 emission = emission_find_innermost (instance);
1489 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1491 g_assert (node != NULL); /* paranoid */
1493 /* we should probably do the same parameter checks as g_signal_emit() here.
1495 if (emission->chain_type != G_TYPE_NONE)
1497 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1499 g_assert (cc != NULL); /* closure currently in call stack */
1501 n_params = node->n_params;
1502 restore_type = cc->instance_type;
1503 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1504 if (cc && cc->instance_type != restore_type)
1506 closure = cc->closure;
1507 chain_type = cc->instance_type;
1511 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1514 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1517 emission->chain_type = chain_type;
1519 g_closure_invoke (closure,
1522 instance_and_params,
1525 emission->chain_type = restore_type;
1530 GSignalInvocationHint*
1531 g_signal_get_invocation_hint (gpointer instance)
1533 Emission *emission = NULL;
1535 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1538 emission = emission_find_innermost (instance);
1541 return emission ? &emission->ihint : NULL;
1545 g_signal_connect_closure_by_id (gpointer instance,
1552 gulong handler_seq_no = 0;
1554 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1555 g_return_val_if_fail (signal_id > 0, 0);
1556 g_return_val_if_fail (closure != NULL, 0);
1559 node = LOOKUP_SIGNAL_NODE (signal_id);
1562 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1563 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1564 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1565 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1568 Handler *handler = handler_new (after);
1570 handler_seq_no = handler->sequential_number;
1571 handler->detail = detail;
1572 handler->closure = g_closure_ref (closure);
1573 g_closure_sink (closure);
1574 handler_insert (signal_id, instance, handler);
1575 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1576 g_closure_set_marshal (closure, node->c_marshaller);
1580 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1583 return handler_seq_no;
1587 g_signal_connect_closure (gpointer instance,
1588 const gchar *detailed_signal,
1593 gulong handler_seq_no = 0;
1597 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1598 g_return_val_if_fail (detailed_signal != NULL, 0);
1599 g_return_val_if_fail (closure != NULL, 0);
1602 itype = G_TYPE_FROM_INSTANCE (instance);
1603 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1606 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1608 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1609 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1610 else if (!g_type_is_a (itype, node->itype))
1611 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1614 Handler *handler = handler_new (after);
1616 handler_seq_no = handler->sequential_number;
1617 handler->detail = detail;
1618 handler->closure = g_closure_ref (closure);
1619 g_closure_sink (closure);
1620 handler_insert (signal_id, instance, handler);
1621 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1622 g_closure_set_marshal (handler->closure, node->c_marshaller);
1626 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1629 return handler_seq_no;
1633 g_signal_connect_data (gpointer instance,
1634 const gchar *detailed_signal,
1635 GCallback c_handler,
1637 GClosureNotify destroy_data,
1638 GConnectFlags connect_flags)
1641 gulong handler_seq_no = 0;
1644 gboolean swapped, after;
1646 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1647 g_return_val_if_fail (detailed_signal != NULL, 0);
1648 g_return_val_if_fail (c_handler != NULL, 0);
1650 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1651 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1654 itype = G_TYPE_FROM_INSTANCE (instance);
1655 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1658 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1660 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1661 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1662 else if (!g_type_is_a (itype, node->itype))
1663 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1666 Handler *handler = handler_new (after);
1668 handler_seq_no = handler->sequential_number;
1669 handler->detail = detail;
1670 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1671 g_closure_sink (handler->closure);
1672 handler_insert (signal_id, instance, handler);
1673 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1674 g_closure_set_marshal (handler->closure, node->c_marshaller);
1678 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1681 return handler_seq_no;
1685 g_signal_handler_block (gpointer instance,
1690 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1691 g_return_if_fail (handler_id > 0);
1694 handler = handler_lookup (instance, handler_id, NULL);
1697 #ifndef G_DISABLE_CHECKS
1698 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1699 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1701 handler->block_count += 1;
1704 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1709 g_signal_handler_unblock (gpointer instance,
1714 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1715 g_return_if_fail (handler_id > 0);
1718 handler = handler_lookup (instance, handler_id, NULL);
1721 if (handler->block_count)
1722 handler->block_count -= 1;
1724 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1727 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1732 g_signal_handler_disconnect (gpointer instance,
1738 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1739 g_return_if_fail (handler_id > 0);
1742 handler = handler_lookup (instance, handler_id, &signal_id);
1745 handler->sequential_number = 0;
1746 handler->block_count = 1;
1747 handler_unref_R (signal_id, instance, handler);
1750 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1755 g_signal_handler_is_connected (gpointer instance,
1761 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1764 handler = handler_lookup (instance, handler_id, NULL);
1765 connected = handler != NULL;
1772 g_signal_handlers_destroy (gpointer instance)
1774 GBSearchArray *hlbsa;
1776 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1779 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1784 /* reentrancy caution, delete instance trace first */
1785 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1787 for (i = 0; i < hlbsa->n_nodes; i++)
1789 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1790 Handler *handler = hlist->handlers;
1794 Handler *tmp = handler;
1796 handler = tmp->next;
1797 tmp->block_count = 1;
1798 /* cruel unlink, this works because _all_ handlers vanish */
1801 if (tmp->sequential_number)
1803 tmp->sequential_number = 0;
1804 handler_unref_R (0, NULL, tmp);
1808 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
1814 g_signal_handler_find (gpointer instance,
1815 GSignalMatchType mask,
1822 gulong handler_seq_no = 0;
1824 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1825 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1827 if (mask & G_SIGNAL_MATCH_MASK)
1829 HandlerMatch *mlist;
1832 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1835 handler_seq_no = mlist->handler->sequential_number;
1836 handler_match_free1_R (mlist, instance);
1841 return handler_seq_no;
1845 signal_handlers_foreach_matched_R (gpointer instance,
1846 GSignalMatchType mask,
1852 void (*callback) (gpointer instance,
1853 gulong handler_seq_no))
1855 HandlerMatch *mlist;
1856 guint n_handlers = 0;
1858 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1862 if (mlist->handler->sequential_number)
1865 callback (instance, mlist->handler->sequential_number);
1868 mlist = handler_match_free1_R (mlist, instance);
1875 g_signal_handlers_block_matched (gpointer instance,
1876 GSignalMatchType mask,
1883 guint n_handlers = 0;
1885 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1886 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1888 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1891 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1892 closure, func, data,
1893 g_signal_handler_block);
1901 g_signal_handlers_unblock_matched (gpointer instance,
1902 GSignalMatchType mask,
1909 guint n_handlers = 0;
1911 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1912 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1914 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1917 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1918 closure, func, data,
1919 g_signal_handler_unblock);
1927 g_signal_handlers_disconnect_matched (gpointer instance,
1928 GSignalMatchType mask,
1935 guint n_handlers = 0;
1937 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1938 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1940 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1943 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1944 closure, func, data,
1945 g_signal_handler_disconnect);
1953 g_signal_has_handler_pending (gpointer instance,
1956 gboolean may_be_blocked)
1958 HandlerMatch *mlist;
1959 gboolean has_pending;
1961 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1962 g_return_val_if_fail (signal_id > 0, FALSE);
1967 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1969 if (!(node->flags & G_SIGNAL_DETAILED))
1971 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1976 mlist = handlers_find (instance,
1977 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1978 signal_id, detail, NULL, NULL, NULL, TRUE);
1982 handler_match_free1_R (mlist, instance);
1985 has_pending = FALSE;
1991 static inline gboolean
1992 signal_check_skip_emission (SignalNode *node,
1998 /* are we able to check for NULL class handlers? */
1999 if (!node->test_class_offset)
2002 /* are there emission hooks pending? */
2003 if (node->emission_hooks && node->emission_hooks->hooks)
2006 /* is there a non-NULL class handler? */
2007 if (node->test_class_offset != TEST_CLASS_MAGIC)
2009 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2011 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2015 /* are signals being debugged? */
2016 #ifdef G_ENABLE_DEBUG
2017 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2019 #endif /* G_ENABLE_DEBUG */
2021 /* is this a no-recurse signal already in emission? */
2022 if (node->flags & G_SIGNAL_NO_RECURSE &&
2023 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2026 /* do we have pending handlers? */
2027 hlist = handler_list_lookup (node->signal_id, instance);
2028 if (hlist && hlist->handlers)
2031 /* none of the above, no emission required */
2036 g_signal_emitv (const GValue *instance_and_params,
2039 GValue *return_value)
2043 #ifdef G_ENABLE_DEBUG
2044 const GValue *param_values;
2048 g_return_if_fail (instance_and_params != NULL);
2049 instance = g_value_peek_pointer (instance_and_params);
2050 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2051 g_return_if_fail (signal_id > 0);
2053 #ifdef G_ENABLE_DEBUG
2054 param_values = instance_and_params + 1;
2058 node = LOOKUP_SIGNAL_NODE (signal_id);
2059 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2061 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2065 #ifdef G_ENABLE_DEBUG
2066 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2068 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2072 for (i = 0; i < node->n_params; i++)
2073 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2075 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2077 type_debug_name (node->param_types[i]),
2080 G_VALUE_TYPE_NAME (param_values + i));
2084 if (node->return_type != G_TYPE_NONE)
2088 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2090 type_debug_name (node->return_type),
2095 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2097 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2099 type_debug_name (node->return_type),
2101 G_VALUE_TYPE_NAME (return_value));
2107 return_value = NULL;
2108 #endif /* G_ENABLE_DEBUG */
2110 /* optimize NOP emissions */
2111 if (signal_check_skip_emission (node, instance, detail))
2113 /* nothing to do to emit this signal */
2115 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2120 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2124 g_signal_emit_valist (gpointer instance,
2129 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2130 GType signal_return_type;
2131 GValue *param_values;
2135 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2136 g_return_if_fail (signal_id > 0);
2139 node = LOOKUP_SIGNAL_NODE (signal_id);
2140 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2142 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2146 #ifndef G_DISABLE_CHECKS
2147 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2149 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2153 #endif /* !G_DISABLE_CHECKS */
2155 /* optimize NOP emissions */
2156 if (signal_check_skip_emission (node, instance, detail))
2158 /* nothing to do to emit this signal */
2160 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2164 n_params = node->n_params;
2165 signal_return_type = node->return_type;
2166 if (node->n_params < MAX_STACK_VALUES)
2167 instance_and_params = stack_values;
2170 free_me = g_new (GValue, node->n_params + 1);
2171 instance_and_params = free_me;
2173 param_values = instance_and_params + 1;
2174 for (i = 0; i < node->n_params; i++)
2177 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2178 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2180 param_values[i].g_type = 0;
2182 g_value_init (param_values + i, ptype);
2183 G_VALUE_COLLECT (param_values + i,
2185 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2189 g_warning ("%s: %s", G_STRLOC, error);
2192 /* we purposely leak the value here, it might not be
2193 * in a sane state if an error condition occoured
2196 g_value_unset (param_values + i);
2204 instance_and_params->g_type = 0;
2205 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2206 g_value_set_instance (instance_and_params, instance);
2207 if (signal_return_type == G_TYPE_NONE)
2208 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2211 GValue return_value = { 0, };
2212 gchar *error = NULL;
2213 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2214 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2216 g_value_init (&return_value, rtype);
2218 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2220 G_VALUE_LCOPY (&return_value,
2222 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2225 g_value_unset (&return_value);
2228 g_warning ("%s: %s", G_STRLOC, error);
2231 /* we purposely leak the value here, it might not be
2232 * in a sane state if an error condition occured
2236 for (i = 0; i < n_params; i++)
2237 g_value_unset (param_values + i);
2238 g_value_unset (instance_and_params);
2244 g_signal_emit (gpointer instance,
2251 va_start (var_args, detail);
2252 g_signal_emit_valist (instance, signal_id, detail, var_args);
2257 g_signal_emit_by_name (gpointer instance,
2258 const gchar *detailed_signal,
2264 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2265 g_return_if_fail (detailed_signal != NULL);
2268 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2275 va_start (var_args, detailed_signal);
2276 g_signal_emit_valist (instance, signal_id, detail, var_args);
2280 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2283 static inline gboolean
2284 accumulate (GSignalInvocationHint *ihint,
2285 GValue *return_accu,
2286 GValue *handler_return,
2287 SignalAccumulator *accumulator)
2289 gboolean continue_emission;
2294 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2295 g_value_reset (handler_return);
2297 return continue_emission;
2301 signal_emit_unlocked_R (SignalNode *node,
2304 GValue *emission_return,
2305 const GValue *instance_and_params)
2307 SignalAccumulator *accumulator;
2309 GClosure *class_closure;
2311 Handler *handler_list = NULL;
2312 GValue *return_accu, accu = { 0, };
2314 gulong max_sequential_handler_number;
2315 gboolean return_value_altered = FALSE;
2317 #ifdef G_ENABLE_DEBUG
2318 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2320 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2321 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2324 if (g_trap_instance_signals == instance)
2327 #endif /* G_ENABLE_DEBUG */
2330 signal_id = node->signal_id;
2331 if (node->flags & G_SIGNAL_NO_RECURSE)
2333 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2337 node->state = EMISSION_RESTART;
2339 return return_value_altered;
2342 accumulator = node->accumulator;
2346 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2347 return_accu = &accu;
2351 return_accu = emission_return;
2352 emission.instance = instance;
2353 emission.ihint.signal_id = node->signal_id;
2354 emission.ihint.detail = detail;
2355 emission.ihint.run_type = 0;
2357 emission.chain_type = G_TYPE_NONE;
2358 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2359 class_closure = signal_lookup_closure (node, instance);
2364 handler_unref_R (signal_id, instance, handler_list);
2365 max_sequential_handler_number = g_handler_sequential_number;
2366 hlist = handler_list_lookup (signal_id, instance);
2367 handler_list = hlist ? hlist->handlers : NULL;
2369 handler_ref (handler_list);
2371 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2373 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2375 emission.state = EMISSION_RUN;
2377 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2379 g_closure_invoke (class_closure,
2382 instance_and_params,
2384 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2385 emission.state == EMISSION_RUN)
2386 emission.state = EMISSION_STOP;
2388 emission.chain_type = G_TYPE_NONE;
2389 return_value_altered = TRUE;
2391 if (emission.state == EMISSION_STOP)
2393 else if (emission.state == EMISSION_RESTART)
2397 if (node->emission_hooks)
2399 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2402 emission.state = EMISSION_HOOK;
2403 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2406 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2408 if (!signal_hook->detail || signal_hook->detail == detail)
2410 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2412 was_in_call = G_HOOK_IN_CALL (hook);
2413 hook->flags |= G_HOOK_FLAG_IN_CALL;
2415 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2418 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2420 g_hook_destroy_link (node->emission_hooks, hook);
2422 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2425 if (emission.state == EMISSION_RESTART)
2431 Handler *handler = handler_list;
2433 emission.state = EMISSION_RUN;
2434 handler_ref (handler);
2441 handler_unref_R (signal_id, instance, handler_list);
2442 handler_list = handler;
2445 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2446 handler->sequential_number < max_sequential_handler_number)
2449 g_closure_invoke (handler->closure,
2452 instance_and_params,
2454 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2455 emission.state == EMISSION_RUN)
2456 emission.state = EMISSION_STOP;
2458 return_value_altered = TRUE;
2460 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2463 tmp = handler->next;
2467 handler_unref_R (signal_id, instance, handler_list);
2468 handler_list = handler;
2473 if (emission.state == EMISSION_STOP)
2475 else if (emission.state == EMISSION_RESTART)
2479 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2481 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2483 emission.state = EMISSION_RUN;
2485 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2487 g_closure_invoke (class_closure,
2490 instance_and_params,
2492 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2493 emission.state == EMISSION_RUN)
2494 emission.state = EMISSION_STOP;
2496 emission.chain_type = G_TYPE_NONE;
2497 return_value_altered = TRUE;
2499 if (emission.state == EMISSION_STOP)
2501 else if (emission.state == EMISSION_RESTART)
2507 Handler *handler = handler_list;
2509 emission.state = EMISSION_RUN;
2510 handler_ref (handler);
2515 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2516 handler->sequential_number < max_sequential_handler_number)
2519 g_closure_invoke (handler->closure,
2522 instance_and_params,
2524 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2525 emission.state == EMISSION_RUN)
2526 emission.state = EMISSION_STOP;
2528 return_value_altered = TRUE;
2530 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2533 tmp = handler->next;
2537 handler_unref_R (signal_id, instance, handler);
2542 if (emission.state == EMISSION_STOP)
2544 else if (emission.state == EMISSION_RESTART)
2550 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2552 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2554 gboolean need_unset = FALSE;
2556 emission.state = EMISSION_STOP;
2558 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2560 if (node->return_type != G_TYPE_NONE && !accumulator)
2562 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2565 g_closure_invoke (class_closure,
2566 node->return_type != G_TYPE_NONE ? &accu : NULL,
2568 instance_and_params,
2571 g_value_unset (&accu);
2573 emission.chain_type = G_TYPE_NONE;
2575 if (emission.state == EMISSION_RESTART)
2580 handler_unref_R (signal_id, instance, handler_list);
2582 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2585 g_value_unset (&accu);
2587 return return_value_altered;
2591 type_debug_name (GType type)
2595 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2596 return name ? name : "<unknown>";
2603 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
2604 GValue *return_accu,
2605 const GValue *handler_return,
2608 gboolean continue_emission;
2609 gboolean signal_handled;
2611 signal_handled = g_value_get_boolean (handler_return);
2612 g_value_set_boolean (return_accu, signal_handled);
2613 continue_emission = !signal_handled;
2615 return continue_emission;
2618 /* --- compile standard marshallers --- */
2619 #include "gobject.h"
2621 #include "gmarshal.c"
2623 #define __G_SIGNAL_C__
2624 #include "gobjectaliasdef.c"