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>
30 #include "gbsearcharray.h"
31 #include "gvaluecollector.h"
32 #include "gvaluetypes.h"
38 /* pre allocation configurations
40 #define MAX_STACK_VALUES (16)
41 #define HANDLER_PRE_ALLOC (48)
43 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
45 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
46 static volatile gpointer g_trace_instance_signals = NULL;
47 static volatile gpointer g_trap_instance_signals = NULL;
48 #endif /* G_ENABLE_DEBUG */
51 /* --- generic allocation --- */
52 /* we special case allocations generically by replacing
53 * these functions with more speed/memory aware variants
55 #ifndef DISABLE_MEM_POOLS
56 static inline gpointer
57 g_generic_node_alloc (GTrashStack **trash_stack_p,
59 guint nodes_pre_alloc)
61 gpointer node = g_trash_stack_pop (trash_stack_p);
67 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
68 block = g_malloc (sizeof_node * nodes_pre_alloc);
69 while (--nodes_pre_alloc)
71 g_trash_stack_push (trash_stack_p, block);
79 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
80 #else /* !DISABLE_MEM_POOLS */
81 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
82 #define g_generic_node_free(t,node) g_free (node)
83 #endif /* !DISABLE_MEM_POOLS */
86 /* --- typedefs --- */
87 typedef struct _SignalNode SignalNode;
88 typedef struct _SignalKey SignalKey;
89 typedef struct _Emission Emission;
90 typedef struct _Handler Handler;
91 typedef struct _HandlerList HandlerList;
92 typedef struct _HandlerMatch HandlerMatch;
102 /* --- prototypes --- */
103 static inline guint signal_id_lookup (GQuark quark,
105 static void signal_destroy_R (SignalNode *signal_node);
106 static inline HandlerList* handler_list_ensure (guint signal_id,
108 static inline HandlerList* handler_list_lookup (guint signal_id,
110 static inline Handler* handler_new (gboolean after);
111 static void handler_insert (guint signal_id,
114 static Handler* handler_lookup (gpointer instance,
117 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
120 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
122 static HandlerMatch* handlers_find (gpointer instance,
123 GSignalMatchType mask,
129 gboolean one_and_only);
130 static inline void handler_ref (Handler *handler);
131 static inline void handler_unref_R (guint signal_id,
134 static gint handler_lists_cmp (gconstpointer node1,
135 gconstpointer node2);
136 static inline void emission_push (Emission **emission_list_p,
138 static inline void emission_pop (Emission **emission_list_p,
140 static inline Emission* emission_find (Emission *emission_list,
144 static gint class_closures_cmp (gconstpointer node1,
145 gconstpointer node2);
146 static gint signal_key_cmp (gconstpointer node1,
147 gconstpointer node2);
148 static gboolean signal_emit_unlocked_R (SignalNode *node,
151 GValue *return_value,
152 const GValue *instance_and_params);
153 static const gchar * type_debug_name (GType type);
156 /* --- structures --- */
159 GSignalAccumulator func;
167 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
171 /* permanent portion */
177 /* reinitializable portion */
180 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
181 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
182 GBSearchArray *class_closure_bsa;
183 SignalAccumulator *accumulator;
184 GSignalCMarshaller c_marshaller;
185 GHookList *emission_hooks;
199 GSignalInvocationHint ihint;
211 gulong sequential_number;
215 guint ref_count : 16;
216 #define HANDLER_MAX_REF_COUNT (1 << 16)
217 guint block_count : 12;
218 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
234 GType instance_type; /* 0 for default closure */
239 /* --- variables --- */
240 static GBSearchArray *g_signal_key_bsa = NULL;
241 static GBSearchConfig g_signal_key_bconfig = G_STATIC_BCONFIG (sizeof (SignalKey),
243 G_BSEARCH_ARRAY_ALIGN_POWER2);
244 static GBSearchConfig g_signal_hlbsa_bconfig = G_STATIC_BCONFIG (sizeof (HandlerList),
246 G_BSEARCH_ARRAY_DEFER_SHRINK);
247 static GBSearchConfig g_class_closure_bconfig = G_STATIC_BCONFIG (sizeof (ClassClosure),
250 static GHashTable *g_handler_list_bsa_ht = NULL;
251 static Emission *g_recursive_emissions = NULL;
252 static Emission *g_restart_emissions = NULL;
253 #ifndef DISABLE_MEM_POOLS
254 static GTrashStack *g_handler_ts = NULL;
256 static gulong g_handler_sequential_number = 1;
257 G_LOCK_DEFINE_STATIC (g_signal_mutex);
258 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
259 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
262 /* --- signal nodes --- */
263 static guint g_n_signal_nodes = 0;
264 static SignalNode **g_signal_nodes = NULL;
266 static inline SignalNode*
267 LOOKUP_SIGNAL_NODE (register guint signal_id)
269 if (signal_id < g_n_signal_nodes)
270 return g_signal_nodes[signal_id];
276 /* --- functions --- */
278 signal_id_lookup (GQuark quark,
281 GType *ifaces, type = itype;
287 /* try looking up signals for this type and its anchestors */
290 SignalKey *signal_key;
293 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
296 return signal_key->signal_id;
298 type = g_type_parent (type);
302 /* no luck, try interfaces it exports */
303 ifaces = g_type_interfaces (itype, &n_ifaces);
306 SignalKey *signal_key;
308 key.itype = ifaces[n_ifaces];
309 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
314 return signal_key->signal_id;
323 class_closures_cmp (gconstpointer node1,
326 const ClassClosure *c1 = node1, *c2 = node2;
328 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
332 handler_lists_cmp (gconstpointer node1,
335 const HandlerList *hlist1 = node1, *hlist2 = node2;
337 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
340 static inline HandlerList*
341 handler_list_ensure (guint signal_id,
344 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
347 key.signal_id = signal_id;
351 hlbsa = g_bsearch_array_new (&g_signal_hlbsa_bconfig);
352 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key, FALSE);
353 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
357 GBSearchArray *o = hlbsa;
359 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key, FALSE);
361 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
363 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
366 static inline HandlerList*
367 handler_list_lookup (guint signal_id,
370 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
373 key.signal_id = signal_id;
375 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
379 handler_lookup (gpointer instance,
383 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
389 for (i = 0; i < hlbsa->n_nodes; i++)
391 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
394 for (handler = hlist->handlers; handler; handler = handler->next)
395 if (handler->sequential_number == handler_id)
398 *signal_id_p = hlist->signal_id;
408 static inline HandlerMatch*
409 handler_match_prepend (HandlerMatch *list,
415 /* yeah, we could use our own memchunk here, introducing yet more
416 * rarely used cached nodes and extra allocation overhead.
417 * instead, we use GList* nodes, since they are exactly the size
418 * we need and are already cached. g_signal_init() asserts this.
420 node = (HandlerMatch*) g_list_alloc ();
421 node->handler = handler;
423 node->d.signal_id = signal_id;
424 handler_ref (handler);
428 static inline HandlerMatch*
429 handler_match_free1_R (HandlerMatch *node,
432 HandlerMatch *next = node->next;
434 handler_unref_R (node->d.signal_id, instance, node->handler);
435 g_list_free_1 ((GList*) node);
441 handlers_find (gpointer instance,
442 GSignalMatchType mask,
448 gboolean one_and_only)
450 HandlerMatch *mlist = NULL;
452 if (mask & G_SIGNAL_MATCH_ID)
454 HandlerList *hlist = handler_list_lookup (signal_id, instance);
456 SignalNode *node = NULL;
458 if (mask & G_SIGNAL_MATCH_FUNC)
460 node = LOOKUP_SIGNAL_NODE (signal_id);
461 if (!node || !node->c_marshaller)
466 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
467 if (handler->sequential_number &&
468 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
469 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
470 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
471 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
472 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
473 handler->closure->meta_marshal == 0 &&
474 ((GCClosure*) handler->closure)->callback == func)))
476 mlist = handler_match_prepend (mlist, handler, signal_id);
483 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
490 for (i = 0; i < hlbsa->n_nodes; i++)
492 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
493 SignalNode *node = NULL;
496 if (!(mask & G_SIGNAL_MATCH_FUNC))
498 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
499 if (!node->c_marshaller)
503 for (handler = hlist->handlers; handler; handler = handler->next)
504 if (handler->sequential_number &&
505 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
506 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
507 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
508 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
509 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
510 handler->closure->meta_marshal == 0 &&
511 ((GCClosure*) handler->closure)->callback == func)))
513 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
524 static inline Handler*
525 handler_new (gboolean after)
527 Handler *handler = g_generic_node_alloc (&g_handler_ts,
530 #ifndef G_DISABLE_CHECKS
531 if (g_handler_sequential_number < 1)
532 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
535 handler->sequential_number = g_handler_sequential_number++;
536 handler->prev = NULL;
537 handler->next = NULL;
539 handler->ref_count = 1;
540 handler->block_count = 0;
541 handler->after = after != FALSE;
542 handler->closure = NULL;
548 handler_ref (Handler *handler)
550 g_return_if_fail (handler->ref_count > 0);
552 #ifndef G_DISABLE_CHECKS
553 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
554 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
557 handler->ref_count += 1;
561 handler_unref_R (guint signal_id,
565 g_return_if_fail (handler->ref_count > 0);
567 handler->ref_count -= 1;
568 if (!handler->ref_count)
571 handler->next->prev = handler->prev;
572 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
573 handler->prev->next = handler->next;
576 HandlerList *hlist = handler_list_lookup (signal_id, instance);
578 hlist->handlers = handler->next;
581 g_closure_unref (handler->closure);
583 g_generic_node_free (&g_handler_ts, handler);
588 handler_insert (guint signal_id,
594 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
596 hlist = handler_list_ensure (signal_id, instance);
597 if (!hlist->handlers)
598 hlist->handlers = handler;
599 else if (hlist->handlers->after && !handler->after)
601 handler->next = hlist->handlers;
602 hlist->handlers->prev = handler;
603 hlist->handlers = handler;
607 Handler *tmp = hlist->handlers;
613 while (tmp->next && !tmp->next->after)
616 tmp->next->prev = handler;
617 handler->next = tmp->next;
624 emission_push (Emission **emission_list_p,
627 emission->next = *emission_list_p;
628 *emission_list_p = emission;
632 emission_pop (Emission **emission_list_p,
635 Emission *node, *last = NULL;
637 for (node = *emission_list_p; node; last = node, node = last->next)
638 if (node == emission)
641 last->next = node->next;
643 *emission_list_p = node->next;
646 g_assert_not_reached ();
649 static inline Emission*
650 emission_find (Emission *emission_list,
657 for (emission = emission_list; emission; emission = emission->next)
658 if (emission->instance == instance &&
659 emission->ihint.signal_id == signal_id &&
660 emission->ihint.detail == detail)
665 static inline Emission*
666 emission_find_innermost (gpointer instance)
668 Emission *emission, *s = NULL, *c = NULL;
670 for (emission = g_restart_emissions; emission; emission = emission->next)
671 if (emission->instance == instance)
676 for (emission = g_recursive_emissions; emission; emission = emission->next)
677 if (emission->instance == instance)
687 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
691 signal_key_cmp (gconstpointer node1,
694 const SignalKey *key1 = node1, *key2 = node2;
696 if (key1->itype == key2->itype)
697 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
699 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
703 g_signal_init (void) /* sync with gtype.c */
706 if (!g_n_signal_nodes)
708 /* handler_id_node_prepend() requires this */
709 g_assert (sizeof (GList) == sizeof (HandlerMatch));
711 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
712 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
713 g_signal_key_bsa = g_bsearch_array_new (&g_signal_key_bconfig);
715 /* invalid (0) signal_id */
716 g_n_signal_nodes = 1;
717 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
718 g_signal_nodes[0] = NULL;
724 _g_signals_destroy (GType itype)
729 for (i = 1; i < g_n_signal_nodes; i++)
731 SignalNode *node = g_signal_nodes[i];
733 if (node->itype == itype)
736 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
738 type_debug_name (node->itype));
740 signal_destroy_R (node);
747 g_signal_stop_emission (gpointer instance,
753 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
754 g_return_if_fail (signal_id > 0);
757 node = LOOKUP_SIGNAL_NODE (signal_id);
758 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
760 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
764 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
766 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
767 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
771 if (emission->state == EMISSION_HOOK)
772 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
773 node->name, instance);
774 else if (emission->state == EMISSION_RUN)
775 emission->state = EMISSION_STOP;
778 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
779 node->name, instance);
782 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
787 signal_finalize_hook (GHookList *hook_list,
790 GDestroyNotify destroy = hook->destroy;
794 hook->destroy = NULL;
796 destroy (hook->data);
802 g_signal_add_emission_hook (guint signal_id,
804 GSignalEmissionHook hook_func,
806 GDestroyNotify data_destroy)
808 static gulong seq_hook_id = 1;
811 SignalHook *signal_hook;
813 g_return_val_if_fail (signal_id > 0, 0);
814 g_return_val_if_fail (hook_func != NULL, 0);
817 node = LOOKUP_SIGNAL_NODE (signal_id);
818 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
820 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
824 if (detail && !(node->flags & G_SIGNAL_DETAILED))
826 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
830 if (!node->emission_hooks)
832 node->emission_hooks = g_new (GHookList, 1);
833 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
834 node->emission_hooks->finalize_hook = signal_finalize_hook;
836 hook = g_hook_alloc (node->emission_hooks);
837 hook->data = hook_data;
838 hook->func = (gpointer) hook_func;
839 hook->destroy = data_destroy;
840 signal_hook = SIGNAL_HOOK (hook);
841 signal_hook->detail = detail;
842 node->emission_hooks->seq_id = seq_hook_id;
843 g_hook_append (node->emission_hooks, hook);
844 seq_hook_id = node->emission_hooks->seq_id;
847 return hook->hook_id;
851 g_signal_remove_emission_hook (guint signal_id,
856 g_return_if_fail (signal_id > 0);
857 g_return_if_fail (hook_id > 0);
860 node = LOOKUP_SIGNAL_NODE (signal_id);
861 if (!node || node->destroyed)
862 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
863 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
864 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
869 signal_parse_name (const gchar *name,
872 gboolean force_quark)
874 const gchar *colon = strchr (name, ':');
879 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
880 if (signal_id && detail_p)
883 else if (colon[1] == ':')
886 guint l = colon - name;
890 memcpy (buffer, name, l);
892 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
896 gchar *signal = g_new (gchar, l + 1);
898 memcpy (signal, name, l);
900 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
904 if (signal_id && detail_p)
905 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
913 g_signal_parse_name (const gchar *detailed_signal,
917 gboolean force_detail_quark)
923 g_return_val_if_fail (detailed_signal != NULL, FALSE);
924 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
927 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
930 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
931 if (!node || node->destroyed ||
932 (detail && !(node->flags & G_SIGNAL_DETAILED)))
936 *signal_id_p = signal_id;
944 g_signal_stop_emission_by_name (gpointer instance,
945 const gchar *detailed_signal)
951 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
952 g_return_if_fail (detailed_signal != NULL);
955 itype = G_TYPE_FROM_INSTANCE (instance);
956 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
959 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
961 if (detail && !(node->flags & G_SIGNAL_DETAILED))
962 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
963 else if (!g_type_is_a (itype, node->itype))
964 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
967 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
968 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
972 if (emission->state == EMISSION_HOOK)
973 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
974 node->name, instance);
975 else if (emission->state == EMISSION_RUN)
976 emission->state = EMISSION_STOP;
979 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
980 node->name, instance);
984 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
989 g_signal_lookup (const gchar *name,
993 g_return_val_if_fail (name != NULL, 0);
994 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
997 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1001 /* give elaborate warnings */
1002 if (!g_type_name (itype))
1003 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1005 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1006 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1007 name, g_type_name (itype));
1008 else if (!g_type_class_peek (itype))
1009 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1010 name, g_type_name (itype));
1017 g_signal_list_ids (GType itype,
1025 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1026 g_return_val_if_fail (n_ids != NULL, NULL);
1029 keys = G_BSEARCH_ARRAY_NODES (g_signal_key_bsa);
1030 n_nodes = g_signal_key_bsa->n_nodes;
1031 result = g_array_new (FALSE, FALSE, sizeof (guint));
1033 for (i = 0; i < n_nodes; i++)
1034 if (keys[i].itype == itype)
1036 const gchar *name = g_quark_to_string (keys[i].quark);
1038 /* Signal names with "_" in them are aliases to the same
1039 * name with "-" instead of "_".
1041 if (!strchr (name, '_'))
1042 g_array_append_val (result, keys[i].signal_id);
1044 *n_ids = result->len;
1048 /* give elaborate warnings */
1049 if (!g_type_name (itype))
1050 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1052 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1053 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1054 g_type_name (itype));
1055 else if (!g_type_class_peek (itype))
1056 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1057 g_type_name (itype));
1060 return (guint*) g_array_free (result, FALSE);
1063 G_CONST_RETURN gchar*
1064 g_signal_name (guint signal_id)
1070 node = LOOKUP_SIGNAL_NODE (signal_id);
1071 name = node ? node->name : NULL;
1078 g_signal_query (guint signal_id,
1079 GSignalQuery *query)
1083 g_return_if_fail (query != NULL);
1086 node = LOOKUP_SIGNAL_NODE (signal_id);
1087 if (!node || node->destroyed)
1088 query->signal_id = 0;
1091 query->signal_id = node->signal_id;
1092 query->signal_name = node->name;
1093 query->itype = node->itype;
1094 query->signal_flags = node->flags;
1095 query->return_type = node->return_type;
1096 query->n_params = node->n_params;
1097 query->param_types = node->param_types;
1103 g_signal_new (const gchar *signal_name,
1105 GSignalFlags signal_flags,
1107 GSignalAccumulator accumulator,
1109 GSignalCMarshaller c_marshaller,
1117 g_return_val_if_fail (signal_name != NULL, 0);
1119 va_start (args, n_params);
1121 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1122 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1123 accumulator, accu_data, c_marshaller,
1124 return_type, n_params, args);
1131 static inline ClassClosure*
1132 signal_find_class_closure (SignalNode *node,
1135 GBSearchArray *bsa = node->class_closure_bsa;
1142 /* cc->instance_type is 0 for default closure */
1144 key.instance_type = itype;
1145 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1146 while (!cc && key.instance_type)
1148 key.instance_type = g_type_parent (key.instance_type);
1149 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1157 static inline GClosure*
1158 signal_lookup_closure (SignalNode *node,
1159 GTypeInstance *instance)
1163 if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1164 cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1166 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1167 return cc ? cc->closure : NULL;
1171 signal_add_class_closure (SignalNode *node,
1177 if (!node->class_closure_bsa)
1178 node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1179 key.instance_type = itype;
1180 key.closure = g_closure_ref (closure);
1181 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1182 &g_class_closure_bconfig,
1185 g_closure_sink (closure);
1186 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1187 g_closure_set_marshal (closure, node->c_marshaller);
1191 g_signal_newv (const gchar *signal_name,
1193 GSignalFlags signal_flags,
1194 GClosure *class_closure,
1195 GSignalAccumulator accumulator,
1197 GSignalCMarshaller c_marshaller,
1206 g_return_val_if_fail (signal_name != NULL, 0);
1207 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1209 g_return_val_if_fail (param_types != NULL, 0);
1210 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1211 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1212 g_return_val_if_fail (accumulator == NULL, 0);
1214 g_return_val_if_fail (accu_data == NULL, 0);
1216 name = g_strdup (signal_name);
1217 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1221 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1222 node = LOOKUP_SIGNAL_NODE (signal_id);
1223 if (node && !node->destroyed)
1225 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1227 type_debug_name (node->itype),
1228 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1233 if (node && node->itype != itype)
1235 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1237 type_debug_name (itype),
1238 type_debug_name (node->itype));
1243 for (i = 0; i < n_params; i++)
1244 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1246 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1247 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1252 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1254 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1255 type_debug_name (return_type), type_debug_name (itype), name);
1260 if (return_type != G_TYPE_NONE &&
1261 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1263 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1264 type_debug_name (itype), name, type_debug_name (return_type));
1270 /* setup permanent portion of signal node */
1275 signal_id = g_n_signal_nodes++;
1276 node = g_new (SignalNode, 1);
1277 node->signal_id = signal_id;
1278 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1279 g_signal_nodes[signal_id] = node;
1280 node->itype = itype;
1283 key.quark = g_quark_from_string (node->name);
1284 key.signal_id = signal_id;
1285 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1286 g_strdelimit (node->name, "_", '-');
1287 key.quark = g_quark_from_static_string (node->name);
1288 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1290 node->destroyed = FALSE;
1292 /* setup reinitializable portion */
1293 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1294 node->n_params = n_params;
1295 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1296 node->return_type = return_type;
1297 node->class_closure_bsa = NULL;
1300 node->accumulator = g_new (SignalAccumulator, 1);
1301 node->accumulator->func = accumulator;
1302 node->accumulator->data = accu_data;
1305 node->accumulator = NULL;
1306 node->c_marshaller = c_marshaller;
1307 node->emission_hooks = NULL;
1309 signal_add_class_closure (node, 0, class_closure);
1316 g_signal_new_valist (const gchar *signal_name,
1318 GSignalFlags signal_flags,
1319 GClosure *class_closure,
1320 GSignalAccumulator accumulator,
1322 GSignalCMarshaller c_marshaller,
1333 param_types = g_new (GType, n_params);
1335 for (i = 0; i < n_params; i++)
1336 param_types[i] = va_arg (args, GType);
1341 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1342 class_closure, accumulator, accu_data, c_marshaller,
1343 return_type, n_params, param_types);
1344 g_free (param_types);
1350 signal_destroy_R (SignalNode *signal_node)
1352 SignalNode node = *signal_node;
1354 signal_node->destroyed = TRUE;
1356 /* reentrancy caution, zero out real contents first */
1357 signal_node->n_params = 0;
1358 signal_node->param_types = NULL;
1359 signal_node->return_type = 0;
1360 signal_node->class_closure_bsa = NULL;
1361 signal_node->accumulator = NULL;
1362 signal_node->c_marshaller = NULL;
1363 signal_node->emission_hooks = NULL;
1365 #ifdef G_ENABLE_DEBUG
1366 /* check current emissions */
1370 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1371 emission; emission = emission->next)
1372 if (emission->ihint.signal_id == node.signal_id)
1373 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1374 node.name, emission->instance);
1378 /* free contents that need to
1381 g_free (node.param_types);
1382 if (node.class_closure_bsa)
1386 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1388 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1390 g_closure_unref (cc->closure);
1392 g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1394 g_free (node.accumulator);
1395 if (node.emission_hooks)
1397 g_hook_list_clear (node.emission_hooks);
1398 g_free (node.emission_hooks);
1404 g_signal_override_class_closure (guint signal_id,
1405 GType instance_type,
1406 GClosure *class_closure)
1410 g_return_if_fail (signal_id > 0);
1411 g_return_if_fail (class_closure != NULL);
1414 node = LOOKUP_SIGNAL_NODE (signal_id);
1415 if (!g_type_is_a (instance_type, node->itype))
1416 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1419 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1421 if (cc && cc->instance_type == instance_type)
1422 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1424 signal_add_class_closure (node, instance_type, class_closure);
1430 g_signal_chain_from_overridden (const GValue *instance_and_params,
1431 GValue *return_value)
1433 GType chain_type = 0, restore_type = 0;
1434 Emission *emission = NULL;
1435 GClosure *closure = NULL;
1439 g_return_if_fail (instance_and_params != NULL);
1440 instance = g_value_peek_pointer (instance_and_params);
1441 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1444 emission = emission_find_innermost (instance);
1447 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1449 g_assert (node != NULL); /* paranoid */
1451 /* we should probably do the same parameter checks as g_signal_emit() here.
1453 if (emission->chain_type != G_TYPE_NONE)
1455 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1457 g_assert (cc != NULL); /* closure currently in call stack */
1459 n_params = node->n_params;
1460 restore_type = cc->instance_type;
1461 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1462 if (cc && cc->instance_type != restore_type)
1464 closure = cc->closure;
1465 chain_type = cc->instance_type;
1469 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1472 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1475 emission->chain_type = chain_type;
1477 g_closure_invoke (closure,
1480 instance_and_params,
1483 emission->chain_type = restore_type;
1488 GSignalInvocationHint*
1489 g_signal_get_invocation_hint (gpointer instance)
1491 Emission *emission = NULL;
1493 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1496 emission = emission_find_innermost (instance);
1499 return emission ? &emission->ihint : NULL;
1503 g_signal_connect_closure_by_id (gpointer instance,
1510 gulong handler_seq_no = 0;
1512 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1513 g_return_val_if_fail (signal_id > 0, 0);
1514 g_return_val_if_fail (closure != NULL, 0);
1517 node = LOOKUP_SIGNAL_NODE (signal_id);
1520 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1521 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1522 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1523 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1526 Handler *handler = handler_new (after);
1528 handler_seq_no = handler->sequential_number;
1529 handler->detail = detail;
1530 handler->closure = g_closure_ref (closure);
1531 g_closure_sink (closure);
1532 handler_insert (signal_id, instance, handler);
1533 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1534 g_closure_set_marshal (closure, node->c_marshaller);
1538 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1541 return handler_seq_no;
1545 g_signal_connect_closure (gpointer instance,
1546 const gchar *detailed_signal,
1551 gulong handler_seq_no = 0;
1555 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1556 g_return_val_if_fail (detailed_signal != NULL, 0);
1557 g_return_val_if_fail (closure != NULL, 0);
1560 itype = G_TYPE_FROM_INSTANCE (instance);
1561 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1564 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1566 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1567 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1568 else if (!g_type_is_a (itype, node->itype))
1569 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1572 Handler *handler = handler_new (after);
1574 handler_seq_no = handler->sequential_number;
1575 handler->detail = detail;
1576 handler->closure = g_closure_ref (closure);
1577 g_closure_sink (closure);
1578 handler_insert (signal_id, instance, handler);
1579 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1580 g_closure_set_marshal (handler->closure, node->c_marshaller);
1584 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1587 return handler_seq_no;
1591 g_signal_connect_data (gpointer instance,
1592 const gchar *detailed_signal,
1593 GCallback c_handler,
1595 GClosureNotify destroy_data,
1596 GConnectFlags connect_flags)
1599 gulong handler_seq_no = 0;
1602 gboolean swapped, after;
1604 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1605 g_return_val_if_fail (detailed_signal != NULL, 0);
1606 g_return_val_if_fail (c_handler != NULL, 0);
1608 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1609 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1612 itype = G_TYPE_FROM_INSTANCE (instance);
1613 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1616 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1618 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1619 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1620 else if (!g_type_is_a (itype, node->itype))
1621 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1624 Handler *handler = handler_new (after);
1626 handler_seq_no = handler->sequential_number;
1627 handler->detail = detail;
1628 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1629 g_closure_sink (handler->closure);
1630 handler_insert (signal_id, instance, handler);
1631 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1632 g_closure_set_marshal (handler->closure, node->c_marshaller);
1636 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1639 return handler_seq_no;
1643 g_signal_handler_block (gpointer instance,
1648 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1649 g_return_if_fail (handler_id > 0);
1652 handler = handler_lookup (instance, handler_id, NULL);
1655 #ifndef G_DISABLE_CHECKS
1656 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1657 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1659 handler->block_count += 1;
1662 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1667 g_signal_handler_unblock (gpointer instance,
1672 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1673 g_return_if_fail (handler_id > 0);
1676 handler = handler_lookup (instance, handler_id, NULL);
1679 if (handler->block_count)
1680 handler->block_count -= 1;
1682 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1685 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1690 g_signal_handler_disconnect (gpointer instance,
1696 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1697 g_return_if_fail (handler_id > 0);
1700 handler = handler_lookup (instance, handler_id, &signal_id);
1703 handler->sequential_number = 0;
1704 handler->block_count = 1;
1705 handler_unref_R (signal_id, instance, handler);
1708 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1713 g_signal_handler_is_connected (gpointer instance,
1719 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1720 g_return_val_if_fail (handler_id > 0, FALSE);
1723 handler = handler_lookup (instance, handler_id, NULL);
1724 connected = handler != NULL;
1731 g_signal_handlers_destroy (gpointer instance)
1733 GBSearchArray *hlbsa;
1735 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1738 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1743 /* reentrancy caution, delete instance trace first */
1744 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1746 for (i = 0; i < hlbsa->n_nodes; i++)
1748 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1749 Handler *handler = hlist->handlers;
1753 Handler *tmp = handler;
1755 handler = tmp->next;
1756 tmp->block_count = 1;
1757 /* cruel unlink, this works because _all_ handlers vanish */
1760 if (tmp->sequential_number)
1762 tmp->sequential_number = 0;
1763 handler_unref_R (0, NULL, tmp);
1767 g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
1773 g_signal_handler_find (gpointer instance,
1774 GSignalMatchType mask,
1781 gulong handler_seq_no = 0;
1783 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1784 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1786 if (mask & G_SIGNAL_MATCH_MASK)
1788 HandlerMatch *mlist;
1791 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1794 handler_seq_no = mlist->handler->sequential_number;
1795 handler_match_free1_R (mlist, instance);
1800 return handler_seq_no;
1804 signal_handlers_foreach_matched_R (gpointer instance,
1805 GSignalMatchType mask,
1811 void (*callback) (gpointer instance,
1812 gulong handler_seq_no))
1814 HandlerMatch *mlist;
1815 guint n_handlers = 0;
1817 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1821 if (mlist->handler->sequential_number)
1824 callback (instance, mlist->handler->sequential_number);
1827 mlist = handler_match_free1_R (mlist, instance);
1834 g_signal_handlers_block_matched (gpointer instance,
1835 GSignalMatchType mask,
1842 guint n_handlers = 0;
1844 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1845 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1847 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1850 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1851 closure, func, data,
1852 g_signal_handler_block);
1860 g_signal_handlers_unblock_matched (gpointer instance,
1861 GSignalMatchType mask,
1868 guint n_handlers = 0;
1870 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1871 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1873 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1876 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1877 closure, func, data,
1878 g_signal_handler_unblock);
1886 g_signal_handlers_disconnect_matched (gpointer instance,
1887 GSignalMatchType mask,
1894 guint n_handlers = 0;
1896 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1897 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1899 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1902 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1903 closure, func, data,
1904 g_signal_handler_disconnect);
1912 g_signal_has_handler_pending (gpointer instance,
1915 gboolean may_be_blocked)
1917 HandlerMatch *mlist;
1918 gboolean has_pending;
1920 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1921 g_return_val_if_fail (signal_id > 0, FALSE);
1926 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1928 if (!(node->flags & G_SIGNAL_DETAILED))
1930 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1935 mlist = handlers_find (instance,
1936 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1937 signal_id, detail, NULL, NULL, NULL, TRUE);
1941 handler_match_free1_R (mlist, instance);
1944 has_pending = FALSE;
1951 g_signal_emitv (const GValue *instance_and_params,
1954 GValue *return_value)
1956 const GValue *param_values;
1959 #ifdef G_ENABLE_DEBUG
1963 g_return_if_fail (instance_and_params != NULL);
1964 instance = g_value_peek_pointer (instance_and_params);
1965 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1966 g_return_if_fail (signal_id > 0);
1968 param_values = instance_and_params + 1;
1971 node = LOOKUP_SIGNAL_NODE (signal_id);
1972 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1974 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1978 #ifdef G_ENABLE_DEBUG
1979 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1981 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1985 for (i = 0; i < node->n_params; i++)
1986 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1988 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1990 type_debug_name (node->param_types[i]),
1993 G_VALUE_TYPE_NAME (param_values + i));
1997 if (node->return_type != G_TYPE_NONE)
2001 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2003 type_debug_name (node->return_type),
2008 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2010 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2012 type_debug_name (node->return_type),
2014 G_VALUE_TYPE_NAME (return_value));
2020 return_value = NULL;
2021 #endif /* G_ENABLE_DEBUG */
2024 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2028 g_signal_emit_valist (gpointer instance,
2033 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2034 GType signal_return_type;
2035 GValue *param_values;
2039 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2040 g_return_if_fail (signal_id > 0);
2043 node = LOOKUP_SIGNAL_NODE (signal_id);
2044 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2046 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2050 #ifndef G_DISABLE_CHECKS
2051 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2053 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2057 #endif /* !G_DISABLE_CHECKS */
2059 n_params = node->n_params;
2060 signal_return_type = node->return_type;
2061 if (node->n_params < MAX_STACK_VALUES)
2062 instance_and_params = stack_values;
2065 free_me = g_new (GValue, node->n_params + 1);
2066 instance_and_params = free_me;
2068 param_values = instance_and_params + 1;
2069 for (i = 0; i < node->n_params; i++)
2072 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2073 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2075 param_values[i].g_type = 0;
2077 g_value_init (param_values + i, ptype);
2078 G_VALUE_COLLECT (param_values + i,
2080 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2084 g_warning ("%s: %s", G_STRLOC, error);
2087 /* we purposely leak the value here, it might not be
2088 * in a sane state if an error condition occoured
2091 g_value_unset (param_values + i);
2099 instance_and_params->g_type = 0;
2100 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2101 g_value_set_instance (instance_and_params, instance);
2102 if (signal_return_type == G_TYPE_NONE)
2103 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2106 GValue return_value = { 0, };
2107 gchar *error = NULL;
2108 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2109 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2111 g_value_init (&return_value, rtype);
2113 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2115 G_VALUE_LCOPY (&return_value,
2117 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2120 g_value_unset (&return_value);
2123 g_warning ("%s: %s", G_STRLOC, error);
2126 /* we purposely leak the value here, it might not be
2127 * in a sane state if an error condition occoured
2131 for (i = 0; i < n_params; i++)
2132 g_value_unset (param_values + i);
2133 g_value_unset (instance_and_params);
2139 g_signal_emit (gpointer instance,
2146 va_start (var_args, detail);
2147 g_signal_emit_valist (instance, signal_id, detail, var_args);
2152 g_signal_emit_by_name (gpointer instance,
2153 const gchar *detailed_signal,
2159 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2160 g_return_if_fail (detailed_signal != NULL);
2163 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2170 va_start (var_args, detailed_signal);
2171 g_signal_emit_valist (instance, signal_id, detail, var_args);
2175 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2178 static inline gboolean
2179 accumulate (GSignalInvocationHint *ihint,
2180 GValue *return_accu,
2181 GValue *handler_return,
2182 SignalAccumulator *accumulator)
2184 gboolean continue_emission;
2189 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2190 g_value_reset (handler_return);
2192 return continue_emission;
2196 signal_emit_unlocked_R (SignalNode *node,
2199 GValue *emission_return,
2200 const GValue *instance_and_params)
2202 SignalAccumulator *accumulator;
2204 GClosure *class_closure;
2206 Handler *handler_list = NULL;
2207 GValue *return_accu, accu = { 0, };
2209 gulong max_sequential_handler_number;
2210 gboolean return_value_altered = FALSE;
2212 #ifdef G_ENABLE_DEBUG
2213 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2215 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2216 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2219 if (g_trap_instance_signals == instance)
2222 #endif /* G_ENABLE_DEBUG */
2225 signal_id = node->signal_id;
2226 if (node->flags & G_SIGNAL_NO_RECURSE)
2228 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2232 node->state = EMISSION_RESTART;
2234 return return_value_altered;
2237 accumulator = node->accumulator;
2241 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2242 return_accu = &accu;
2246 return_accu = emission_return;
2247 emission.instance = instance;
2248 emission.ihint.signal_id = node->signal_id;
2249 emission.ihint.detail = detail;
2250 emission.ihint.run_type = 0;
2252 emission.chain_type = G_TYPE_NONE;
2253 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2254 class_closure = signal_lookup_closure (node, instance);
2259 handler_unref_R (signal_id, instance, handler_list);
2260 max_sequential_handler_number = g_handler_sequential_number;
2261 hlist = handler_list_lookup (signal_id, instance);
2262 handler_list = hlist ? hlist->handlers : NULL;
2264 handler_ref (handler_list);
2266 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2268 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2270 emission.state = EMISSION_RUN;
2272 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2274 g_closure_invoke (class_closure,
2277 instance_and_params,
2279 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2280 emission.state == EMISSION_RUN)
2281 emission.state = EMISSION_STOP;
2283 emission.chain_type = G_TYPE_NONE;
2284 return_value_altered = TRUE;
2286 if (emission.state == EMISSION_STOP)
2288 else if (emission.state == EMISSION_RESTART)
2292 if (node->emission_hooks)
2294 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2297 emission.state = EMISSION_HOOK;
2298 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2301 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2303 if (!signal_hook->detail || signal_hook->detail == detail)
2305 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2307 was_in_call = G_HOOK_IN_CALL (hook);
2308 hook->flags |= G_HOOK_FLAG_IN_CALL;
2310 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2313 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2315 g_hook_destroy_link (node->emission_hooks, hook);
2317 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2320 if (emission.state == EMISSION_RESTART)
2326 Handler *handler = handler_list;
2328 emission.state = EMISSION_RUN;
2329 handler_ref (handler);
2336 handler_unref_R (signal_id, instance, handler_list);
2337 handler_list = handler;
2340 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2341 handler->sequential_number < max_sequential_handler_number)
2344 g_closure_invoke (handler->closure,
2347 instance_and_params,
2349 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2350 emission.state == EMISSION_RUN)
2351 emission.state = EMISSION_STOP;
2353 return_value_altered = TRUE;
2355 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2358 tmp = handler->next;
2362 handler_unref_R (signal_id, instance, handler_list);
2363 handler_list = handler;
2368 if (emission.state == EMISSION_STOP)
2370 else if (emission.state == EMISSION_RESTART)
2374 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2376 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2378 emission.state = EMISSION_RUN;
2380 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2382 g_closure_invoke (class_closure,
2385 instance_and_params,
2387 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2388 emission.state == EMISSION_RUN)
2389 emission.state = EMISSION_STOP;
2391 emission.chain_type = G_TYPE_NONE;
2392 return_value_altered = TRUE;
2394 if (emission.state == EMISSION_STOP)
2396 else if (emission.state == EMISSION_RESTART)
2402 Handler *handler = handler_list;
2404 emission.state = EMISSION_RUN;
2405 handler_ref (handler);
2410 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2411 handler->sequential_number < max_sequential_handler_number)
2414 g_closure_invoke (handler->closure,
2417 instance_and_params,
2419 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2420 emission.state == EMISSION_RUN)
2421 emission.state = EMISSION_STOP;
2423 return_value_altered = TRUE;
2425 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2428 tmp = handler->next;
2432 handler_unref_R (signal_id, instance, handler);
2437 if (emission.state == EMISSION_STOP)
2439 else if (emission.state == EMISSION_RESTART)
2445 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2447 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2449 gboolean need_unset = FALSE;
2451 emission.state = EMISSION_STOP;
2453 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2455 if (node->return_type != G_TYPE_NONE && !accumulator)
2457 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2460 g_closure_invoke (class_closure,
2461 node->return_type != G_TYPE_NONE ? &accu : NULL,
2463 instance_and_params,
2466 g_value_unset (&accu);
2468 emission.chain_type = G_TYPE_NONE;
2470 if (emission.state == EMISSION_RESTART)
2475 handler_unref_R (signal_id, instance, handler_list);
2477 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2480 g_value_unset (&accu);
2482 return return_value_altered;
2486 type_debug_name (GType type)
2490 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2491 return name ? name : "<unknown>";
2497 /* --- compile standard marshallers --- */
2498 #include "gobject.h"
2500 #include "gmarshal.c"