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:
748 * @instance: the object whose signal handlers you wish to stop.
749 * @signal_id: the signal identifier, as returned by g_signal_lookup().
750 * @detail: the detail which the signal was emitted with.
752 * Stops a signal's current emission.
754 * This will prevent the default method from running, if the signal was
755 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
758 * Prints a warning if used on a signal which isn't being emitted.
761 g_signal_stop_emission (gpointer instance,
767 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
768 g_return_if_fail (signal_id > 0);
771 node = LOOKUP_SIGNAL_NODE (signal_id);
772 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
774 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
778 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
780 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
781 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
785 if (emission->state == EMISSION_HOOK)
786 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
787 node->name, instance);
788 else if (emission->state == EMISSION_RUN)
789 emission->state = EMISSION_STOP;
792 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
793 node->name, instance);
796 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
801 signal_finalize_hook (GHookList *hook_list,
804 GDestroyNotify destroy = hook->destroy;
808 hook->destroy = NULL;
810 destroy (hook->data);
816 * g_signal_add_emission_hook:
817 * @signal_id: the signal identifier, as returned by g_signal_lookup().
818 * @detail: the detail on which to call the hook.
819 * @hook_func: a #GSignalEmissionHook function.
820 * @hook_data: user data for @hook_func.
821 * @data_destroy: a #GDestroyNotify for @hook_data.
823 * Adds an emission hook for a signal, which will get called for any emission
824 * of that signal, independent of the instance.
826 * Return value: the hook id, for later use with
827 * g_signal_remove_emission_hook().
830 g_signal_add_emission_hook (guint signal_id,
832 GSignalEmissionHook hook_func,
834 GDestroyNotify data_destroy)
836 static gulong seq_hook_id = 1;
839 SignalHook *signal_hook;
841 g_return_val_if_fail (signal_id > 0, 0);
842 g_return_val_if_fail (hook_func != NULL, 0);
845 node = LOOKUP_SIGNAL_NODE (signal_id);
846 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
848 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
852 if (detail && !(node->flags & G_SIGNAL_DETAILED))
854 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
858 if (!node->emission_hooks)
860 node->emission_hooks = g_new (GHookList, 1);
861 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
862 node->emission_hooks->finalize_hook = signal_finalize_hook;
864 hook = g_hook_alloc (node->emission_hooks);
865 hook->data = hook_data;
866 hook->func = (gpointer) hook_func;
867 hook->destroy = data_destroy;
868 signal_hook = SIGNAL_HOOK (hook);
869 signal_hook->detail = detail;
870 node->emission_hooks->seq_id = seq_hook_id;
871 g_hook_append (node->emission_hooks, hook);
872 seq_hook_id = node->emission_hooks->seq_id;
875 return hook->hook_id;
879 * g_signal_remove_emission_hook:
880 * @signal_id: the id of the signal
881 * @hook_id: the id of the emission hook, as returned by
882 * g_signal_add_emission_hook()
884 * Deletes an emission hook.
887 g_signal_remove_emission_hook (guint signal_id,
892 g_return_if_fail (signal_id > 0);
893 g_return_if_fail (hook_id > 0);
896 node = LOOKUP_SIGNAL_NODE (signal_id);
897 if (!node || node->destroyed)
898 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
899 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
900 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
905 signal_parse_name (const gchar *name,
908 gboolean force_quark)
910 const gchar *colon = strchr (name, ':');
915 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
916 if (signal_id && detail_p)
919 else if (colon[1] == ':')
922 guint l = colon - name;
926 memcpy (buffer, name, l);
928 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
932 gchar *signal = g_new (gchar, l + 1);
934 memcpy (signal, name, l);
936 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
940 if (signal_id && detail_p)
941 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
949 * g_signal_parse_name:
950 * @detailed_signal: a string of the form "signal-name::detail".
951 * @itype: The interface/instance type that introduced "signal-name".
952 * @signal_id_p: Location to store the signal id.
953 * @detail_p: Location to store the detail quark.
954 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
956 * Internal function to parse a signal name into its @signal_id
959 * Return value: Whether the signal name could successfully be parsed and
960 * @signal_id_p and @detail_p contain valid return values.
963 g_signal_parse_name (const gchar *detailed_signal,
967 gboolean force_detail_quark)
973 g_return_val_if_fail (detailed_signal != NULL, FALSE);
974 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
977 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
980 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
981 if (!node || node->destroyed ||
982 (detail && !(node->flags & G_SIGNAL_DETAILED)))
986 *signal_id_p = signal_id;
994 * g_signal_stop_emission_by_name:
995 * @instance: the object whose signal handlers you wish to stop.
996 * @detailed_signal: a string of the form "signal-name::detail".
998 * Stops a signal's current emission.
1000 * This is just like g_signal_stop_emission() except it will look up the
1001 * signal id for you.
1004 g_signal_stop_emission_by_name (gpointer instance,
1005 const gchar *detailed_signal)
1011 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1012 g_return_if_fail (detailed_signal != NULL);
1015 itype = G_TYPE_FROM_INSTANCE (instance);
1016 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1019 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1021 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1022 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1023 else if (!g_type_is_a (itype, node->itype))
1024 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1027 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1028 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1032 if (emission->state == EMISSION_HOOK)
1033 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1034 node->name, instance);
1035 else if (emission->state == EMISSION_RUN)
1036 emission->state = EMISSION_STOP;
1039 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1040 node->name, instance);
1044 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1050 * @name: the signal's name.
1051 * @itype: the type that the signal operates on.
1053 * Given the name of the signal and the type of object it connects to, gets
1054 * the signal's identifying integer. Emitting the signal by number is
1055 * somewhat faster than using the name each time.
1057 * Also tries the ancestors of the given type.
1059 * Return value: the signal's identifying number, or 0 if no signal was found.
1062 g_signal_lookup (const gchar *name,
1066 g_return_val_if_fail (name != NULL, 0);
1067 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1070 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1074 /* give elaborate warnings */
1075 if (!g_type_name (itype))
1076 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1078 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1079 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1080 name, g_type_name (itype));
1081 else if (!g_type_class_peek (itype))
1082 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1083 name, g_type_name (itype));
1090 * g_signal_list_ids:
1091 * @itype: Instance or interface type.
1092 * @n_ids: Location to store the number of signal ids for @itype.
1094 * Lists the signals by id that a certain instance or interface type
1095 * created. Further information about the signals can be acquired through
1098 * Return value: Newly allocated array of signal IDs.
1101 g_signal_list_ids (GType itype,
1109 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1110 g_return_val_if_fail (n_ids != NULL, NULL);
1113 keys = G_BSEARCH_ARRAY_NODES (g_signal_key_bsa);
1114 n_nodes = g_signal_key_bsa->n_nodes;
1115 result = g_array_new (FALSE, FALSE, sizeof (guint));
1117 for (i = 0; i < n_nodes; i++)
1118 if (keys[i].itype == itype)
1120 const gchar *name = g_quark_to_string (keys[i].quark);
1122 /* Signal names with "_" in them are aliases to the same
1123 * name with "-" instead of "_".
1125 if (!strchr (name, '_'))
1126 g_array_append_val (result, keys[i].signal_id);
1128 *n_ids = result->len;
1132 /* give elaborate warnings */
1133 if (!g_type_name (itype))
1134 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1136 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1137 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1138 g_type_name (itype));
1139 else if (!g_type_class_peek (itype))
1140 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1141 g_type_name (itype));
1144 return (guint*) g_array_free (result, FALSE);
1149 * @signal_id: the signal's identifying number.
1151 * Given the signal's identifier, finds its name.
1153 * Two different signals may have the same name, if they have differing types.
1155 * Return value: the signal name, or %NULL if the signal number was invalid.
1157 G_CONST_RETURN gchar*
1158 g_signal_name (guint signal_id)
1164 node = LOOKUP_SIGNAL_NODE (signal_id);
1165 name = node ? node->name : NULL;
1173 * @signal_id: The signal id of the signal to query information for.
1174 * @query: A user provided structure that is filled in with constant
1175 * values upon success.
1177 * Queries the signal system for in-depth information about a
1178 * specific signal. This function will fill in a user-provided
1179 * structure to hold signal-specific information. If an invalid
1180 * signal id is passed in, the @signal_id member of the #GSignalQuery
1181 * is 0. All members filled into the #GSignalQuery structure should
1182 * be considered constant and have to be left untouched.
1185 g_signal_query (guint signal_id,
1186 GSignalQuery *query)
1190 g_return_if_fail (query != NULL);
1193 node = LOOKUP_SIGNAL_NODE (signal_id);
1194 if (!node || node->destroyed)
1195 query->signal_id = 0;
1198 query->signal_id = node->signal_id;
1199 query->signal_name = node->name;
1200 query->itype = node->itype;
1201 query->signal_flags = node->flags;
1202 query->return_type = node->return_type;
1203 query->n_params = node->n_params;
1204 query->param_types = node->param_types;
1211 * @signal_name: the name for the signal
1212 * @itype: the type this signal pertains to. It will also pertain to
1213 * types which are derived from this type.
1214 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1215 * the default handler is to be invoked. You should at least specify
1216 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1217 * @class_offset: The offset of the function pointer in the class structure
1218 * for this type. Used to invoke a class method generically.
1219 * @accumulator: the accumulator for this signal; may be %NULL.
1220 * @accu_data: user data for the @accumulator.
1221 * @c_marshaller: the function to translate arrays of parameter values to
1222 * signal emissions into C language callback invocations.
1223 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1224 * without a return value.
1225 * @n_params: the number of parameter types to follow.
1226 * @Varargs: a list of types, one for each parameter.
1228 * Creates a new signal. (This is usually done in the class initializer.)
1230 * Note that you can use '-' and '_' interchangeably in signal names.
1232 * Return value: the signal id
1235 g_signal_new (const gchar *signal_name,
1237 GSignalFlags signal_flags,
1239 GSignalAccumulator accumulator,
1241 GSignalCMarshaller c_marshaller,
1249 g_return_val_if_fail (signal_name != NULL, 0);
1251 va_start (args, n_params);
1253 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1254 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1255 accumulator, accu_data, c_marshaller,
1256 return_type, n_params, args);
1263 static inline ClassClosure*
1264 signal_find_class_closure (SignalNode *node,
1267 GBSearchArray *bsa = node->class_closure_bsa;
1274 /* cc->instance_type is 0 for default closure */
1276 key.instance_type = itype;
1277 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1278 while (!cc && key.instance_type)
1280 key.instance_type = g_type_parent (key.instance_type);
1281 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1289 static inline GClosure*
1290 signal_lookup_closure (SignalNode *node,
1291 GTypeInstance *instance)
1295 if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1296 cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1298 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1299 return cc ? cc->closure : NULL;
1303 signal_add_class_closure (SignalNode *node,
1309 if (!node->class_closure_bsa)
1310 node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1311 key.instance_type = itype;
1312 key.closure = g_closure_ref (closure);
1313 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1314 &g_class_closure_bconfig,
1317 g_closure_sink (closure);
1318 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1319 g_closure_set_marshal (closure, node->c_marshaller);
1324 * @signal_name: the name for the signal
1325 * @itype: the type this signal pertains to. It will also pertain to
1326 * types which are derived from this type.
1327 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1328 * the default handler is to be invoked. You should at least specify
1329 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1330 * @class_closure: The closure to invoke on signal emission.
1331 * @accumulator: the accumulator for this signal; may be %NULL.
1332 * @accu_data: user data for the @accumulator.
1333 * @c_marshaller: the function to translate arrays of parameter values to
1334 * signal emissions into C language callback invocations.
1335 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1336 * without a return value.
1337 * @n_params: the length of @param_types.
1338 * @param_types: an array types, one for each parameter.
1340 * Creates a new signal. (This is usually done in the class initializer.)
1342 * Note that you can use '-' and '_' interchangeably in signal names.
1344 * Return value: the signal id
1347 g_signal_newv (const gchar *signal_name,
1349 GSignalFlags signal_flags,
1350 GClosure *class_closure,
1351 GSignalAccumulator accumulator,
1353 GSignalCMarshaller c_marshaller,
1362 g_return_val_if_fail (signal_name != NULL, 0);
1363 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1365 g_return_val_if_fail (param_types != NULL, 0);
1366 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1367 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1368 g_return_val_if_fail (accumulator == NULL, 0);
1370 g_return_val_if_fail (accu_data == NULL, 0);
1372 name = g_strdup (signal_name);
1373 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1377 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1378 node = LOOKUP_SIGNAL_NODE (signal_id);
1379 if (node && !node->destroyed)
1381 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1383 type_debug_name (node->itype),
1384 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1389 if (node && node->itype != itype)
1391 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1393 type_debug_name (itype),
1394 type_debug_name (node->itype));
1399 for (i = 0; i < n_params; i++)
1400 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1402 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1403 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1408 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1410 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1411 type_debug_name (return_type), type_debug_name (itype), name);
1416 if (return_type != G_TYPE_NONE &&
1417 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1419 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1420 type_debug_name (itype), name, type_debug_name (return_type));
1426 /* setup permanent portion of signal node */
1431 signal_id = g_n_signal_nodes++;
1432 node = g_new (SignalNode, 1);
1433 node->signal_id = signal_id;
1434 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1435 g_signal_nodes[signal_id] = node;
1436 node->itype = itype;
1439 key.quark = g_quark_from_string (node->name);
1440 key.signal_id = signal_id;
1441 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1442 g_strdelimit (node->name, "_", '-');
1443 key.quark = g_quark_from_static_string (node->name);
1444 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1446 node->destroyed = FALSE;
1448 /* setup reinitializable portion */
1449 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1450 node->n_params = n_params;
1451 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1452 node->return_type = return_type;
1453 node->class_closure_bsa = NULL;
1456 node->accumulator = g_new (SignalAccumulator, 1);
1457 node->accumulator->func = accumulator;
1458 node->accumulator->data = accu_data;
1461 node->accumulator = NULL;
1462 node->c_marshaller = c_marshaller;
1463 node->emission_hooks = NULL;
1465 signal_add_class_closure (node, 0, class_closure);
1472 * g_signal_new_valist:
1473 * @signal_name: the name for the signal
1474 * @itype: the type this signal pertains to. It will also pertain to
1475 * types which are derived from this type.
1476 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1477 * the default handler is to be invoked. You should at least specify
1478 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1479 * @class_closure: The closure to invoke on signal emission.
1480 * @accumulator: the accumulator for this signal; may be %NULL.
1481 * @accu_data: user data for the @accumulator.
1482 * @c_marshaller: the function to translate arrays of parameter values to
1483 * signal emissions into C language callback invocations.
1484 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1485 * without a return value.
1486 * @n_params: the number of parameter types in @args.
1487 * @args: va_list of #GType, one for each parameter.
1489 * Creates a new signal. (This is usually done in the class initializer.)
1491 * Note that you can use '-' and '_' interchangeably in signal names.
1493 * Return value: the signal id
1496 g_signal_new_valist (const gchar *signal_name,
1498 GSignalFlags signal_flags,
1499 GClosure *class_closure,
1500 GSignalAccumulator accumulator,
1502 GSignalCMarshaller c_marshaller,
1513 param_types = g_new (GType, n_params);
1515 for (i = 0; i < n_params; i++)
1516 param_types[i] = va_arg (args, GType);
1521 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1522 class_closure, accumulator, accu_data, c_marshaller,
1523 return_type, n_params, param_types);
1524 g_free (param_types);
1530 signal_destroy_R (SignalNode *signal_node)
1532 SignalNode node = *signal_node;
1534 signal_node->destroyed = TRUE;
1536 /* reentrancy caution, zero out real contents first */
1537 signal_node->n_params = 0;
1538 signal_node->param_types = NULL;
1539 signal_node->return_type = 0;
1540 signal_node->class_closure_bsa = NULL;
1541 signal_node->accumulator = NULL;
1542 signal_node->c_marshaller = NULL;
1543 signal_node->emission_hooks = NULL;
1545 #ifdef G_ENABLE_DEBUG
1546 /* check current emissions */
1550 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1551 emission; emission = emission->next)
1552 if (emission->ihint.signal_id == node.signal_id)
1553 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1554 node.name, emission->instance);
1558 /* free contents that need to
1561 g_free (node.param_types);
1562 if (node.class_closure_bsa)
1566 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1568 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1570 g_closure_unref (cc->closure);
1572 g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1574 g_free (node.accumulator);
1575 if (node.emission_hooks)
1577 g_hook_list_clear (node.emission_hooks);
1578 g_free (node.emission_hooks);
1584 * g_signal_override_class_closure:
1585 * @signal_id: the signal id
1586 * @instance_type: the instance type on which to override the class closure
1588 * @class_closure: the closure.
1590 * Overrides the class closure (i.e. the default handler) for the given signal
1591 * for emissions on instances of @instance_type. @instance_type must be derived
1592 * from the type to which the signal belongs.
1595 g_signal_override_class_closure (guint signal_id,
1596 GType instance_type,
1597 GClosure *class_closure)
1601 g_return_if_fail (signal_id > 0);
1602 g_return_if_fail (class_closure != NULL);
1605 node = LOOKUP_SIGNAL_NODE (signal_id);
1606 if (!g_type_is_a (instance_type, node->itype))
1607 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1610 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1612 if (cc && cc->instance_type == instance_type)
1613 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1615 signal_add_class_closure (node, instance_type, class_closure);
1621 * g_signal_chain_from_overridden:
1622 * @instance_and_params: the argument list of the signal emission. The first
1623 * element in the array is a #GValue for the instance the signal is
1624 * being emitted on. The rest are any arguments to be passed to the
1626 * @return_value: Location for the return value.
1628 * Calls the original class closure of a signal. This function should only
1629 * be called from an overridden class closure; see
1630 * g_signal_override_class_closure().
1633 g_signal_chain_from_overridden (const GValue *instance_and_params,
1634 GValue *return_value)
1636 GType chain_type = 0, restore_type = 0;
1637 Emission *emission = NULL;
1638 GClosure *closure = NULL;
1642 g_return_if_fail (instance_and_params != NULL);
1643 instance = g_value_peek_pointer (instance_and_params);
1644 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1647 emission = emission_find_innermost (instance);
1650 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1652 g_assert (node != NULL); /* paranoid */
1654 /* we should probably do the same parameter checks as g_signal_emit() here.
1656 if (emission->chain_type != G_TYPE_NONE)
1658 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1660 g_assert (cc != NULL); /* closure currently in call stack */
1662 n_params = node->n_params;
1663 restore_type = cc->instance_type;
1664 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1665 if (cc && cc->instance_type != restore_type)
1667 closure = cc->closure;
1668 chain_type = cc->instance_type;
1672 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1675 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1678 emission->chain_type = chain_type;
1680 g_closure_invoke (closure,
1683 instance_and_params,
1686 emission->chain_type = restore_type;
1692 * g_signal_get_invocation_hint:
1693 * @instance: the instance to query
1695 * Returns the invocation hint of the innermost signal emission of instance.
1697 * Return value: the invocation hint of the innermost signal emission.
1699 GSignalInvocationHint*
1700 g_signal_get_invocation_hint (gpointer instance)
1702 Emission *emission = NULL;
1704 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1707 emission = emission_find_innermost (instance);
1710 return emission ? &emission->ihint : NULL;
1714 * g_signal_connect_closure_by_id:
1715 * @instance: the instance to connect to.
1716 * @signal_id: the id of the signal.
1717 * @detail: the detail.
1718 * @closure: the closure to connect.
1719 * @after: whether the handler should be called before or after the
1720 * default handler of the signal.
1722 * Connects a closure to a signal for a particular object.
1724 * Return value: the handler id
1727 g_signal_connect_closure_by_id (gpointer instance,
1734 gulong handler_seq_no = 0;
1736 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1737 g_return_val_if_fail (signal_id > 0, 0);
1738 g_return_val_if_fail (closure != NULL, 0);
1741 node = LOOKUP_SIGNAL_NODE (signal_id);
1744 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1745 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1746 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1747 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1750 Handler *handler = handler_new (after);
1752 handler_seq_no = handler->sequential_number;
1753 handler->detail = detail;
1754 handler->closure = g_closure_ref (closure);
1755 g_closure_sink (closure);
1756 handler_insert (signal_id, instance, handler);
1757 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1758 g_closure_set_marshal (closure, node->c_marshaller);
1762 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1765 return handler_seq_no;
1769 * g_signal_connect_closure:
1770 * @instance: the instance to connect to.
1771 * @detailed_signal: a string of the form "signal-name::detail".
1772 * @closure: the closure to connect.
1773 * @after: whether the handler should be called before or after the
1774 * default handler of the signal.
1776 * Connects a closure to a signal for a particular object.
1778 * Return value: the handler id
1781 g_signal_connect_closure (gpointer instance,
1782 const gchar *detailed_signal,
1787 gulong handler_seq_no = 0;
1791 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1792 g_return_val_if_fail (detailed_signal != NULL, 0);
1793 g_return_val_if_fail (closure != NULL, 0);
1796 itype = G_TYPE_FROM_INSTANCE (instance);
1797 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1800 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1802 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1803 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1804 else if (!g_type_is_a (itype, node->itype))
1805 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1808 Handler *handler = handler_new (after);
1810 handler_seq_no = handler->sequential_number;
1811 handler->detail = detail;
1812 handler->closure = g_closure_ref (closure);
1813 g_closure_sink (closure);
1814 handler_insert (signal_id, instance, handler);
1815 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1816 g_closure_set_marshal (handler->closure, node->c_marshaller);
1820 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1823 return handler_seq_no;
1827 * g_signal_connect_data:
1828 * @instance: the instance to connect to.
1829 * @detailed_signal: a string of the form "signal-name::detail".
1830 * @c_handler: the #GCallback to connect.
1831 * @data: data to pass to @c_handler calls.
1832 * @destroy_data: a #GDestroyNotify for @data.
1833 * @connect_flags: a combination of #GConnectFlags.
1835 * Connects a #GCallback function to a signal for a particular object.
1837 * Return value: the handler id
1840 g_signal_connect_data (gpointer instance,
1841 const gchar *detailed_signal,
1842 GCallback c_handler,
1844 GClosureNotify destroy_data,
1845 GConnectFlags connect_flags)
1848 gulong handler_seq_no = 0;
1851 gboolean swapped, after;
1853 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1854 g_return_val_if_fail (detailed_signal != NULL, 0);
1855 g_return_val_if_fail (c_handler != NULL, 0);
1857 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1858 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1861 itype = G_TYPE_FROM_INSTANCE (instance);
1862 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1865 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1867 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1868 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1869 else if (!g_type_is_a (itype, node->itype))
1870 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1873 Handler *handler = handler_new (after);
1875 handler_seq_no = handler->sequential_number;
1876 handler->detail = detail;
1877 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1878 g_closure_sink (handler->closure);
1879 handler_insert (signal_id, instance, handler);
1880 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1881 g_closure_set_marshal (handler->closure, node->c_marshaller);
1885 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1888 return handler_seq_no;
1892 * g_signal_handler_block:
1893 * @instance: The instance to block the signal handler of.
1894 * @handler_id: Handler id of the handler to be blocked.
1896 * Blocks a handler of an instance so it will not be called during
1897 * any signal emissions unless it is unblocked again. Thus "blocking"
1898 * a signal handler means to temporarily deactive it, a signal handler
1899 * has to be unblocked exactly the same amount of times it has been
1900 * blocked before to become active again.
1902 * The @handler_id has to be a valid signal handler id, connected to a
1903 * signal of @instance.
1906 g_signal_handler_block (gpointer instance,
1911 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1912 g_return_if_fail (handler_id > 0);
1915 handler = handler_lookup (instance, handler_id, NULL);
1918 #ifndef G_DISABLE_CHECKS
1919 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1920 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1922 handler->block_count += 1;
1925 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1930 * g_signal_handler_unblock:
1931 * @instance: The instance to unblock the signal handler of.
1932 * @handler_id: Handler id of the handler to be unblocked.
1934 * Undoes the effect of a previous g_signal_handler_block() call.
1935 * A blocked handler is skipped during signal emissions and will not be
1936 * invoked, unblocking it (for exactly the amount of times it has been
1937 * blocked before) reverts its "blocked" state, so the handler will be
1938 * recognized by the signal system and is called upon future or currently
1939 * ongoing signal emissions (since the order in which handlers are
1940 * called during signal emissions is deterministic, whether the
1941 * unblocked handler in question is called as part of a currently
1942 * ongoing emission depends on how far that emission has proceeded
1945 * The @handler_id has to be a valid id of a signal handler that is
1946 * connected to a signal of @instance and is currently blocked.
1949 g_signal_handler_unblock (gpointer instance,
1954 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1955 g_return_if_fail (handler_id > 0);
1958 handler = handler_lookup (instance, handler_id, NULL);
1961 if (handler->block_count)
1962 handler->block_count -= 1;
1964 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1967 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1972 * g_signal_handler_disconnect:
1973 * @instance: The instance to remove the signal handler from.
1974 * @handler_id: Handler id of the handler to be disconnected.
1976 * Disconnects a handler from an instance so it will not be called during
1977 * any future or currently ongoing emissions of the signal it has been
1978 * connected to. The @handler_id becomes invalid and may be reused.
1980 * The @handler_id has to be a valid signal handler id, connected to a
1981 * signal of @instance.
1984 g_signal_handler_disconnect (gpointer instance,
1990 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1991 g_return_if_fail (handler_id > 0);
1994 handler = handler_lookup (instance, handler_id, &signal_id);
1997 handler->sequential_number = 0;
1998 handler->block_count = 1;
1999 handler_unref_R (signal_id, instance, handler);
2002 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2007 * g_signal_handler_is_connected:
2008 * @instance: The instance where a signal handler is sought.
2009 * @handler_id: the handler id.
2011 * Returns whether @handler_id is the id of a handler connected to @instance.
2013 * Return value: whether @handler_id identifies a handler connected to
2017 g_signal_handler_is_connected (gpointer instance,
2023 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2024 g_return_val_if_fail (handler_id > 0, FALSE);
2027 handler = handler_lookup (instance, handler_id, NULL);
2028 connected = handler != NULL;
2035 * g_signal_handlers_destroy:
2036 * @instance: The instance whose signal handlers are to be destroyed
2038 * Destroys all the signal handlers connected to an object. This is done
2039 * automatically when the object is destroyed.
2041 * This function is labeled private.
2044 g_signal_handlers_destroy (gpointer instance)
2046 GBSearchArray *hlbsa;
2048 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2051 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2056 /* reentrancy caution, delete instance trace first */
2057 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2059 for (i = 0; i < hlbsa->n_nodes; i++)
2061 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2062 Handler *handler = hlist->handlers;
2066 Handler *tmp = handler;
2068 handler = tmp->next;
2069 tmp->block_count = 1;
2070 /* cruel unlink, this works because _all_ handlers vanish */
2073 if (tmp->sequential_number)
2075 tmp->sequential_number = 0;
2076 handler_unref_R (0, NULL, tmp);
2080 g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
2086 * g_signal_handler_find:
2087 * @instance: The instance owning the signal handler to be found.
2088 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2089 * and/or @data the handler has to match.
2090 * @signal_id: Signal the handler has to be connected to.
2091 * @detail: Signal detail the handler has to be connected to.
2092 * @closure: The closure the handler will invoke.
2093 * @func: The C closure callback of the handler (useless for non-C closures).
2094 * @data: The closure data of the handler's closure.
2096 * Finds the first signal handler that matches certain selection criteria.
2097 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2098 * flags, and the criteria values are passed as arguments.
2099 * The match @mask has to be non-0 for successful matches.
2100 * If no handler was found, 0 is returned.
2102 * Return value: A valid non-0 signal handler id for a successful match.
2105 g_signal_handler_find (gpointer instance,
2106 GSignalMatchType mask,
2113 gulong handler_seq_no = 0;
2115 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2116 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2118 if (mask & G_SIGNAL_MATCH_MASK)
2120 HandlerMatch *mlist;
2123 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2126 handler_seq_no = mlist->handler->sequential_number;
2127 handler_match_free1_R (mlist, instance);
2132 return handler_seq_no;
2136 signal_handlers_foreach_matched_R (gpointer instance,
2137 GSignalMatchType mask,
2143 void (*callback) (gpointer instance,
2144 gulong handler_seq_no))
2146 HandlerMatch *mlist;
2147 guint n_handlers = 0;
2149 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2153 if (mlist->handler->sequential_number)
2156 callback (instance, mlist->handler->sequential_number);
2159 mlist = handler_match_free1_R (mlist, instance);
2166 * g_signal_handlers_block_matched:
2167 * @instance: The instance to block handlers from.
2168 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2169 * and/or @data the handlers have to match.
2170 * @signal_id: Signal the handlers have to be connected to.
2171 * @detail: Signal detail the handlers have to be connected to.
2172 * @closure: The closure the handlers will invoke.
2173 * @func: The C closure callback of the handlers (useless for non-C closures).
2174 * @data: The closure data of the handlers' closures.
2176 * Blocks all handlers on an instance that match a certain selection criteria.
2177 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2178 * flags, and the criteria values are passed as arguments.
2179 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2180 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2181 * If no handlers were found, 0 is returned, the number of blocked handlers
2184 * Return value: The amount of handlers that got blocked.
2187 g_signal_handlers_block_matched (gpointer instance,
2188 GSignalMatchType mask,
2195 guint n_handlers = 0;
2197 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2198 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
2200 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2203 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2204 closure, func, data,
2205 g_signal_handler_block);
2213 * g_signal_handlers_unblock_matched:
2214 * @instance: The instance to unblock handlers from.
2215 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2216 * and/or @data the handlers have to match.
2217 * @signal_id: Signal the handlers have to be connected to.
2218 * @detail: Signal detail the handlers have to be connected to.
2219 * @closure: The closure the handlers will invoke.
2220 * @func: The C closure callback of the handlers (useless for non-C closures).
2221 * @data: The closure data of the handlers' closures.
2223 * Unblocks all handlers on an instance that match a certain selection
2224 * criteria. The criteria mask is passed as an OR-ed combination of
2225 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2226 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2227 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2228 * If no handlers were found, 0 is returned, the number of unblocked handlers
2229 * otherwise. The match criteria should not apply to any handlers that are
2230 * not currently blocked.
2232 * Return value: The amount of handlers that got unblocked.
2235 g_signal_handlers_unblock_matched (gpointer instance,
2236 GSignalMatchType mask,
2243 guint n_handlers = 0;
2245 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2246 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
2248 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2251 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2252 closure, func, data,
2253 g_signal_handler_unblock);
2261 * g_signal_handlers_disconnect_matched:
2262 * @instance: The instance to remove handlers from.
2263 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2264 * and/or @data the handlers have to match.
2265 * @signal_id: Signal the handlers have to be connected to.
2266 * @detail: Signal detail the handlers have to be connected to.
2267 * @closure: The closure the handlers will invoke.
2268 * @func: The C closure callback of the handlers (useless for non-C closures).
2269 * @data: The closure data of the handlers' closures.
2271 * Disconnects all handlers on an instance that match a certain selection
2272 * criteria. The criteria mask is passed as an OR-ed combination of
2273 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2274 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2275 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2276 * If no handlers were found, 0 is returned, the number of disconnected
2277 * handlers otherwise.
2279 * Return value: The amount of handlers that got disconnected.
2282 g_signal_handlers_disconnect_matched (gpointer instance,
2283 GSignalMatchType mask,
2290 guint n_handlers = 0;
2292 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2293 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
2295 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2298 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2299 closure, func, data,
2300 g_signal_handler_disconnect);
2308 * g_signal_has_handler_pending:
2309 * @instance: the object whose signal handlers are sought.
2310 * @signal_id: the signal id.
2311 * @detail: the detail.
2312 * @may_be_blocked: whether blocked handlers should count as match.
2314 * Returns whether there are any handlers connected to @instance for the
2315 * given signal id and detail.
2317 * One example of when you might use this is when the arguments to the
2318 * signal are difficult to compute. A class implementor may opt to not emit
2319 * the signal if no one is attached anyway, thus saving the cost of building
2322 * Return value: %TRUE if a handler is connected to the signal,
2326 g_signal_has_handler_pending (gpointer instance,
2329 gboolean may_be_blocked)
2331 HandlerMatch *mlist;
2332 gboolean has_pending;
2334 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2335 g_return_val_if_fail (signal_id > 0, FALSE);
2340 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2342 if (!(node->flags & G_SIGNAL_DETAILED))
2344 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2349 mlist = handlers_find (instance,
2350 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2351 signal_id, detail, NULL, NULL, NULL, TRUE);
2355 handler_match_free1_R (mlist, instance);
2358 has_pending = FALSE;
2366 * @instance_and_params: argument list for the signal emission. The first
2367 * element in the array is a #GValue for the instance the signal is
2368 * being emitted on. The rest are any arguments to be passed to the
2370 * @signal_id: the signal id
2371 * @detail: the detail
2372 * @return_value: Location to store the return value of the signal emission.
2376 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2377 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2380 g_signal_emitv (const GValue *instance_and_params,
2383 GValue *return_value)
2385 const GValue *param_values;
2388 #ifdef G_ENABLE_DEBUG
2392 g_return_if_fail (instance_and_params != NULL);
2393 instance = g_value_peek_pointer (instance_and_params);
2394 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2395 g_return_if_fail (signal_id > 0);
2397 param_values = instance_and_params + 1;
2400 node = LOOKUP_SIGNAL_NODE (signal_id);
2401 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2403 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2407 #ifdef G_ENABLE_DEBUG
2408 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2410 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2414 for (i = 0; i < node->n_params; i++)
2415 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2417 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2419 type_debug_name (node->param_types[i]),
2422 G_VALUE_TYPE_NAME (param_values + i));
2426 if (node->return_type != G_TYPE_NONE)
2430 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2432 type_debug_name (node->return_type),
2437 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2439 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2441 type_debug_name (node->return_type),
2443 G_VALUE_TYPE_NAME (return_value));
2449 return_value = NULL;
2450 #endif /* G_ENABLE_DEBUG */
2453 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2457 * g_signal_emit_valist:
2458 * @instance: the instance the signal is being emitted on.
2459 * @signal_id: the signal id
2460 * @detail: the detail
2461 * @var_args: a list of parameters to be passed to the signal, followed by a
2462 * location for the return value. If the return type of the signal
2463 * is #G_TYPE_NONE, the return value location can be omitted.
2467 * Note that g_signal_emit_valist() resets the return value to the default
2468 * if no handlers are connected, in contrast to g_signal_emitv().
2471 g_signal_emit_valist (gpointer instance,
2476 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2477 GType signal_return_type;
2478 GValue *param_values;
2482 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2483 g_return_if_fail (signal_id > 0);
2486 node = LOOKUP_SIGNAL_NODE (signal_id);
2487 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2489 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2493 #ifndef G_DISABLE_CHECKS
2494 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2496 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2500 #endif /* !G_DISABLE_CHECKS */
2502 n_params = node->n_params;
2503 signal_return_type = node->return_type;
2504 if (node->n_params < MAX_STACK_VALUES)
2505 instance_and_params = stack_values;
2508 free_me = g_new (GValue, node->n_params + 1);
2509 instance_and_params = free_me;
2511 param_values = instance_and_params + 1;
2512 for (i = 0; i < node->n_params; i++)
2515 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2516 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2518 param_values[i].g_type = 0;
2520 g_value_init (param_values + i, ptype);
2521 G_VALUE_COLLECT (param_values + i,
2523 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2527 g_warning ("%s: %s", G_STRLOC, error);
2530 /* we purposely leak the value here, it might not be
2531 * in a sane state if an error condition occoured
2534 g_value_unset (param_values + i);
2542 instance_and_params->g_type = 0;
2543 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2544 g_value_set_instance (instance_and_params, instance);
2545 if (signal_return_type == G_TYPE_NONE)
2546 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2549 GValue return_value = { 0, };
2550 gchar *error = NULL;
2551 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2552 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2554 g_value_init (&return_value, rtype);
2556 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2558 G_VALUE_LCOPY (&return_value,
2560 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2563 g_value_unset (&return_value);
2566 g_warning ("%s: %s", G_STRLOC, error);
2569 /* we purposely leak the value here, it might not be
2570 * in a sane state if an error condition occoured
2574 for (i = 0; i < n_params; i++)
2575 g_value_unset (param_values + i);
2576 g_value_unset (instance_and_params);
2583 * @instance: the instance the signal is being emitted on.
2584 * @signal_id: the signal id
2585 * @detail: the detail
2586 * @Varargs: parameters to be passed to the signal, followed by a
2587 * location for the return value. If the return type of the signal
2588 * is #G_TYPE_NONE, the return value location can be omitted.
2592 * Note that g_signal_emit() resets the return value to the default
2593 * if no handlers are connected, in contrast to g_signal_emitv().
2596 g_signal_emit (gpointer instance,
2603 va_start (var_args, detail);
2604 g_signal_emit_valist (instance, signal_id, detail, var_args);
2609 * g_signal_emit_by_name:
2610 * @instance: the instance the signal is being emitted on.
2611 * @detailed_signal: a string of the form "signal-name::detail".
2612 * @Varargs: parameters to be passed to the signal, followed by a
2613 * location for the return value. If the return type of the signal
2614 * is #G_TYPE_NONE, the return value location can be omitted.
2618 * Note that g_signal_emit_by_name() resets the return value to the default
2619 * if no handlers are connected, in contrast to g_signal_emitv().
2622 g_signal_emit_by_name (gpointer instance,
2623 const gchar *detailed_signal,
2629 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2630 g_return_if_fail (detailed_signal != NULL);
2633 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2640 va_start (var_args, detailed_signal);
2641 g_signal_emit_valist (instance, signal_id, detail, var_args);
2645 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2648 static inline gboolean
2649 accumulate (GSignalInvocationHint *ihint,
2650 GValue *return_accu,
2651 GValue *handler_return,
2652 SignalAccumulator *accumulator)
2654 gboolean continue_emission;
2659 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2660 g_value_reset (handler_return);
2662 return continue_emission;
2666 signal_emit_unlocked_R (SignalNode *node,
2669 GValue *emission_return,
2670 const GValue *instance_and_params)
2672 SignalAccumulator *accumulator;
2674 GClosure *class_closure;
2676 Handler *handler_list = NULL;
2677 GValue *return_accu, accu = { 0, };
2679 gulong max_sequential_handler_number;
2680 gboolean return_value_altered = FALSE;
2682 #ifdef G_ENABLE_DEBUG
2683 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2685 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2686 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2689 if (g_trap_instance_signals == instance)
2692 #endif /* G_ENABLE_DEBUG */
2695 signal_id = node->signal_id;
2696 if (node->flags & G_SIGNAL_NO_RECURSE)
2698 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2702 node->state = EMISSION_RESTART;
2704 return return_value_altered;
2707 accumulator = node->accumulator;
2711 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2712 return_accu = &accu;
2716 return_accu = emission_return;
2717 emission.instance = instance;
2718 emission.ihint.signal_id = node->signal_id;
2719 emission.ihint.detail = detail;
2720 emission.ihint.run_type = 0;
2722 emission.chain_type = G_TYPE_NONE;
2723 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2724 class_closure = signal_lookup_closure (node, instance);
2729 handler_unref_R (signal_id, instance, handler_list);
2730 max_sequential_handler_number = g_handler_sequential_number;
2731 hlist = handler_list_lookup (signal_id, instance);
2732 handler_list = hlist ? hlist->handlers : NULL;
2734 handler_ref (handler_list);
2736 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2738 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2740 emission.state = EMISSION_RUN;
2742 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2744 g_closure_invoke (class_closure,
2747 instance_and_params,
2749 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2750 emission.state == EMISSION_RUN)
2751 emission.state = EMISSION_STOP;
2753 emission.chain_type = G_TYPE_NONE;
2754 return_value_altered = TRUE;
2756 if (emission.state == EMISSION_STOP)
2758 else if (emission.state == EMISSION_RESTART)
2762 if (node->emission_hooks)
2764 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2767 emission.state = EMISSION_HOOK;
2768 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2771 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2773 if (!signal_hook->detail || signal_hook->detail == detail)
2775 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2777 was_in_call = G_HOOK_IN_CALL (hook);
2778 hook->flags |= G_HOOK_FLAG_IN_CALL;
2780 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2783 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2785 g_hook_destroy_link (node->emission_hooks, hook);
2787 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2790 if (emission.state == EMISSION_RESTART)
2796 Handler *handler = handler_list;
2798 emission.state = EMISSION_RUN;
2799 handler_ref (handler);
2806 handler_unref_R (signal_id, instance, handler_list);
2807 handler_list = handler;
2810 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2811 handler->sequential_number < max_sequential_handler_number)
2814 g_closure_invoke (handler->closure,
2817 instance_and_params,
2819 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2820 emission.state == EMISSION_RUN)
2821 emission.state = EMISSION_STOP;
2823 return_value_altered = TRUE;
2825 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2828 tmp = handler->next;
2832 handler_unref_R (signal_id, instance, handler_list);
2833 handler_list = handler;
2838 if (emission.state == EMISSION_STOP)
2840 else if (emission.state == EMISSION_RESTART)
2844 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2846 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2848 emission.state = EMISSION_RUN;
2850 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2852 g_closure_invoke (class_closure,
2855 instance_and_params,
2857 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2858 emission.state == EMISSION_RUN)
2859 emission.state = EMISSION_STOP;
2861 emission.chain_type = G_TYPE_NONE;
2862 return_value_altered = TRUE;
2864 if (emission.state == EMISSION_STOP)
2866 else if (emission.state == EMISSION_RESTART)
2872 Handler *handler = handler_list;
2874 emission.state = EMISSION_RUN;
2875 handler_ref (handler);
2880 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2881 handler->sequential_number < max_sequential_handler_number)
2884 g_closure_invoke (handler->closure,
2887 instance_and_params,
2889 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2890 emission.state == EMISSION_RUN)
2891 emission.state = EMISSION_STOP;
2893 return_value_altered = TRUE;
2895 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2898 tmp = handler->next;
2902 handler_unref_R (signal_id, instance, handler);
2907 if (emission.state == EMISSION_STOP)
2909 else if (emission.state == EMISSION_RESTART)
2915 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2917 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2919 gboolean need_unset = FALSE;
2921 emission.state = EMISSION_STOP;
2923 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2925 if (node->return_type != G_TYPE_NONE && !accumulator)
2927 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2930 g_closure_invoke (class_closure,
2931 node->return_type != G_TYPE_NONE ? &accu : NULL,
2933 instance_and_params,
2936 g_value_unset (&accu);
2938 emission.chain_type = G_TYPE_NONE;
2940 if (emission.state == EMISSION_RESTART)
2945 handler_unref_R (signal_id, instance, handler_list);
2947 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2950 g_value_unset (&accu);
2952 return return_value_altered;
2956 type_debug_name (GType type)
2960 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2961 return name ? name : "<unknown>";
2967 /* --- compile standard marshallers --- */
2968 #include "gobject.h"
2970 #include "gmarshal.c"