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)
45 #define HANDLER_PRE_ALLOC (48)
47 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
49 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
50 static volatile gpointer g_trace_instance_signals = NULL;
51 static volatile gpointer g_trap_instance_signals = NULL;
52 #endif /* G_ENABLE_DEBUG */
55 /* --- generic allocation --- */
56 /* we special case allocations generically by replacing
57 * these functions with more speed/memory aware variants
59 #ifndef DISABLE_MEM_POOLS
60 static inline gpointer
61 g_generic_node_alloc (GTrashStack **trash_stack_p,
63 guint nodes_pre_alloc)
65 gpointer node = g_trash_stack_pop (trash_stack_p);
71 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
72 block = g_malloc (sizeof_node * nodes_pre_alloc);
73 while (--nodes_pre_alloc)
75 g_trash_stack_push (trash_stack_p, block);
83 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
84 #else /* !DISABLE_MEM_POOLS */
85 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
86 #define g_generic_node_free(t,node) g_free (node)
87 #endif /* !DISABLE_MEM_POOLS */
90 /* --- typedefs --- */
91 typedef struct _SignalNode SignalNode;
92 typedef struct _SignalKey SignalKey;
93 typedef struct _Emission Emission;
94 typedef struct _Handler Handler;
95 typedef struct _HandlerList HandlerList;
96 typedef struct _HandlerMatch HandlerMatch;
106 /* --- prototypes --- */
107 static inline guint signal_id_lookup (GQuark quark,
109 static void signal_destroy_R (SignalNode *signal_node);
110 static inline HandlerList* handler_list_ensure (guint signal_id,
112 static inline HandlerList* handler_list_lookup (guint signal_id,
114 static inline Handler* handler_new (gboolean after);
115 static void handler_insert (guint signal_id,
118 static Handler* handler_lookup (gpointer instance,
121 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
124 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
126 static HandlerMatch* handlers_find (gpointer instance,
127 GSignalMatchType mask,
133 gboolean one_and_only);
134 static inline void handler_ref (Handler *handler);
135 static inline void handler_unref_R (guint signal_id,
138 static gint handler_lists_cmp (gconstpointer node1,
139 gconstpointer node2);
140 static inline void emission_push (Emission **emission_list_p,
142 static inline void emission_pop (Emission **emission_list_p,
144 static inline Emission* emission_find (Emission *emission_list,
148 static gint class_closures_cmp (gconstpointer node1,
149 gconstpointer node2);
150 static gint signal_key_cmp (gconstpointer node1,
151 gconstpointer node2);
152 static gboolean signal_emit_unlocked_R (SignalNode *node,
155 GValue *return_value,
156 const GValue *instance_and_params);
157 static const gchar * type_debug_name (GType type);
160 /* --- structures --- */
163 GSignalAccumulator func;
171 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
175 /* permanent portion */
181 /* reinitializable portion */
182 guint test_class_offset : 12;
185 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
186 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
187 GBSearchArray *class_closure_bsa;
188 SignalAccumulator *accumulator;
189 GSignalCMarshaller c_marshaller;
190 GHookList *emission_hooks;
192 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
193 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
206 GSignalInvocationHint ihint;
215 Handler *tail_before; /* normal signal handlers are appended here */
216 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
221 gulong sequential_number;
226 guint block_count : 16;
227 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
243 GType instance_type; /* 0 for default closure */
248 /* --- variables --- */
249 static GBSearchArray *g_signal_key_bsa = NULL;
250 static const GBSearchConfig g_signal_key_bconfig = {
253 G_BSEARCH_ARRAY_ALIGN_POWER2,
255 static GBSearchConfig g_signal_hlbsa_bconfig = {
256 sizeof (HandlerList),
260 static GBSearchConfig g_class_closure_bconfig = {
261 sizeof (ClassClosure),
265 static GHashTable *g_handler_list_bsa_ht = NULL;
266 static Emission *g_recursive_emissions = NULL;
267 static Emission *g_restart_emissions = NULL;
268 #ifndef DISABLE_MEM_POOLS
269 static GTrashStack *g_handler_ts = NULL;
271 static gulong g_handler_sequential_number = 1;
272 G_LOCK_DEFINE_STATIC (g_signal_mutex);
273 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
274 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
277 /* --- signal nodes --- */
278 static guint g_n_signal_nodes = 0;
279 static SignalNode **g_signal_nodes = NULL;
281 static inline SignalNode*
282 LOOKUP_SIGNAL_NODE (register guint signal_id)
284 if (signal_id < g_n_signal_nodes)
285 return g_signal_nodes[signal_id];
291 /* --- functions --- */
293 signal_id_lookup (GQuark quark,
296 GType *ifaces, type = itype;
302 /* try looking up signals for this type and its ancestors */
305 SignalKey *signal_key;
308 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
311 return signal_key->signal_id;
313 type = g_type_parent (type);
317 /* no luck, try interfaces it exports */
318 ifaces = g_type_interfaces (itype, &n_ifaces);
321 SignalKey *signal_key;
323 key.itype = ifaces[n_ifaces];
324 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
329 return signal_key->signal_id;
338 class_closures_cmp (gconstpointer node1,
341 const ClassClosure *c1 = node1, *c2 = node2;
343 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
347 handler_lists_cmp (gconstpointer node1,
350 const HandlerList *hlist1 = node1, *hlist2 = node2;
352 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
355 static inline HandlerList*
356 handler_list_ensure (guint signal_id,
359 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
362 key.signal_id = signal_id;
364 key.tail_before = NULL;
365 key.tail_after = NULL;
368 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
369 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
370 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
374 GBSearchArray *o = hlbsa;
376 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
378 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
380 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
383 static inline HandlerList*
384 handler_list_lookup (guint signal_id,
387 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
390 key.signal_id = signal_id;
392 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
396 handler_lookup (gpointer instance,
400 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
406 for (i = 0; i < hlbsa->n_nodes; i++)
408 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
411 for (handler = hlist->handlers; handler; handler = handler->next)
412 if (handler->sequential_number == handler_id)
415 *signal_id_p = hlist->signal_id;
425 static inline HandlerMatch*
426 handler_match_prepend (HandlerMatch *list,
432 /* yeah, we could use our own memchunk here, introducing yet more
433 * rarely used cached nodes and extra allocation overhead.
434 * instead, we use GList* nodes, since they are exactly the size
435 * we need and are already cached. g_signal_init() asserts this.
437 node = (HandlerMatch*) g_list_alloc ();
438 node->handler = handler;
440 node->d.signal_id = signal_id;
441 handler_ref (handler);
445 static inline HandlerMatch*
446 handler_match_free1_R (HandlerMatch *node,
449 HandlerMatch *next = node->next;
451 handler_unref_R (node->d.signal_id, instance, node->handler);
452 g_list_free_1 ((GList*) node);
458 handlers_find (gpointer instance,
459 GSignalMatchType mask,
465 gboolean one_and_only)
467 HandlerMatch *mlist = NULL;
469 if (mask & G_SIGNAL_MATCH_ID)
471 HandlerList *hlist = handler_list_lookup (signal_id, instance);
473 SignalNode *node = NULL;
475 if (mask & G_SIGNAL_MATCH_FUNC)
477 node = LOOKUP_SIGNAL_NODE (signal_id);
478 if (!node || !node->c_marshaller)
483 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
484 if (handler->sequential_number &&
485 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
486 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
487 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
488 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
489 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
490 handler->closure->meta_marshal == 0 &&
491 ((GCClosure*) handler->closure)->callback == func)))
493 mlist = handler_match_prepend (mlist, handler, signal_id);
500 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
507 for (i = 0; i < hlbsa->n_nodes; i++)
509 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
510 SignalNode *node = NULL;
513 if (!(mask & G_SIGNAL_MATCH_FUNC))
515 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
516 if (!node->c_marshaller)
520 for (handler = hlist->handlers; handler; handler = handler->next)
521 if (handler->sequential_number &&
522 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
523 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
524 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
525 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
526 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
527 handler->closure->meta_marshal == 0 &&
528 ((GCClosure*) handler->closure)->callback == func)))
530 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
541 static inline Handler*
542 handler_new (gboolean after)
544 Handler *handler = g_generic_node_alloc (&g_handler_ts,
547 #ifndef G_DISABLE_CHECKS
548 if (g_handler_sequential_number < 1)
549 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
552 handler->sequential_number = g_handler_sequential_number++;
553 handler->prev = NULL;
554 handler->next = NULL;
556 handler->ref_count = 1;
557 handler->block_count = 0;
558 handler->after = after != FALSE;
559 handler->closure = NULL;
565 handler_ref (Handler *handler)
567 g_return_if_fail (handler->ref_count > 0);
569 g_atomic_int_inc (&handler->ref_count);
573 handler_unref_R (guint signal_id,
579 g_return_if_fail (handler->ref_count > 0);
581 is_zero = g_atomic_int_dec_and_test (&handler->ref_count);
583 if (G_UNLIKELY (is_zero))
585 HandlerList *hlist = NULL;
588 handler->next->prev = handler->prev;
589 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
590 handler->prev->next = handler->next;
593 hlist = handler_list_lookup (signal_id, instance);
594 hlist->handlers = handler->next;
599 /* check if we are removing the handler pointed to by tail_before */
600 if (!handler->after && (!handler->next || handler->next->after))
603 hlist = handler_list_lookup (signal_id, instance);
606 g_assert (hlist->tail_before == handler); /* paranoid */
607 hlist->tail_before = handler->prev;
611 /* check if we are removing the handler pointed to by tail_after */
615 hlist = handler_list_lookup (signal_id, instance);
618 g_assert (hlist->tail_after == handler); /* paranoid */
619 hlist->tail_after = handler->prev;
625 g_closure_unref (handler->closure);
627 g_generic_node_free (&g_handler_ts, handler);
632 handler_insert (guint signal_id,
638 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
640 hlist = handler_list_ensure (signal_id, instance);
641 if (!hlist->handlers)
643 hlist->handlers = handler;
645 hlist->tail_before = handler;
647 else if (handler->after)
649 handler->prev = hlist->tail_after;
650 hlist->tail_after->next = handler;
654 if (hlist->tail_before)
656 handler->next = hlist->tail_before->next;
658 handler->next->prev = handler;
659 handler->prev = hlist->tail_before;
660 hlist->tail_before->next = handler;
662 else /* insert !after handler into a list of only after handlers */
664 handler->next = hlist->handlers;
666 handler->next->prev = handler;
667 hlist->handlers = handler;
669 hlist->tail_before = handler;
673 hlist->tail_after = handler;
677 emission_push (Emission **emission_list_p,
680 emission->next = *emission_list_p;
681 *emission_list_p = emission;
685 emission_pop (Emission **emission_list_p,
688 Emission *node, *last = NULL;
690 for (node = *emission_list_p; node; last = node, node = last->next)
691 if (node == emission)
694 last->next = node->next;
696 *emission_list_p = node->next;
699 g_assert_not_reached ();
702 static inline Emission*
703 emission_find (Emission *emission_list,
710 for (emission = emission_list; emission; emission = emission->next)
711 if (emission->instance == instance &&
712 emission->ihint.signal_id == signal_id &&
713 emission->ihint.detail == detail)
718 static inline Emission*
719 emission_find_innermost (gpointer instance)
721 Emission *emission, *s = NULL, *c = NULL;
723 for (emission = g_restart_emissions; emission; emission = emission->next)
724 if (emission->instance == instance)
729 for (emission = g_recursive_emissions; emission; emission = emission->next)
730 if (emission->instance == instance)
740 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
744 signal_key_cmp (gconstpointer node1,
747 const SignalKey *key1 = node1, *key2 = node2;
749 if (key1->itype == key2->itype)
750 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
752 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
759 if (!g_n_signal_nodes)
761 /* handler_id_node_prepend() requires this */
762 g_assert (sizeof (GList) == sizeof (HandlerMatch));
764 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
765 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
766 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
768 /* invalid (0) signal_id */
769 g_n_signal_nodes = 1;
770 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
771 g_signal_nodes[0] = NULL;
777 _g_signals_destroy (GType itype)
782 for (i = 1; i < g_n_signal_nodes; i++)
784 SignalNode *node = g_signal_nodes[i];
786 if (node->itype == itype)
789 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
791 type_debug_name (node->itype));
793 signal_destroy_R (node);
800 g_signal_stop_emission (gpointer instance,
806 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
807 g_return_if_fail (signal_id > 0);
810 node = LOOKUP_SIGNAL_NODE (signal_id);
811 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
813 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
817 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
819 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
820 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
824 if (emission->state == EMISSION_HOOK)
825 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
826 node->name, instance);
827 else if (emission->state == EMISSION_RUN)
828 emission->state = EMISSION_STOP;
831 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
832 node->name, instance);
835 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
840 signal_finalize_hook (GHookList *hook_list,
843 GDestroyNotify destroy = hook->destroy;
847 hook->destroy = NULL;
849 destroy (hook->data);
855 g_signal_add_emission_hook (guint signal_id,
857 GSignalEmissionHook hook_func,
859 GDestroyNotify data_destroy)
861 static gulong seq_hook_id = 1;
864 SignalHook *signal_hook;
866 g_return_val_if_fail (signal_id > 0, 0);
867 g_return_val_if_fail (hook_func != NULL, 0);
870 node = LOOKUP_SIGNAL_NODE (signal_id);
871 if (!node || node->destroyed)
873 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
877 if (node->flags & G_SIGNAL_NO_HOOKS)
879 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
883 if (detail && !(node->flags & G_SIGNAL_DETAILED))
885 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
889 if (!node->emission_hooks)
891 node->emission_hooks = g_new (GHookList, 1);
892 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
893 node->emission_hooks->finalize_hook = signal_finalize_hook;
895 hook = g_hook_alloc (node->emission_hooks);
896 hook->data = hook_data;
897 hook->func = (gpointer) hook_func;
898 hook->destroy = data_destroy;
899 signal_hook = SIGNAL_HOOK (hook);
900 signal_hook->detail = detail;
901 node->emission_hooks->seq_id = seq_hook_id;
902 g_hook_append (node->emission_hooks, hook);
903 seq_hook_id = node->emission_hooks->seq_id;
906 return hook->hook_id;
910 g_signal_remove_emission_hook (guint signal_id,
915 g_return_if_fail (signal_id > 0);
916 g_return_if_fail (hook_id > 0);
919 node = LOOKUP_SIGNAL_NODE (signal_id);
920 if (!node || node->destroyed)
921 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
922 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
923 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
928 signal_parse_name (const gchar *name,
931 gboolean force_quark)
933 const gchar *colon = strchr (name, ':');
938 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
939 if (signal_id && detail_p)
942 else if (colon[1] == ':')
945 guint l = colon - name;
949 memcpy (buffer, name, l);
951 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
955 gchar *signal = g_new (gchar, l + 1);
957 memcpy (signal, name, l);
959 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
963 if (signal_id && detail_p)
964 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
972 g_signal_parse_name (const gchar *detailed_signal,
976 gboolean force_detail_quark)
982 g_return_val_if_fail (detailed_signal != NULL, FALSE);
983 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
986 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
989 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
990 if (!node || node->destroyed ||
991 (detail && !(node->flags & G_SIGNAL_DETAILED)))
995 *signal_id_p = signal_id;
1003 g_signal_stop_emission_by_name (gpointer instance,
1004 const gchar *detailed_signal)
1010 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1011 g_return_if_fail (detailed_signal != NULL);
1014 itype = G_TYPE_FROM_INSTANCE (instance);
1015 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1018 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1020 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1021 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1022 else if (!g_type_is_a (itype, node->itype))
1023 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1026 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1027 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1031 if (emission->state == EMISSION_HOOK)
1032 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1033 node->name, instance);
1034 else if (emission->state == EMISSION_RUN)
1035 emission->state = EMISSION_STOP;
1038 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1039 node->name, instance);
1043 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1048 g_signal_lookup (const gchar *name,
1052 g_return_val_if_fail (name != NULL, 0);
1053 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1056 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1060 /* give elaborate warnings */
1061 if (!g_type_name (itype))
1062 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1064 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1065 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1066 name, g_type_name (itype));
1067 else if (!g_type_class_peek (itype))
1068 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1069 name, g_type_name (itype));
1076 g_signal_list_ids (GType itype,
1084 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1085 g_return_val_if_fail (n_ids != NULL, NULL);
1088 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1089 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1090 result = g_array_new (FALSE, FALSE, sizeof (guint));
1092 for (i = 0; i < n_nodes; i++)
1093 if (keys[i].itype == itype)
1095 const gchar *name = g_quark_to_string (keys[i].quark);
1097 /* Signal names with "_" in them are aliases to the same
1098 * name with "-" instead of "_".
1100 if (!strchr (name, '_'))
1101 g_array_append_val (result, keys[i].signal_id);
1103 *n_ids = result->len;
1107 /* give elaborate warnings */
1108 if (!g_type_name (itype))
1109 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1111 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1112 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1113 g_type_name (itype));
1114 else if (!g_type_class_peek (itype))
1115 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1116 g_type_name (itype));
1119 return (guint*) g_array_free (result, FALSE);
1122 G_CONST_RETURN gchar*
1123 g_signal_name (guint signal_id)
1129 node = LOOKUP_SIGNAL_NODE (signal_id);
1130 name = node ? node->name : NULL;
1133 return (char*) name;
1137 g_signal_query (guint signal_id,
1138 GSignalQuery *query)
1142 g_return_if_fail (query != NULL);
1145 node = LOOKUP_SIGNAL_NODE (signal_id);
1146 if (!node || node->destroyed)
1147 query->signal_id = 0;
1150 query->signal_id = node->signal_id;
1151 query->signal_name = node->name;
1152 query->itype = node->itype;
1153 query->signal_flags = node->flags;
1154 query->return_type = node->return_type;
1155 query->n_params = node->n_params;
1156 query->param_types = node->param_types;
1162 g_signal_new (const gchar *signal_name,
1164 GSignalFlags signal_flags,
1166 GSignalAccumulator accumulator,
1168 GSignalCMarshaller c_marshaller,
1176 g_return_val_if_fail (signal_name != NULL, 0);
1178 va_start (args, n_params);
1180 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1181 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1182 accumulator, accu_data, c_marshaller,
1183 return_type, n_params, args);
1187 /* optimize NOP emissions with NULL class handlers */
1188 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1189 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1194 node = LOOKUP_SIGNAL_NODE (signal_id);
1195 node->test_class_offset = class_offset;
1202 static inline ClassClosure*
1203 signal_find_class_closure (SignalNode *node,
1206 GBSearchArray *bsa = node->class_closure_bsa;
1213 /* cc->instance_type is 0 for default closure */
1215 key.instance_type = itype;
1216 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1217 while (!cc && key.instance_type)
1219 key.instance_type = g_type_parent (key.instance_type);
1220 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1228 static inline GClosure*
1229 signal_lookup_closure (SignalNode *node,
1230 GTypeInstance *instance)
1234 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1235 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1237 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1238 return cc ? cc->closure : NULL;
1242 signal_add_class_closure (SignalNode *node,
1248 /* can't optimize NOP emissions with overridden class closures */
1249 node->test_class_offset = 0;
1251 if (!node->class_closure_bsa)
1252 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1253 key.instance_type = itype;
1254 key.closure = g_closure_ref (closure);
1255 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1256 &g_class_closure_bconfig,
1258 g_closure_sink (closure);
1259 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1260 g_closure_set_marshal (closure, node->c_marshaller);
1264 g_signal_newv (const gchar *signal_name,
1266 GSignalFlags signal_flags,
1267 GClosure *class_closure,
1268 GSignalAccumulator accumulator,
1270 GSignalCMarshaller c_marshaller,
1279 g_return_val_if_fail (signal_name != NULL, 0);
1280 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1282 g_return_val_if_fail (param_types != NULL, 0);
1283 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1284 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1285 g_return_val_if_fail (accumulator == NULL, 0);
1287 g_return_val_if_fail (accu_data == NULL, 0);
1289 name = g_strdup (signal_name);
1290 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1294 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1295 node = LOOKUP_SIGNAL_NODE (signal_id);
1296 if (node && !node->destroyed)
1298 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1300 type_debug_name (node->itype),
1301 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1306 if (node && node->itype != itype)
1308 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1310 type_debug_name (itype),
1311 type_debug_name (node->itype));
1316 for (i = 0; i < n_params; i++)
1317 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1319 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1320 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1325 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1327 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1328 type_debug_name (return_type), type_debug_name (itype), name);
1333 if (return_type != G_TYPE_NONE &&
1334 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1336 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1337 type_debug_name (itype), name, type_debug_name (return_type));
1343 /* setup permanent portion of signal node */
1348 signal_id = g_n_signal_nodes++;
1349 node = g_new (SignalNode, 1);
1350 node->signal_id = signal_id;
1351 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1352 g_signal_nodes[signal_id] = node;
1353 node->itype = itype;
1356 key.quark = g_quark_from_string (node->name);
1357 key.signal_id = signal_id;
1358 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1359 g_strdelimit (name, "_", '-');
1360 node->name = g_intern_string (name);
1361 key.quark = g_quark_from_string (name);
1362 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1364 node->destroyed = FALSE;
1365 node->test_class_offset = 0;
1367 /* setup reinitializable portion */
1368 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1369 node->n_params = n_params;
1370 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1371 node->return_type = return_type;
1372 node->class_closure_bsa = NULL;
1375 node->accumulator = g_new (SignalAccumulator, 1);
1376 node->accumulator->func = accumulator;
1377 node->accumulator->data = accu_data;
1380 node->accumulator = NULL;
1381 node->c_marshaller = c_marshaller;
1382 node->emission_hooks = NULL;
1384 signal_add_class_closure (node, 0, class_closure);
1385 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1387 /* optimize NOP emissions */
1388 node->test_class_offset = TEST_CLASS_MAGIC;
1398 g_signal_new_valist (const gchar *signal_name,
1400 GSignalFlags signal_flags,
1401 GClosure *class_closure,
1402 GSignalAccumulator accumulator,
1404 GSignalCMarshaller c_marshaller,
1415 param_types = g_new (GType, n_params);
1417 for (i = 0; i < n_params; i++)
1418 param_types[i] = va_arg (args, GType);
1423 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1424 class_closure, accumulator, accu_data, c_marshaller,
1425 return_type, n_params, param_types);
1426 g_free (param_types);
1432 signal_destroy_R (SignalNode *signal_node)
1434 SignalNode node = *signal_node;
1436 signal_node->destroyed = TRUE;
1438 /* reentrancy caution, zero out real contents first */
1439 signal_node->test_class_offset = 0;
1440 signal_node->n_params = 0;
1441 signal_node->param_types = NULL;
1442 signal_node->return_type = 0;
1443 signal_node->class_closure_bsa = NULL;
1444 signal_node->accumulator = NULL;
1445 signal_node->c_marshaller = NULL;
1446 signal_node->emission_hooks = NULL;
1448 #ifdef G_ENABLE_DEBUG
1449 /* check current emissions */
1453 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1454 emission; emission = emission->next)
1455 if (emission->ihint.signal_id == node.signal_id)
1456 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1457 node.name, emission->instance);
1461 /* free contents that need to
1464 g_free (node.param_types);
1465 if (node.class_closure_bsa)
1469 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1471 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1473 g_closure_unref (cc->closure);
1475 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1477 g_free (node.accumulator);
1478 if (node.emission_hooks)
1480 g_hook_list_clear (node.emission_hooks);
1481 g_free (node.emission_hooks);
1487 g_signal_override_class_closure (guint signal_id,
1488 GType instance_type,
1489 GClosure *class_closure)
1493 g_return_if_fail (signal_id > 0);
1494 g_return_if_fail (class_closure != NULL);
1497 node = LOOKUP_SIGNAL_NODE (signal_id);
1498 if (!g_type_is_a (instance_type, node->itype))
1499 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1502 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1504 if (cc && cc->instance_type == instance_type)
1505 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1507 signal_add_class_closure (node, instance_type, class_closure);
1513 g_signal_chain_from_overridden (const GValue *instance_and_params,
1514 GValue *return_value)
1516 GType chain_type = 0, restore_type = 0;
1517 Emission *emission = NULL;
1518 GClosure *closure = NULL;
1522 g_return_if_fail (instance_and_params != NULL);
1523 instance = g_value_peek_pointer (instance_and_params);
1524 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1527 emission = emission_find_innermost (instance);
1530 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1532 g_assert (node != NULL); /* paranoid */
1534 /* we should probably do the same parameter checks as g_signal_emit() here.
1536 if (emission->chain_type != G_TYPE_NONE)
1538 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1540 g_assert (cc != NULL); /* closure currently in call stack */
1542 n_params = node->n_params;
1543 restore_type = cc->instance_type;
1544 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1545 if (cc && cc->instance_type != restore_type)
1547 closure = cc->closure;
1548 chain_type = cc->instance_type;
1552 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1555 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1558 emission->chain_type = chain_type;
1560 g_closure_invoke (closure,
1563 instance_and_params,
1566 emission->chain_type = restore_type;
1571 GSignalInvocationHint*
1572 g_signal_get_invocation_hint (gpointer instance)
1574 Emission *emission = NULL;
1576 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1579 emission = emission_find_innermost (instance);
1582 return emission ? &emission->ihint : NULL;
1586 g_signal_connect_closure_by_id (gpointer instance,
1593 gulong handler_seq_no = 0;
1595 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1596 g_return_val_if_fail (signal_id > 0, 0);
1597 g_return_val_if_fail (closure != NULL, 0);
1600 node = LOOKUP_SIGNAL_NODE (signal_id);
1603 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1604 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1605 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1606 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1609 Handler *handler = handler_new (after);
1611 handler_seq_no = handler->sequential_number;
1612 handler->detail = detail;
1613 handler->closure = g_closure_ref (closure);
1614 g_closure_sink (closure);
1615 handler_insert (signal_id, instance, handler);
1616 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1617 g_closure_set_marshal (closure, node->c_marshaller);
1621 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1624 return handler_seq_no;
1628 g_signal_connect_closure (gpointer instance,
1629 const gchar *detailed_signal,
1634 gulong handler_seq_no = 0;
1638 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1639 g_return_val_if_fail (detailed_signal != NULL, 0);
1640 g_return_val_if_fail (closure != NULL, 0);
1643 itype = G_TYPE_FROM_INSTANCE (instance);
1644 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1647 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1649 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1650 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1651 else if (!g_type_is_a (itype, node->itype))
1652 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1655 Handler *handler = handler_new (after);
1657 handler_seq_no = handler->sequential_number;
1658 handler->detail = detail;
1659 handler->closure = g_closure_ref (closure);
1660 g_closure_sink (closure);
1661 handler_insert (signal_id, instance, handler);
1662 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1663 g_closure_set_marshal (handler->closure, node->c_marshaller);
1667 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1670 return handler_seq_no;
1674 g_signal_connect_data (gpointer instance,
1675 const gchar *detailed_signal,
1676 GCallback c_handler,
1678 GClosureNotify destroy_data,
1679 GConnectFlags connect_flags)
1682 gulong handler_seq_no = 0;
1685 gboolean swapped, after;
1687 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1688 g_return_val_if_fail (detailed_signal != NULL, 0);
1689 g_return_val_if_fail (c_handler != NULL, 0);
1691 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1692 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1695 itype = G_TYPE_FROM_INSTANCE (instance);
1696 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1699 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1701 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1702 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1703 else if (!g_type_is_a (itype, node->itype))
1704 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1707 Handler *handler = handler_new (after);
1709 handler_seq_no = handler->sequential_number;
1710 handler->detail = detail;
1711 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1712 g_closure_sink (handler->closure);
1713 handler_insert (signal_id, instance, handler);
1714 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1715 g_closure_set_marshal (handler->closure, node->c_marshaller);
1719 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1722 return handler_seq_no;
1726 g_signal_handler_block (gpointer instance,
1731 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1732 g_return_if_fail (handler_id > 0);
1735 handler = handler_lookup (instance, handler_id, NULL);
1738 #ifndef G_DISABLE_CHECKS
1739 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1740 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1742 handler->block_count += 1;
1745 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1750 g_signal_handler_unblock (gpointer instance,
1755 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1756 g_return_if_fail (handler_id > 0);
1759 handler = handler_lookup (instance, handler_id, NULL);
1762 if (handler->block_count)
1763 handler->block_count -= 1;
1765 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1768 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1773 g_signal_handler_disconnect (gpointer instance,
1779 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1780 g_return_if_fail (handler_id > 0);
1783 handler = handler_lookup (instance, handler_id, &signal_id);
1786 handler->sequential_number = 0;
1787 handler->block_count = 1;
1788 handler_unref_R (signal_id, instance, handler);
1791 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1796 g_signal_handler_is_connected (gpointer instance,
1802 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1805 handler = handler_lookup (instance, handler_id, NULL);
1806 connected = handler != NULL;
1813 g_signal_handlers_destroy (gpointer instance)
1815 GBSearchArray *hlbsa;
1817 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1820 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1825 /* reentrancy caution, delete instance trace first */
1826 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1828 for (i = 0; i < hlbsa->n_nodes; i++)
1830 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1831 Handler *handler = hlist->handlers;
1835 Handler *tmp = handler;
1837 handler = tmp->next;
1838 tmp->block_count = 1;
1839 /* cruel unlink, this works because _all_ handlers vanish */
1842 if (tmp->sequential_number)
1844 tmp->sequential_number = 0;
1845 handler_unref_R (0, NULL, tmp);
1849 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
1855 g_signal_handler_find (gpointer instance,
1856 GSignalMatchType mask,
1863 gulong handler_seq_no = 0;
1865 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1866 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1868 if (mask & G_SIGNAL_MATCH_MASK)
1870 HandlerMatch *mlist;
1873 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1876 handler_seq_no = mlist->handler->sequential_number;
1877 handler_match_free1_R (mlist, instance);
1882 return handler_seq_no;
1886 signal_handlers_foreach_matched_R (gpointer instance,
1887 GSignalMatchType mask,
1893 void (*callback) (gpointer instance,
1894 gulong handler_seq_no))
1896 HandlerMatch *mlist;
1897 guint n_handlers = 0;
1899 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1903 if (mlist->handler->sequential_number)
1906 callback (instance, mlist->handler->sequential_number);
1909 mlist = handler_match_free1_R (mlist, instance);
1916 g_signal_handlers_block_matched (gpointer instance,
1917 GSignalMatchType mask,
1924 guint n_handlers = 0;
1926 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1927 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1929 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1932 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1933 closure, func, data,
1934 g_signal_handler_block);
1942 g_signal_handlers_unblock_matched (gpointer instance,
1943 GSignalMatchType mask,
1950 guint n_handlers = 0;
1952 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1953 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1955 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1958 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1959 closure, func, data,
1960 g_signal_handler_unblock);
1968 g_signal_handlers_disconnect_matched (gpointer instance,
1969 GSignalMatchType mask,
1976 guint n_handlers = 0;
1978 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1979 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1981 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1984 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1985 closure, func, data,
1986 g_signal_handler_disconnect);
1994 g_signal_has_handler_pending (gpointer instance,
1997 gboolean may_be_blocked)
1999 HandlerMatch *mlist;
2000 gboolean has_pending;
2002 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2003 g_return_val_if_fail (signal_id > 0, FALSE);
2008 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2010 if (!(node->flags & G_SIGNAL_DETAILED))
2012 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2017 mlist = handlers_find (instance,
2018 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2019 signal_id, detail, NULL, NULL, NULL, TRUE);
2023 handler_match_free1_R (mlist, instance);
2026 has_pending = FALSE;
2032 static inline gboolean
2033 signal_check_skip_emission (SignalNode *node,
2039 /* are we able to check for NULL class handlers? */
2040 if (!node->test_class_offset)
2043 /* are there emission hooks pending? */
2044 if (node->emission_hooks && node->emission_hooks->hooks)
2047 /* is there a non-NULL class handler? */
2048 if (node->test_class_offset != TEST_CLASS_MAGIC)
2050 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2052 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2056 /* are signals being debugged? */
2057 #ifdef G_ENABLE_DEBUG
2058 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2060 #endif /* G_ENABLE_DEBUG */
2062 /* is this a no-recurse signal already in emission? */
2063 if (node->flags & G_SIGNAL_NO_RECURSE &&
2064 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2067 /* do we have pending handlers? */
2068 hlist = handler_list_lookup (node->signal_id, instance);
2069 if (hlist && hlist->handlers)
2072 /* none of the above, no emission required */
2077 g_signal_emitv (const GValue *instance_and_params,
2080 GValue *return_value)
2084 #ifdef G_ENABLE_DEBUG
2085 const GValue *param_values;
2089 g_return_if_fail (instance_and_params != NULL);
2090 instance = g_value_peek_pointer (instance_and_params);
2091 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2092 g_return_if_fail (signal_id > 0);
2094 #ifdef G_ENABLE_DEBUG
2095 param_values = instance_and_params + 1;
2099 node = LOOKUP_SIGNAL_NODE (signal_id);
2100 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2102 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2106 #ifdef G_ENABLE_DEBUG
2107 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2109 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2113 for (i = 0; i < node->n_params; i++)
2114 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2116 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2118 type_debug_name (node->param_types[i]),
2121 G_VALUE_TYPE_NAME (param_values + i));
2125 if (node->return_type != G_TYPE_NONE)
2129 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2131 type_debug_name (node->return_type),
2136 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2138 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2140 type_debug_name (node->return_type),
2142 G_VALUE_TYPE_NAME (return_value));
2148 return_value = NULL;
2149 #endif /* G_ENABLE_DEBUG */
2151 /* optimize NOP emissions */
2152 if (signal_check_skip_emission (node, instance, detail))
2154 /* nothing to do to emit this signal */
2156 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2161 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2165 g_signal_emit_valist (gpointer instance,
2170 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2171 GType signal_return_type;
2172 GValue *param_values;
2176 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2177 g_return_if_fail (signal_id > 0);
2180 node = LOOKUP_SIGNAL_NODE (signal_id);
2181 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2183 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2187 #ifndef G_DISABLE_CHECKS
2188 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2190 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2194 #endif /* !G_DISABLE_CHECKS */
2196 /* optimize NOP emissions */
2197 if (signal_check_skip_emission (node, instance, detail))
2199 /* nothing to do to emit this signal */
2201 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2205 n_params = node->n_params;
2206 signal_return_type = node->return_type;
2207 if (node->n_params < MAX_STACK_VALUES)
2208 instance_and_params = stack_values;
2211 free_me = g_new (GValue, node->n_params + 1);
2212 instance_and_params = free_me;
2214 param_values = instance_and_params + 1;
2215 for (i = 0; i < node->n_params; i++)
2218 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2219 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2221 param_values[i].g_type = 0;
2223 g_value_init (param_values + i, ptype);
2224 G_VALUE_COLLECT (param_values + i,
2226 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2230 g_warning ("%s: %s", G_STRLOC, error);
2233 /* we purposely leak the value here, it might not be
2234 * in a sane state if an error condition occoured
2237 g_value_unset (param_values + i);
2245 instance_and_params->g_type = 0;
2246 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2247 g_value_set_instance (instance_and_params, instance);
2248 if (signal_return_type == G_TYPE_NONE)
2249 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2252 GValue return_value = { 0, };
2253 gchar *error = NULL;
2254 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2255 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2257 g_value_init (&return_value, rtype);
2259 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2261 G_VALUE_LCOPY (&return_value,
2263 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2266 g_value_unset (&return_value);
2269 g_warning ("%s: %s", G_STRLOC, error);
2272 /* we purposely leak the value here, it might not be
2273 * in a sane state if an error condition occured
2277 for (i = 0; i < n_params; i++)
2278 g_value_unset (param_values + i);
2279 g_value_unset (instance_and_params);
2285 g_signal_emit (gpointer instance,
2292 va_start (var_args, detail);
2293 g_signal_emit_valist (instance, signal_id, detail, var_args);
2298 g_signal_emit_by_name (gpointer instance,
2299 const gchar *detailed_signal,
2305 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2306 g_return_if_fail (detailed_signal != NULL);
2309 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2316 va_start (var_args, detailed_signal);
2317 g_signal_emit_valist (instance, signal_id, detail, var_args);
2321 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2324 static inline gboolean
2325 accumulate (GSignalInvocationHint *ihint,
2326 GValue *return_accu,
2327 GValue *handler_return,
2328 SignalAccumulator *accumulator)
2330 gboolean continue_emission;
2335 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2336 g_value_reset (handler_return);
2338 return continue_emission;
2342 signal_emit_unlocked_R (SignalNode *node,
2345 GValue *emission_return,
2346 const GValue *instance_and_params)
2348 SignalAccumulator *accumulator;
2350 GClosure *class_closure;
2352 Handler *handler_list = NULL;
2353 GValue *return_accu, accu = { 0, };
2355 gulong max_sequential_handler_number;
2356 gboolean return_value_altered = FALSE;
2358 #ifdef G_ENABLE_DEBUG
2359 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2361 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2362 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2365 if (g_trap_instance_signals == instance)
2368 #endif /* G_ENABLE_DEBUG */
2371 signal_id = node->signal_id;
2372 if (node->flags & G_SIGNAL_NO_RECURSE)
2374 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2378 node->state = EMISSION_RESTART;
2380 return return_value_altered;
2383 accumulator = node->accumulator;
2387 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2388 return_accu = &accu;
2392 return_accu = emission_return;
2393 emission.instance = instance;
2394 emission.ihint.signal_id = node->signal_id;
2395 emission.ihint.detail = detail;
2396 emission.ihint.run_type = 0;
2398 emission.chain_type = G_TYPE_NONE;
2399 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2400 class_closure = signal_lookup_closure (node, instance);
2405 handler_unref_R (signal_id, instance, handler_list);
2406 max_sequential_handler_number = g_handler_sequential_number;
2407 hlist = handler_list_lookup (signal_id, instance);
2408 handler_list = hlist ? hlist->handlers : NULL;
2410 handler_ref (handler_list);
2412 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2414 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2416 emission.state = EMISSION_RUN;
2418 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2420 g_closure_invoke (class_closure,
2423 instance_and_params,
2425 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2426 emission.state == EMISSION_RUN)
2427 emission.state = EMISSION_STOP;
2429 emission.chain_type = G_TYPE_NONE;
2430 return_value_altered = TRUE;
2432 if (emission.state == EMISSION_STOP)
2434 else if (emission.state == EMISSION_RESTART)
2438 if (node->emission_hooks)
2440 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2443 emission.state = EMISSION_HOOK;
2444 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2447 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2449 if (!signal_hook->detail || signal_hook->detail == detail)
2451 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2453 was_in_call = G_HOOK_IN_CALL (hook);
2454 hook->flags |= G_HOOK_FLAG_IN_CALL;
2456 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2459 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2461 g_hook_destroy_link (node->emission_hooks, hook);
2463 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2466 if (emission.state == EMISSION_RESTART)
2472 Handler *handler = handler_list;
2474 emission.state = EMISSION_RUN;
2475 handler_ref (handler);
2482 handler_unref_R (signal_id, instance, handler_list);
2483 handler_list = handler;
2486 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2487 handler->sequential_number < max_sequential_handler_number)
2490 g_closure_invoke (handler->closure,
2493 instance_and_params,
2495 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2496 emission.state == EMISSION_RUN)
2497 emission.state = EMISSION_STOP;
2499 return_value_altered = TRUE;
2501 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2504 tmp = handler->next;
2508 handler_unref_R (signal_id, instance, handler_list);
2509 handler_list = handler;
2514 if (emission.state == EMISSION_STOP)
2516 else if (emission.state == EMISSION_RESTART)
2520 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2522 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2524 emission.state = EMISSION_RUN;
2526 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2528 g_closure_invoke (class_closure,
2531 instance_and_params,
2533 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2534 emission.state == EMISSION_RUN)
2535 emission.state = EMISSION_STOP;
2537 emission.chain_type = G_TYPE_NONE;
2538 return_value_altered = TRUE;
2540 if (emission.state == EMISSION_STOP)
2542 else if (emission.state == EMISSION_RESTART)
2548 Handler *handler = handler_list;
2550 emission.state = EMISSION_RUN;
2551 handler_ref (handler);
2556 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2557 handler->sequential_number < max_sequential_handler_number)
2560 g_closure_invoke (handler->closure,
2563 instance_and_params,
2565 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2566 emission.state == EMISSION_RUN)
2567 emission.state = EMISSION_STOP;
2569 return_value_altered = TRUE;
2571 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2574 tmp = handler->next;
2578 handler_unref_R (signal_id, instance, handler);
2583 if (emission.state == EMISSION_STOP)
2585 else if (emission.state == EMISSION_RESTART)
2591 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2593 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2595 gboolean need_unset = FALSE;
2597 emission.state = EMISSION_STOP;
2599 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2601 if (node->return_type != G_TYPE_NONE && !accumulator)
2603 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2606 g_closure_invoke (class_closure,
2607 node->return_type != G_TYPE_NONE ? &accu : NULL,
2609 instance_and_params,
2612 g_value_unset (&accu);
2614 emission.chain_type = G_TYPE_NONE;
2616 if (emission.state == EMISSION_RESTART)
2621 handler_unref_R (signal_id, instance, handler_list);
2623 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2626 g_value_unset (&accu);
2628 return return_value_altered;
2632 type_debug_name (GType type)
2636 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2637 return name ? name : "<unknown>";
2644 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
2645 GValue *return_accu,
2646 const GValue *handler_return,
2649 gboolean continue_emission;
2650 gboolean signal_handled;
2652 signal_handled = g_value_get_boolean (handler_return);
2653 g_value_set_boolean (return_accu, signal_handled);
2654 continue_emission = !signal_handled;
2656 return continue_emission;
2659 /* --- compile standard marshallers --- */
2660 #include "gobject.h"
2662 #include "gmarshal.c"
2664 #define __G_SIGNAL_C__
2665 #include "gobjectaliasdef.c"