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 * See g_signal_new() for details on allowed signal names.
1061 * Return value: the signal's identifying number, or 0 if no signal was found.
1064 g_signal_lookup (const gchar *name,
1068 g_return_val_if_fail (name != NULL, 0);
1069 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1072 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1076 /* give elaborate warnings */
1077 if (!g_type_name (itype))
1078 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1080 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1081 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1082 name, g_type_name (itype));
1083 else if (!g_type_class_peek (itype))
1084 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1085 name, g_type_name (itype));
1092 * g_signal_list_ids:
1093 * @itype: Instance or interface type.
1094 * @n_ids: Location to store the number of signal ids for @itype.
1096 * Lists the signals by id that a certain instance or interface type
1097 * created. Further information about the signals can be acquired through
1100 * Return value: Newly allocated array of signal IDs.
1103 g_signal_list_ids (GType itype,
1111 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1112 g_return_val_if_fail (n_ids != NULL, NULL);
1115 keys = G_BSEARCH_ARRAY_NODES (g_signal_key_bsa);
1116 n_nodes = g_signal_key_bsa->n_nodes;
1117 result = g_array_new (FALSE, FALSE, sizeof (guint));
1119 for (i = 0; i < n_nodes; i++)
1120 if (keys[i].itype == itype)
1122 const gchar *name = g_quark_to_string (keys[i].quark);
1124 /* Signal names with "_" in them are aliases to the same
1125 * name with "-" instead of "_".
1127 if (!strchr (name, '_'))
1128 g_array_append_val (result, keys[i].signal_id);
1130 *n_ids = result->len;
1134 /* give elaborate warnings */
1135 if (!g_type_name (itype))
1136 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1138 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1139 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1140 g_type_name (itype));
1141 else if (!g_type_class_peek (itype))
1142 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1143 g_type_name (itype));
1146 return (guint*) g_array_free (result, FALSE);
1151 * @signal_id: the signal's identifying number.
1153 * Given the signal's identifier, finds its name.
1155 * Two different signals may have the same name, if they have differing types.
1157 * Return value: the signal name, or %NULL if the signal number was invalid.
1159 G_CONST_RETURN gchar*
1160 g_signal_name (guint signal_id)
1166 node = LOOKUP_SIGNAL_NODE (signal_id);
1167 name = node ? node->name : NULL;
1175 * @signal_id: The signal id of the signal to query information for.
1176 * @query: A user provided structure that is filled in with constant
1177 * values upon success.
1179 * Queries the signal system for in-depth information about a
1180 * specific signal. This function will fill in a user-provided
1181 * structure to hold signal-specific information. If an invalid
1182 * signal id is passed in, the @signal_id member of the #GSignalQuery
1183 * is 0. All members filled into the #GSignalQuery structure should
1184 * be considered constant and have to be left untouched.
1187 g_signal_query (guint signal_id,
1188 GSignalQuery *query)
1192 g_return_if_fail (query != NULL);
1195 node = LOOKUP_SIGNAL_NODE (signal_id);
1196 if (!node || node->destroyed)
1197 query->signal_id = 0;
1200 query->signal_id = node->signal_id;
1201 query->signal_name = node->name;
1202 query->itype = node->itype;
1203 query->signal_flags = node->flags;
1204 query->return_type = node->return_type;
1205 query->n_params = node->n_params;
1206 query->param_types = node->param_types;
1213 * @signal_name: the name for the signal
1214 * @itype: the type this signal pertains to. It will also pertain to
1215 * types which are derived from this type.
1216 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1217 * the default handler is to be invoked. You should at least specify
1218 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1219 * @class_offset: The offset of the function pointer in the class structure
1220 * for this type. Used to invoke a class method generically.
1221 * @accumulator: the accumulator for this signal; may be %NULL.
1222 * @accu_data: user data for the @accumulator.
1223 * @c_marshaller: the function to translate arrays of parameter values to
1224 * signal emissions into C language callback invocations.
1225 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1226 * without a return value.
1227 * @n_params: the number of parameter types to follow.
1228 * @Varargs: a list of types, one for each parameter.
1230 * Creates a new signal. (This is usually done in the class initializer.)
1232 * A signal name consists of segments consisting of ASCII letters and
1233 * digits, separated by either the '-' or '_' character. The first
1234 * character of a signal name must be a letter. Names which violate these
1235 * rules lead to undefined behaviour of the GSignal system.
1237 * When registering a signal and looking up a signal, either separator can
1238 * be used, but they cannot be mixed.
1240 * Return value: the signal id
1243 g_signal_new (const gchar *signal_name,
1245 GSignalFlags signal_flags,
1247 GSignalAccumulator accumulator,
1249 GSignalCMarshaller c_marshaller,
1257 g_return_val_if_fail (signal_name != NULL, 0);
1259 va_start (args, n_params);
1261 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1262 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1263 accumulator, accu_data, c_marshaller,
1264 return_type, n_params, args);
1271 static inline ClassClosure*
1272 signal_find_class_closure (SignalNode *node,
1275 GBSearchArray *bsa = node->class_closure_bsa;
1282 /* cc->instance_type is 0 for default closure */
1284 key.instance_type = itype;
1285 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1286 while (!cc && key.instance_type)
1288 key.instance_type = g_type_parent (key.instance_type);
1289 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1297 static inline GClosure*
1298 signal_lookup_closure (SignalNode *node,
1299 GTypeInstance *instance)
1303 if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1304 cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1306 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1307 return cc ? cc->closure : NULL;
1311 signal_add_class_closure (SignalNode *node,
1317 if (!node->class_closure_bsa)
1318 node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1319 key.instance_type = itype;
1320 key.closure = g_closure_ref (closure);
1321 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1322 &g_class_closure_bconfig,
1325 g_closure_sink (closure);
1326 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1327 g_closure_set_marshal (closure, node->c_marshaller);
1332 * @signal_name: the name for the signal
1333 * @itype: the type this signal pertains to. It will also pertain to
1334 * types which are derived from this type.
1335 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1336 * the default handler is to be invoked. You should at least specify
1337 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1338 * @class_closure: The closure to invoke on signal emission.
1339 * @accumulator: the accumulator for this signal; may be %NULL.
1340 * @accu_data: user data for the @accumulator.
1341 * @c_marshaller: the function to translate arrays of parameter values to
1342 * signal emissions into C language callback invocations.
1343 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1344 * without a return value.
1345 * @n_params: the length of @param_types.
1346 * @param_types: an array types, one for each parameter.
1348 * Creates a new signal. (This is usually done in the class initializer.)
1350 * See g_signal_new() for details on allowed signal names.
1352 * Return value: the signal id
1355 g_signal_newv (const gchar *signal_name,
1357 GSignalFlags signal_flags,
1358 GClosure *class_closure,
1359 GSignalAccumulator accumulator,
1361 GSignalCMarshaller c_marshaller,
1370 g_return_val_if_fail (signal_name != NULL, 0);
1371 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1373 g_return_val_if_fail (param_types != NULL, 0);
1374 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1375 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1376 g_return_val_if_fail (accumulator == NULL, 0);
1378 g_return_val_if_fail (accu_data == NULL, 0);
1380 name = g_strdup (signal_name);
1381 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1385 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1386 node = LOOKUP_SIGNAL_NODE (signal_id);
1387 if (node && !node->destroyed)
1389 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1391 type_debug_name (node->itype),
1392 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1397 if (node && node->itype != itype)
1399 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1401 type_debug_name (itype),
1402 type_debug_name (node->itype));
1407 for (i = 0; i < n_params; i++)
1408 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1410 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1411 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1416 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1418 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1419 type_debug_name (return_type), type_debug_name (itype), name);
1424 if (return_type != G_TYPE_NONE &&
1425 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1427 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1428 type_debug_name (itype), name, type_debug_name (return_type));
1434 /* setup permanent portion of signal node */
1439 signal_id = g_n_signal_nodes++;
1440 node = g_new (SignalNode, 1);
1441 node->signal_id = signal_id;
1442 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1443 g_signal_nodes[signal_id] = node;
1444 node->itype = itype;
1447 key.quark = g_quark_from_string (node->name);
1448 key.signal_id = signal_id;
1449 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1450 g_strdelimit (node->name, "_", '-');
1451 key.quark = g_quark_from_static_string (node->name);
1452 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1454 node->destroyed = FALSE;
1456 /* setup reinitializable portion */
1457 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1458 node->n_params = n_params;
1459 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1460 node->return_type = return_type;
1461 node->class_closure_bsa = NULL;
1464 node->accumulator = g_new (SignalAccumulator, 1);
1465 node->accumulator->func = accumulator;
1466 node->accumulator->data = accu_data;
1469 node->accumulator = NULL;
1470 node->c_marshaller = c_marshaller;
1471 node->emission_hooks = NULL;
1473 signal_add_class_closure (node, 0, class_closure);
1480 * g_signal_new_valist:
1481 * @signal_name: the name for the signal
1482 * @itype: the type this signal pertains to. It will also pertain to
1483 * types which are derived from this type.
1484 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1485 * the default handler is to be invoked. You should at least specify
1486 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1487 * @class_closure: The closure to invoke on signal emission.
1488 * @accumulator: the accumulator for this signal; may be %NULL.
1489 * @accu_data: user data for the @accumulator.
1490 * @c_marshaller: the function to translate arrays of parameter values to
1491 * signal emissions into C language callback invocations.
1492 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1493 * without a return value.
1494 * @n_params: the number of parameter types in @args.
1495 * @args: va_list of #GType, one for each parameter.
1497 * Creates a new signal. (This is usually done in the class initializer.)
1499 * See g_signal_new() for details on allowed signal names.
1501 * Return value: the signal id
1504 g_signal_new_valist (const gchar *signal_name,
1506 GSignalFlags signal_flags,
1507 GClosure *class_closure,
1508 GSignalAccumulator accumulator,
1510 GSignalCMarshaller c_marshaller,
1521 param_types = g_new (GType, n_params);
1523 for (i = 0; i < n_params; i++)
1524 param_types[i] = va_arg (args, GType);
1529 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1530 class_closure, accumulator, accu_data, c_marshaller,
1531 return_type, n_params, param_types);
1532 g_free (param_types);
1538 signal_destroy_R (SignalNode *signal_node)
1540 SignalNode node = *signal_node;
1542 signal_node->destroyed = TRUE;
1544 /* reentrancy caution, zero out real contents first */
1545 signal_node->n_params = 0;
1546 signal_node->param_types = NULL;
1547 signal_node->return_type = 0;
1548 signal_node->class_closure_bsa = NULL;
1549 signal_node->accumulator = NULL;
1550 signal_node->c_marshaller = NULL;
1551 signal_node->emission_hooks = NULL;
1553 #ifdef G_ENABLE_DEBUG
1554 /* check current emissions */
1558 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1559 emission; emission = emission->next)
1560 if (emission->ihint.signal_id == node.signal_id)
1561 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1562 node.name, emission->instance);
1566 /* free contents that need to
1569 g_free (node.param_types);
1570 if (node.class_closure_bsa)
1574 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1576 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1578 g_closure_unref (cc->closure);
1580 g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1582 g_free (node.accumulator);
1583 if (node.emission_hooks)
1585 g_hook_list_clear (node.emission_hooks);
1586 g_free (node.emission_hooks);
1592 * g_signal_override_class_closure:
1593 * @signal_id: the signal id
1594 * @instance_type: the instance type on which to override the class closure
1596 * @class_closure: the closure.
1598 * Overrides the class closure (i.e. the default handler) for the given signal
1599 * for emissions on instances of @instance_type. @instance_type must be derived
1600 * from the type to which the signal belongs.
1603 g_signal_override_class_closure (guint signal_id,
1604 GType instance_type,
1605 GClosure *class_closure)
1609 g_return_if_fail (signal_id > 0);
1610 g_return_if_fail (class_closure != NULL);
1613 node = LOOKUP_SIGNAL_NODE (signal_id);
1614 if (!g_type_is_a (instance_type, node->itype))
1615 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1618 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1620 if (cc && cc->instance_type == instance_type)
1621 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1623 signal_add_class_closure (node, instance_type, class_closure);
1629 * g_signal_chain_from_overridden:
1630 * @instance_and_params: the argument list of the signal emission. The first
1631 * element in the array is a #GValue for the instance the signal is
1632 * being emitted on. The rest are any arguments to be passed to the
1634 * @return_value: Location for the return value.
1636 * Calls the original class closure of a signal. This function should only
1637 * be called from an overridden class closure; see
1638 * g_signal_override_class_closure().
1641 g_signal_chain_from_overridden (const GValue *instance_and_params,
1642 GValue *return_value)
1644 GType chain_type = 0, restore_type = 0;
1645 Emission *emission = NULL;
1646 GClosure *closure = NULL;
1650 g_return_if_fail (instance_and_params != NULL);
1651 instance = g_value_peek_pointer (instance_and_params);
1652 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1655 emission = emission_find_innermost (instance);
1658 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1660 g_assert (node != NULL); /* paranoid */
1662 /* we should probably do the same parameter checks as g_signal_emit() here.
1664 if (emission->chain_type != G_TYPE_NONE)
1666 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1668 g_assert (cc != NULL); /* closure currently in call stack */
1670 n_params = node->n_params;
1671 restore_type = cc->instance_type;
1672 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1673 if (cc && cc->instance_type != restore_type)
1675 closure = cc->closure;
1676 chain_type = cc->instance_type;
1680 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1683 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1686 emission->chain_type = chain_type;
1688 g_closure_invoke (closure,
1691 instance_and_params,
1694 emission->chain_type = restore_type;
1700 * g_signal_get_invocation_hint:
1701 * @instance: the instance to query
1703 * Returns the invocation hint of the innermost signal emission of instance.
1705 * Return value: the invocation hint of the innermost signal emission.
1707 GSignalInvocationHint*
1708 g_signal_get_invocation_hint (gpointer instance)
1710 Emission *emission = NULL;
1712 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1715 emission = emission_find_innermost (instance);
1718 return emission ? &emission->ihint : NULL;
1722 * g_signal_connect_closure_by_id:
1723 * @instance: the instance to connect to.
1724 * @signal_id: the id of the signal.
1725 * @detail: the detail.
1726 * @closure: the closure to connect.
1727 * @after: whether the handler should be called before or after the
1728 * default handler of the signal.
1730 * Connects a closure to a signal for a particular object.
1732 * Return value: the handler id
1735 g_signal_connect_closure_by_id (gpointer instance,
1742 gulong handler_seq_no = 0;
1744 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1745 g_return_val_if_fail (signal_id > 0, 0);
1746 g_return_val_if_fail (closure != NULL, 0);
1749 node = LOOKUP_SIGNAL_NODE (signal_id);
1752 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1753 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1754 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1755 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1758 Handler *handler = handler_new (after);
1760 handler_seq_no = handler->sequential_number;
1761 handler->detail = detail;
1762 handler->closure = g_closure_ref (closure);
1763 g_closure_sink (closure);
1764 handler_insert (signal_id, instance, handler);
1765 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1766 g_closure_set_marshal (closure, node->c_marshaller);
1770 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1773 return handler_seq_no;
1777 * g_signal_connect_closure:
1778 * @instance: the instance to connect to.
1779 * @detailed_signal: a string of the form "signal-name::detail".
1780 * @closure: the closure to connect.
1781 * @after: whether the handler should be called before or after the
1782 * default handler of the signal.
1784 * Connects a closure to a signal for a particular object.
1786 * Return value: the handler id
1789 g_signal_connect_closure (gpointer instance,
1790 const gchar *detailed_signal,
1795 gulong handler_seq_no = 0;
1799 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1800 g_return_val_if_fail (detailed_signal != NULL, 0);
1801 g_return_val_if_fail (closure != NULL, 0);
1804 itype = G_TYPE_FROM_INSTANCE (instance);
1805 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1808 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1810 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1811 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1812 else if (!g_type_is_a (itype, node->itype))
1813 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1816 Handler *handler = handler_new (after);
1818 handler_seq_no = handler->sequential_number;
1819 handler->detail = detail;
1820 handler->closure = g_closure_ref (closure);
1821 g_closure_sink (closure);
1822 handler_insert (signal_id, instance, handler);
1823 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1824 g_closure_set_marshal (handler->closure, node->c_marshaller);
1828 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1831 return handler_seq_no;
1835 * g_signal_connect_data:
1836 * @instance: the instance to connect to.
1837 * @detailed_signal: a string of the form "signal-name::detail".
1838 * @c_handler: the #GCallback to connect.
1839 * @data: data to pass to @c_handler calls.
1840 * @destroy_data: a #GDestroyNotify for @data.
1841 * @connect_flags: a combination of #GConnectFlags.
1843 * Connects a #GCallback function to a signal for a particular object.
1845 * Return value: the handler id
1848 g_signal_connect_data (gpointer instance,
1849 const gchar *detailed_signal,
1850 GCallback c_handler,
1852 GClosureNotify destroy_data,
1853 GConnectFlags connect_flags)
1856 gulong handler_seq_no = 0;
1859 gboolean swapped, after;
1861 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1862 g_return_val_if_fail (detailed_signal != NULL, 0);
1863 g_return_val_if_fail (c_handler != NULL, 0);
1865 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1866 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1869 itype = G_TYPE_FROM_INSTANCE (instance);
1870 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1873 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1875 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1876 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1877 else if (!g_type_is_a (itype, node->itype))
1878 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1881 Handler *handler = handler_new (after);
1883 handler_seq_no = handler->sequential_number;
1884 handler->detail = detail;
1885 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1886 g_closure_sink (handler->closure);
1887 handler_insert (signal_id, instance, handler);
1888 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1889 g_closure_set_marshal (handler->closure, node->c_marshaller);
1893 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1896 return handler_seq_no;
1900 * g_signal_handler_block:
1901 * @instance: The instance to block the signal handler of.
1902 * @handler_id: Handler id of the handler to be blocked.
1904 * Blocks a handler of an instance so it will not be called during
1905 * any signal emissions unless it is unblocked again. Thus "blocking"
1906 * a signal handler means to temporarily deactive it, a signal handler
1907 * has to be unblocked exactly the same amount of times it has been
1908 * blocked before to become active again.
1910 * The @handler_id has to be a valid signal handler id, connected to a
1911 * signal of @instance.
1914 g_signal_handler_block (gpointer instance,
1919 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1920 g_return_if_fail (handler_id > 0);
1923 handler = handler_lookup (instance, handler_id, NULL);
1926 #ifndef G_DISABLE_CHECKS
1927 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1928 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1930 handler->block_count += 1;
1933 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1938 * g_signal_handler_unblock:
1939 * @instance: The instance to unblock the signal handler of.
1940 * @handler_id: Handler id of the handler to be unblocked.
1942 * Undoes the effect of a previous g_signal_handler_block() call.
1943 * A blocked handler is skipped during signal emissions and will not be
1944 * invoked, unblocking it (for exactly the amount of times it has been
1945 * blocked before) reverts its "blocked" state, so the handler will be
1946 * recognized by the signal system and is called upon future or currently
1947 * ongoing signal emissions (since the order in which handlers are
1948 * called during signal emissions is deterministic, whether the
1949 * unblocked handler in question is called as part of a currently
1950 * ongoing emission depends on how far that emission has proceeded
1953 * The @handler_id has to be a valid id of a signal handler that is
1954 * connected to a signal of @instance and is currently blocked.
1957 g_signal_handler_unblock (gpointer instance,
1962 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1963 g_return_if_fail (handler_id > 0);
1966 handler = handler_lookup (instance, handler_id, NULL);
1969 if (handler->block_count)
1970 handler->block_count -= 1;
1972 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1975 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1980 * g_signal_handler_disconnect:
1981 * @instance: The instance to remove the signal handler from.
1982 * @handler_id: Handler id of the handler to be disconnected.
1984 * Disconnects a handler from an instance so it will not be called during
1985 * any future or currently ongoing emissions of the signal it has been
1986 * connected to. The @handler_id becomes invalid and may be reused.
1988 * The @handler_id has to be a valid signal handler id, connected to a
1989 * signal of @instance.
1992 g_signal_handler_disconnect (gpointer instance,
1998 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1999 g_return_if_fail (handler_id > 0);
2002 handler = handler_lookup (instance, handler_id, &signal_id);
2005 handler->sequential_number = 0;
2006 handler->block_count = 1;
2007 handler_unref_R (signal_id, instance, handler);
2010 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2015 * g_signal_handler_is_connected:
2016 * @instance: The instance where a signal handler is sought.
2017 * @handler_id: the handler id.
2019 * Returns whether @handler_id is the id of a handler connected to @instance.
2021 * Return value: whether @handler_id identifies a handler connected to
2025 g_signal_handler_is_connected (gpointer instance,
2031 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2032 g_return_val_if_fail (handler_id > 0, FALSE);
2035 handler = handler_lookup (instance, handler_id, NULL);
2036 connected = handler != NULL;
2043 * g_signal_handlers_destroy:
2044 * @instance: The instance whose signal handlers are to be destroyed
2046 * Destroys all the signal handlers connected to an object. This is done
2047 * automatically when the object is destroyed.
2049 * This function is labeled private.
2052 g_signal_handlers_destroy (gpointer instance)
2054 GBSearchArray *hlbsa;
2056 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2059 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2064 /* reentrancy caution, delete instance trace first */
2065 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2067 for (i = 0; i < hlbsa->n_nodes; i++)
2069 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2070 Handler *handler = hlist->handlers;
2074 Handler *tmp = handler;
2076 handler = tmp->next;
2077 tmp->block_count = 1;
2078 /* cruel unlink, this works because _all_ handlers vanish */
2081 if (tmp->sequential_number)
2083 tmp->sequential_number = 0;
2084 handler_unref_R (0, NULL, tmp);
2088 g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
2094 * g_signal_handler_find:
2095 * @instance: The instance owning the signal handler to be found.
2096 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2097 * and/or @data the handler has to match.
2098 * @signal_id: Signal the handler has to be connected to.
2099 * @detail: Signal detail the handler has to be connected to.
2100 * @closure: The closure the handler will invoke.
2101 * @func: The C closure callback of the handler (useless for non-C closures).
2102 * @data: The closure data of the handler's closure.
2104 * Finds the first signal handler that matches certain selection criteria.
2105 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2106 * flags, and the criteria values are passed as arguments.
2107 * The match @mask has to be non-0 for successful matches.
2108 * If no handler was found, 0 is returned.
2110 * Return value: A valid non-0 signal handler id for a successful match.
2113 g_signal_handler_find (gpointer instance,
2114 GSignalMatchType mask,
2121 gulong handler_seq_no = 0;
2123 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2124 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2126 if (mask & G_SIGNAL_MATCH_MASK)
2128 HandlerMatch *mlist;
2131 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2134 handler_seq_no = mlist->handler->sequential_number;
2135 handler_match_free1_R (mlist, instance);
2140 return handler_seq_no;
2144 signal_handlers_foreach_matched_R (gpointer instance,
2145 GSignalMatchType mask,
2151 void (*callback) (gpointer instance,
2152 gulong handler_seq_no))
2154 HandlerMatch *mlist;
2155 guint n_handlers = 0;
2157 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2161 if (mlist->handler->sequential_number)
2164 callback (instance, mlist->handler->sequential_number);
2167 mlist = handler_match_free1_R (mlist, instance);
2174 * g_signal_handlers_block_matched:
2175 * @instance: The instance to block handlers from.
2176 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2177 * and/or @data the handlers have to match.
2178 * @signal_id: Signal the handlers have to be connected to.
2179 * @detail: Signal detail the handlers have to be connected to.
2180 * @closure: The closure the handlers will invoke.
2181 * @func: The C closure callback of the handlers (useless for non-C closures).
2182 * @data: The closure data of the handlers' closures.
2184 * Blocks all handlers on an instance that match a certain selection criteria.
2185 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2186 * flags, and the criteria values are passed as arguments.
2187 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2188 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2189 * If no handlers were found, 0 is returned, the number of blocked handlers
2192 * Return value: The amount of handlers that got blocked.
2195 g_signal_handlers_block_matched (gpointer instance,
2196 GSignalMatchType mask,
2203 guint n_handlers = 0;
2205 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2206 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
2208 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2211 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2212 closure, func, data,
2213 g_signal_handler_block);
2221 * g_signal_handlers_unblock_matched:
2222 * @instance: The instance to unblock handlers from.
2223 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2224 * and/or @data the handlers have to match.
2225 * @signal_id: Signal the handlers have to be connected to.
2226 * @detail: Signal detail the handlers have to be connected to.
2227 * @closure: The closure the handlers will invoke.
2228 * @func: The C closure callback of the handlers (useless for non-C closures).
2229 * @data: The closure data of the handlers' closures.
2231 * Unblocks all handlers on an instance that match a certain selection
2232 * criteria. The criteria mask is passed as an OR-ed combination of
2233 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2234 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2235 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2236 * If no handlers were found, 0 is returned, the number of unblocked handlers
2237 * otherwise. The match criteria should not apply to any handlers that are
2238 * not currently blocked.
2240 * Return value: The amount of handlers that got unblocked.
2243 g_signal_handlers_unblock_matched (gpointer instance,
2244 GSignalMatchType mask,
2251 guint n_handlers = 0;
2253 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2254 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
2256 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2259 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2260 closure, func, data,
2261 g_signal_handler_unblock);
2269 * g_signal_handlers_disconnect_matched:
2270 * @instance: The instance to remove handlers from.
2271 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2272 * and/or @data the handlers have to match.
2273 * @signal_id: Signal the handlers have to be connected to.
2274 * @detail: Signal detail the handlers have to be connected to.
2275 * @closure: The closure the handlers will invoke.
2276 * @func: The C closure callback of the handlers (useless for non-C closures).
2277 * @data: The closure data of the handlers' closures.
2279 * Disconnects all handlers on an instance that match a certain selection
2280 * criteria. The criteria mask is passed as an OR-ed combination of
2281 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2282 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2283 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2284 * If no handlers were found, 0 is returned, the number of disconnected
2285 * handlers otherwise.
2287 * Return value: The amount of handlers that got disconnected.
2290 g_signal_handlers_disconnect_matched (gpointer instance,
2291 GSignalMatchType mask,
2298 guint n_handlers = 0;
2300 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2301 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
2303 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2306 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2307 closure, func, data,
2308 g_signal_handler_disconnect);
2316 * g_signal_has_handler_pending:
2317 * @instance: the object whose signal handlers are sought.
2318 * @signal_id: the signal id.
2319 * @detail: the detail.
2320 * @may_be_blocked: whether blocked handlers should count as match.
2322 * Returns whether there are any handlers connected to @instance for the
2323 * given signal id and detail.
2325 * One example of when you might use this is when the arguments to the
2326 * signal are difficult to compute. A class implementor may opt to not emit
2327 * the signal if no one is attached anyway, thus saving the cost of building
2330 * Return value: %TRUE if a handler is connected to the signal,
2334 g_signal_has_handler_pending (gpointer instance,
2337 gboolean may_be_blocked)
2339 HandlerMatch *mlist;
2340 gboolean has_pending;
2342 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2343 g_return_val_if_fail (signal_id > 0, FALSE);
2348 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2350 if (!(node->flags & G_SIGNAL_DETAILED))
2352 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2357 mlist = handlers_find (instance,
2358 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2359 signal_id, detail, NULL, NULL, NULL, TRUE);
2363 handler_match_free1_R (mlist, instance);
2366 has_pending = FALSE;
2374 * @instance_and_params: argument list for the signal emission. The first
2375 * element in the array is a #GValue for the instance the signal is
2376 * being emitted on. The rest are any arguments to be passed to the
2378 * @signal_id: the signal id
2379 * @detail: the detail
2380 * @return_value: Location to store the return value of the signal emission.
2384 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2385 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2388 g_signal_emitv (const GValue *instance_and_params,
2391 GValue *return_value)
2393 const GValue *param_values;
2396 #ifdef G_ENABLE_DEBUG
2400 g_return_if_fail (instance_and_params != NULL);
2401 instance = g_value_peek_pointer (instance_and_params);
2402 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2403 g_return_if_fail (signal_id > 0);
2405 param_values = instance_and_params + 1;
2408 node = LOOKUP_SIGNAL_NODE (signal_id);
2409 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2411 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2415 #ifdef G_ENABLE_DEBUG
2416 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2418 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2422 for (i = 0; i < node->n_params; i++)
2423 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2425 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2427 type_debug_name (node->param_types[i]),
2430 G_VALUE_TYPE_NAME (param_values + i));
2434 if (node->return_type != G_TYPE_NONE)
2438 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2440 type_debug_name (node->return_type),
2445 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2447 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2449 type_debug_name (node->return_type),
2451 G_VALUE_TYPE_NAME (return_value));
2457 return_value = NULL;
2458 #endif /* G_ENABLE_DEBUG */
2461 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2465 * g_signal_emit_valist:
2466 * @instance: the instance the signal is being emitted on.
2467 * @signal_id: the signal id
2468 * @detail: the detail
2469 * @var_args: a list of parameters to be passed to the signal, followed by a
2470 * location for the return value. If the return type of the signal
2471 * is #G_TYPE_NONE, the return value location can be omitted.
2475 * Note that g_signal_emit_valist() resets the return value to the default
2476 * if no handlers are connected, in contrast to g_signal_emitv().
2479 g_signal_emit_valist (gpointer instance,
2484 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2485 GType signal_return_type;
2486 GValue *param_values;
2490 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2491 g_return_if_fail (signal_id > 0);
2494 node = LOOKUP_SIGNAL_NODE (signal_id);
2495 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2497 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2501 #ifndef G_DISABLE_CHECKS
2502 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2504 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2508 #endif /* !G_DISABLE_CHECKS */
2510 n_params = node->n_params;
2511 signal_return_type = node->return_type;
2512 if (node->n_params < MAX_STACK_VALUES)
2513 instance_and_params = stack_values;
2516 free_me = g_new (GValue, node->n_params + 1);
2517 instance_and_params = free_me;
2519 param_values = instance_and_params + 1;
2520 for (i = 0; i < node->n_params; i++)
2523 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2524 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2526 param_values[i].g_type = 0;
2528 g_value_init (param_values + i, ptype);
2529 G_VALUE_COLLECT (param_values + i,
2531 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2535 g_warning ("%s: %s", G_STRLOC, error);
2538 /* we purposely leak the value here, it might not be
2539 * in a sane state if an error condition occoured
2542 g_value_unset (param_values + i);
2550 instance_and_params->g_type = 0;
2551 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2552 g_value_set_instance (instance_and_params, instance);
2553 if (signal_return_type == G_TYPE_NONE)
2554 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2557 GValue return_value = { 0, };
2558 gchar *error = NULL;
2559 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2560 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2562 g_value_init (&return_value, rtype);
2564 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2566 G_VALUE_LCOPY (&return_value,
2568 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2571 g_value_unset (&return_value);
2574 g_warning ("%s: %s", G_STRLOC, error);
2577 /* we purposely leak the value here, it might not be
2578 * in a sane state if an error condition occoured
2582 for (i = 0; i < n_params; i++)
2583 g_value_unset (param_values + i);
2584 g_value_unset (instance_and_params);
2591 * @instance: the instance the signal is being emitted on.
2592 * @signal_id: the signal id
2593 * @detail: the detail
2594 * @Varargs: parameters to be passed to the signal, followed by a
2595 * location for the return value. If the return type of the signal
2596 * is #G_TYPE_NONE, the return value location can be omitted.
2600 * Note that g_signal_emit() resets the return value to the default
2601 * if no handlers are connected, in contrast to g_signal_emitv().
2604 g_signal_emit (gpointer instance,
2611 va_start (var_args, detail);
2612 g_signal_emit_valist (instance, signal_id, detail, var_args);
2617 * g_signal_emit_by_name:
2618 * @instance: the instance the signal is being emitted on.
2619 * @detailed_signal: a string of the form "signal-name::detail".
2620 * @Varargs: parameters to be passed to the signal, followed by a
2621 * location for the return value. If the return type of the signal
2622 * is #G_TYPE_NONE, the return value location can be omitted.
2626 * Note that g_signal_emit_by_name() resets the return value to the default
2627 * if no handlers are connected, in contrast to g_signal_emitv().
2630 g_signal_emit_by_name (gpointer instance,
2631 const gchar *detailed_signal,
2637 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2638 g_return_if_fail (detailed_signal != NULL);
2641 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2648 va_start (var_args, detailed_signal);
2649 g_signal_emit_valist (instance, signal_id, detail, var_args);
2653 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2656 static inline gboolean
2657 accumulate (GSignalInvocationHint *ihint,
2658 GValue *return_accu,
2659 GValue *handler_return,
2660 SignalAccumulator *accumulator)
2662 gboolean continue_emission;
2667 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2668 g_value_reset (handler_return);
2670 return continue_emission;
2674 signal_emit_unlocked_R (SignalNode *node,
2677 GValue *emission_return,
2678 const GValue *instance_and_params)
2680 SignalAccumulator *accumulator;
2682 GClosure *class_closure;
2684 Handler *handler_list = NULL;
2685 GValue *return_accu, accu = { 0, };
2687 gulong max_sequential_handler_number;
2688 gboolean return_value_altered = FALSE;
2690 #ifdef G_ENABLE_DEBUG
2691 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2693 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2694 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2697 if (g_trap_instance_signals == instance)
2700 #endif /* G_ENABLE_DEBUG */
2703 signal_id = node->signal_id;
2704 if (node->flags & G_SIGNAL_NO_RECURSE)
2706 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2710 node->state = EMISSION_RESTART;
2712 return return_value_altered;
2715 accumulator = node->accumulator;
2719 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2720 return_accu = &accu;
2724 return_accu = emission_return;
2725 emission.instance = instance;
2726 emission.ihint.signal_id = node->signal_id;
2727 emission.ihint.detail = detail;
2728 emission.ihint.run_type = 0;
2730 emission.chain_type = G_TYPE_NONE;
2731 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2732 class_closure = signal_lookup_closure (node, instance);
2737 handler_unref_R (signal_id, instance, handler_list);
2738 max_sequential_handler_number = g_handler_sequential_number;
2739 hlist = handler_list_lookup (signal_id, instance);
2740 handler_list = hlist ? hlist->handlers : NULL;
2742 handler_ref (handler_list);
2744 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2746 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2748 emission.state = EMISSION_RUN;
2750 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2752 g_closure_invoke (class_closure,
2755 instance_and_params,
2757 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2758 emission.state == EMISSION_RUN)
2759 emission.state = EMISSION_STOP;
2761 emission.chain_type = G_TYPE_NONE;
2762 return_value_altered = TRUE;
2764 if (emission.state == EMISSION_STOP)
2766 else if (emission.state == EMISSION_RESTART)
2770 if (node->emission_hooks)
2772 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2775 emission.state = EMISSION_HOOK;
2776 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2779 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2781 if (!signal_hook->detail || signal_hook->detail == detail)
2783 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2785 was_in_call = G_HOOK_IN_CALL (hook);
2786 hook->flags |= G_HOOK_FLAG_IN_CALL;
2788 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2791 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2793 g_hook_destroy_link (node->emission_hooks, hook);
2795 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2798 if (emission.state == EMISSION_RESTART)
2804 Handler *handler = handler_list;
2806 emission.state = EMISSION_RUN;
2807 handler_ref (handler);
2814 handler_unref_R (signal_id, instance, handler_list);
2815 handler_list = handler;
2818 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2819 handler->sequential_number < max_sequential_handler_number)
2822 g_closure_invoke (handler->closure,
2825 instance_and_params,
2827 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2828 emission.state == EMISSION_RUN)
2829 emission.state = EMISSION_STOP;
2831 return_value_altered = TRUE;
2833 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2836 tmp = handler->next;
2840 handler_unref_R (signal_id, instance, handler_list);
2841 handler_list = handler;
2846 if (emission.state == EMISSION_STOP)
2848 else if (emission.state == EMISSION_RESTART)
2852 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2854 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2856 emission.state = EMISSION_RUN;
2858 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2860 g_closure_invoke (class_closure,
2863 instance_and_params,
2865 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2866 emission.state == EMISSION_RUN)
2867 emission.state = EMISSION_STOP;
2869 emission.chain_type = G_TYPE_NONE;
2870 return_value_altered = TRUE;
2872 if (emission.state == EMISSION_STOP)
2874 else if (emission.state == EMISSION_RESTART)
2880 Handler *handler = handler_list;
2882 emission.state = EMISSION_RUN;
2883 handler_ref (handler);
2888 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2889 handler->sequential_number < max_sequential_handler_number)
2892 g_closure_invoke (handler->closure,
2895 instance_and_params,
2897 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2898 emission.state == EMISSION_RUN)
2899 emission.state = EMISSION_STOP;
2901 return_value_altered = TRUE;
2903 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2906 tmp = handler->next;
2910 handler_unref_R (signal_id, instance, handler);
2915 if (emission.state == EMISSION_STOP)
2917 else if (emission.state == EMISSION_RESTART)
2923 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2925 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2927 gboolean need_unset = FALSE;
2929 emission.state = EMISSION_STOP;
2931 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2933 if (node->return_type != G_TYPE_NONE && !accumulator)
2935 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2938 g_closure_invoke (class_closure,
2939 node->return_type != G_TYPE_NONE ? &accu : NULL,
2941 instance_and_params,
2944 g_value_unset (&accu);
2946 emission.chain_type = G_TYPE_NONE;
2948 if (emission.state == EMISSION_RESTART)
2953 handler_unref_R (signal_id, instance, handler_list);
2955 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2958 g_value_unset (&accu);
2960 return return_value_altered;
2964 type_debug_name (GType type)
2968 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2969 return name ? name : "<unknown>";
2975 /* --- compile standard marshallers --- */
2976 #include "gobject.h"
2978 #include "gmarshal.c"