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>
33 #include "gbsearcharray.h"
34 #include "gvaluecollector.h"
35 #include "gvaluetypes.h"
39 #include "gobjectalias.h"
44 * @short_description: A means for customization of object behaviour
45 * and a general purpose notification mechanism
48 * The basic concept of the signal system is that of the
49 * <emphasis>emission</emphasis> of a signal. Signals are introduced
50 * per-type and are identified through strings. Signals introduced
51 * for a parent type are available in derived types as well, so
52 * basically they are a per-type facility that is inherited. A signal
53 * emission mainly involves invocation of a certain set of callbacks
54 * in precisely defined manner. There are two main categories of such
55 * callbacks, per-object
56 * <footnote><para>Although signals can deal with any kind of instantiatable
57 * type, i'm referring to those types as "object types" in the following,
58 * simply because that is the context most users will encounter signals in.
60 * ones and user provided ones.
61 * The per-object callbacks are most often referred to as "object method
62 * handler" or "default (signal) handler", while user provided callbacks are
63 * usually just called "signal handler".
64 * The object method handler is provided at signal creation time (this most
65 * frequently happens at the end of an object class' creation), while user
66 * provided handlers are frequently connected and disconnected to/from a certain
67 * signal on certain object instances.
69 * A signal emission consists of five stages, unless prematurely stopped:
71 * <varlistentry><term></term><listitem><para>
72 * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
73 * </para></listitem></varlistentry>
74 * <varlistentry><term></term><listitem><para>
75 * 2 - Invocation of normal user-provided signal handlers (<emphasis>after</emphasis> flag %FALSE)
76 * </para></listitem></varlistentry>
77 * <varlistentry><term></term><listitem><para>
78 * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
79 * </para></listitem></varlistentry>
80 * <varlistentry><term></term><listitem><para>
81 * 4 - Invocation of user provided signal handlers, connected with an <emphasis>after</emphasis> flag of %TRUE
82 * </para></listitem></varlistentry>
83 * <varlistentry><term></term><listitem><para>
84 * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
85 * </para></listitem></varlistentry>
87 * The user-provided signal handlers are called in the order they were
89 * All handlers may prematurely stop a signal emission, and any number of
90 * handlers may be connected, disconnected, blocked or unblocked during
92 * There are certain criteria for skipping user handlers in stages 2 and 4
93 * of a signal emission.
94 * First, user handlers may be <emphasis>blocked</emphasis>, blocked handlers are omitted
95 * during callback invocation, to return from the "blocked" state, a
96 * handler has to get unblocked exactly the same amount of times
97 * it has been blocked before.
98 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
99 * "detail" argument passed in to g_signal_emit() has to match the detail
100 * argument of the signal handler currently subject to invocation.
101 * Specification of no detail argument for signal handlers (omission of the
102 * detail part of the signal specification upon connection) serves as a
103 * wildcard and matches any detail argument passed in to emission.
107 /* pre allocation configurations
109 #define MAX_STACK_VALUES (16)
111 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
112 #ifdef G_ENABLE_DEBUG
113 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
114 static volatile gpointer g_trace_instance_signals = NULL;
115 static volatile gpointer g_trap_instance_signals = NULL;
116 #endif /* G_ENABLE_DEBUG */
119 /* --- typedefs --- */
120 typedef struct _SignalNode SignalNode;
121 typedef struct _SignalKey SignalKey;
122 typedef struct _Emission Emission;
123 typedef struct _Handler Handler;
124 typedef struct _HandlerList HandlerList;
125 typedef struct _HandlerMatch HandlerMatch;
135 /* --- prototypes --- */
136 static inline guint signal_id_lookup (GQuark quark,
138 static void signal_destroy_R (SignalNode *signal_node);
139 static inline HandlerList* handler_list_ensure (guint signal_id,
141 static inline HandlerList* handler_list_lookup (guint signal_id,
143 static inline Handler* handler_new (gboolean after);
144 static void handler_insert (guint signal_id,
147 static Handler* handler_lookup (gpointer instance,
150 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
153 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
155 static HandlerMatch* handlers_find (gpointer instance,
156 GSignalMatchType mask,
162 gboolean one_and_only);
163 static inline void handler_ref (Handler *handler);
164 static inline void handler_unref_R (guint signal_id,
167 static gint handler_lists_cmp (gconstpointer node1,
168 gconstpointer node2);
169 static inline void emission_push (Emission **emission_list_p,
171 static inline void emission_pop (Emission **emission_list_p,
173 static inline Emission* emission_find (Emission *emission_list,
177 static gint class_closures_cmp (gconstpointer node1,
178 gconstpointer node2);
179 static gint signal_key_cmp (gconstpointer node1,
180 gconstpointer node2);
181 static gboolean signal_emit_unlocked_R (SignalNode *node,
184 GValue *return_value,
185 const GValue *instance_and_params);
186 static const gchar * type_debug_name (GType type);
189 /* --- structures --- */
192 GSignalAccumulator func;
200 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
204 /* permanent portion */
210 /* reinitializable portion */
211 guint test_class_offset : 12;
214 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
215 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
216 GBSearchArray *class_closure_bsa;
217 SignalAccumulator *accumulator;
218 GSignalCMarshaller c_marshaller;
219 GHookList *emission_hooks;
221 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
222 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
235 GSignalInvocationHint ihint;
244 Handler *tail_before; /* normal signal handlers are appended here */
245 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
250 gulong sequential_number;
255 guint block_count : 16;
256 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
269 GType instance_type; /* 0 for default closure */
274 /* --- variables --- */
275 static GBSearchArray *g_signal_key_bsa = NULL;
276 static const GBSearchConfig g_signal_key_bconfig = {
279 G_BSEARCH_ARRAY_ALIGN_POWER2,
281 static GBSearchConfig g_signal_hlbsa_bconfig = {
282 sizeof (HandlerList),
286 static GBSearchConfig g_class_closure_bconfig = {
287 sizeof (ClassClosure),
291 static GHashTable *g_handler_list_bsa_ht = NULL;
292 static Emission *g_recursive_emissions = NULL;
293 static Emission *g_restart_emissions = NULL;
294 static gulong g_handler_sequential_number = 1;
295 G_LOCK_DEFINE_STATIC (g_signal_mutex);
296 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
297 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
300 /* --- signal nodes --- */
301 static guint g_n_signal_nodes = 0;
302 static SignalNode **g_signal_nodes = NULL;
304 static inline SignalNode*
305 LOOKUP_SIGNAL_NODE (register guint signal_id)
307 if (signal_id < g_n_signal_nodes)
308 return g_signal_nodes[signal_id];
314 /* --- functions --- */
316 signal_id_lookup (GQuark quark,
319 GType *ifaces, type = itype;
325 /* try looking up signals for this type and its ancestors */
328 SignalKey *signal_key;
331 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
334 return signal_key->signal_id;
336 type = g_type_parent (type);
340 /* no luck, try interfaces it exports */
341 ifaces = g_type_interfaces (itype, &n_ifaces);
344 SignalKey *signal_key;
346 key.itype = ifaces[n_ifaces];
347 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
352 return signal_key->signal_id;
361 class_closures_cmp (gconstpointer node1,
364 const ClassClosure *c1 = node1, *c2 = node2;
366 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
370 handler_lists_cmp (gconstpointer node1,
373 const HandlerList *hlist1 = node1, *hlist2 = node2;
375 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
378 static inline HandlerList*
379 handler_list_ensure (guint signal_id,
382 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
385 key.signal_id = signal_id;
387 key.tail_before = NULL;
388 key.tail_after = NULL;
391 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
392 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
393 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
397 GBSearchArray *o = hlbsa;
399 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
401 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
403 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
406 static inline HandlerList*
407 handler_list_lookup (guint signal_id,
410 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
413 key.signal_id = signal_id;
415 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
419 handler_lookup (gpointer instance,
423 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
429 for (i = 0; i < hlbsa->n_nodes; i++)
431 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
434 for (handler = hlist->handlers; handler; handler = handler->next)
435 if (handler->sequential_number == handler_id)
438 *signal_id_p = hlist->signal_id;
448 static inline HandlerMatch*
449 handler_match_prepend (HandlerMatch *list,
455 node = g_slice_new (HandlerMatch);
456 node->handler = handler;
458 node->signal_id = signal_id;
459 handler_ref (handler);
463 static inline HandlerMatch*
464 handler_match_free1_R (HandlerMatch *node,
467 HandlerMatch *next = node->next;
469 handler_unref_R (node->signal_id, instance, node->handler);
470 g_slice_free (HandlerMatch, node);
476 handlers_find (gpointer instance,
477 GSignalMatchType mask,
483 gboolean one_and_only)
485 HandlerMatch *mlist = NULL;
487 if (mask & G_SIGNAL_MATCH_ID)
489 HandlerList *hlist = handler_list_lookup (signal_id, instance);
491 SignalNode *node = NULL;
493 if (mask & G_SIGNAL_MATCH_FUNC)
495 node = LOOKUP_SIGNAL_NODE (signal_id);
496 if (!node || !node->c_marshaller)
501 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
502 if (handler->sequential_number &&
503 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
504 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
505 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
506 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
507 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
508 handler->closure->meta_marshal == 0 &&
509 ((GCClosure*) handler->closure)->callback == func)))
511 mlist = handler_match_prepend (mlist, handler, signal_id);
518 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
525 for (i = 0; i < hlbsa->n_nodes; i++)
527 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
528 SignalNode *node = NULL;
531 if (!(mask & G_SIGNAL_MATCH_FUNC))
533 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
534 if (!node->c_marshaller)
538 for (handler = hlist->handlers; handler; handler = handler->next)
539 if (handler->sequential_number &&
540 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
541 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
542 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
543 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
544 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
545 handler->closure->meta_marshal == 0 &&
546 ((GCClosure*) handler->closure)->callback == func)))
548 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
559 static inline Handler*
560 handler_new (gboolean after)
562 Handler *handler = g_slice_new (Handler);
563 #ifndef G_DISABLE_CHECKS
564 if (g_handler_sequential_number < 1)
565 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
568 handler->sequential_number = g_handler_sequential_number++;
569 handler->prev = NULL;
570 handler->next = NULL;
572 handler->ref_count = 1;
573 handler->block_count = 0;
574 handler->after = after != FALSE;
575 handler->closure = NULL;
581 handler_ref (Handler *handler)
583 g_return_if_fail (handler->ref_count > 0);
585 g_atomic_int_inc (&handler->ref_count);
589 handler_unref_R (guint signal_id,
595 g_return_if_fail (handler->ref_count > 0);
597 is_zero = g_atomic_int_dec_and_test (&handler->ref_count);
599 if (G_UNLIKELY (is_zero))
601 HandlerList *hlist = NULL;
604 handler->next->prev = handler->prev;
605 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
606 handler->prev->next = handler->next;
609 hlist = handler_list_lookup (signal_id, instance);
610 hlist->handlers = handler->next;
615 /* check if we are removing the handler pointed to by tail_before */
616 if (!handler->after && (!handler->next || handler->next->after))
619 hlist = handler_list_lookup (signal_id, instance);
622 g_assert (hlist->tail_before == handler); /* paranoid */
623 hlist->tail_before = handler->prev;
627 /* check if we are removing the handler pointed to by tail_after */
631 hlist = handler_list_lookup (signal_id, instance);
634 g_assert (hlist->tail_after == handler); /* paranoid */
635 hlist->tail_after = handler->prev;
641 g_closure_unref (handler->closure);
643 g_slice_free (Handler, handler);
648 handler_insert (guint signal_id,
654 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
656 hlist = handler_list_ensure (signal_id, instance);
657 if (!hlist->handlers)
659 hlist->handlers = handler;
661 hlist->tail_before = handler;
663 else if (handler->after)
665 handler->prev = hlist->tail_after;
666 hlist->tail_after->next = handler;
670 if (hlist->tail_before)
672 handler->next = hlist->tail_before->next;
674 handler->next->prev = handler;
675 handler->prev = hlist->tail_before;
676 hlist->tail_before->next = handler;
678 else /* insert !after handler into a list of only after handlers */
680 handler->next = hlist->handlers;
682 handler->next->prev = handler;
683 hlist->handlers = handler;
685 hlist->tail_before = handler;
689 hlist->tail_after = handler;
693 emission_push (Emission **emission_list_p,
696 emission->next = *emission_list_p;
697 *emission_list_p = emission;
701 emission_pop (Emission **emission_list_p,
704 Emission *node, *last = NULL;
706 for (node = *emission_list_p; node; last = node, node = last->next)
707 if (node == emission)
710 last->next = node->next;
712 *emission_list_p = node->next;
715 g_assert_not_reached ();
718 static inline Emission*
719 emission_find (Emission *emission_list,
726 for (emission = emission_list; emission; emission = emission->next)
727 if (emission->instance == instance &&
728 emission->ihint.signal_id == signal_id &&
729 emission->ihint.detail == detail)
734 static inline Emission*
735 emission_find_innermost (gpointer instance)
737 Emission *emission, *s = NULL, *c = NULL;
739 for (emission = g_restart_emissions; emission; emission = emission->next)
740 if (emission->instance == instance)
745 for (emission = g_recursive_emissions; emission; emission = emission->next)
746 if (emission->instance == instance)
756 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
760 signal_key_cmp (gconstpointer node1,
763 const SignalKey *key1 = node1, *key2 = node2;
765 if (key1->itype == key2->itype)
766 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
768 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
775 if (!g_n_signal_nodes)
777 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
778 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
779 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
781 /* invalid (0) signal_id */
782 g_n_signal_nodes = 1;
783 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
784 g_signal_nodes[0] = NULL;
790 _g_signals_destroy (GType itype)
795 for (i = 1; i < g_n_signal_nodes; i++)
797 SignalNode *node = g_signal_nodes[i];
799 if (node->itype == itype)
802 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
804 type_debug_name (node->itype));
806 signal_destroy_R (node);
813 * g_signal_stop_emission:
814 * @instance: the object whose signal handlers you wish to stop.
815 * @signal_id: the signal identifier, as returned by g_signal_lookup().
816 * @detail: the detail which the signal was emitted with.
818 * Stops a signal's current emission.
820 * This will prevent the default method from running, if the signal was
821 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
824 * Prints a warning if used on a signal which isn't being emitted.
827 g_signal_stop_emission (gpointer instance,
833 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
834 g_return_if_fail (signal_id > 0);
837 node = LOOKUP_SIGNAL_NODE (signal_id);
838 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
840 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
844 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
846 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
847 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
851 if (emission->state == EMISSION_HOOK)
852 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
853 node->name, instance);
854 else if (emission->state == EMISSION_RUN)
855 emission->state = EMISSION_STOP;
858 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
859 node->name, instance);
862 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
867 signal_finalize_hook (GHookList *hook_list,
870 GDestroyNotify destroy = hook->destroy;
874 hook->destroy = NULL;
876 destroy (hook->data);
882 * g_signal_add_emission_hook:
883 * @signal_id: the signal identifier, as returned by g_signal_lookup().
884 * @detail: the detail on which to call the hook.
885 * @hook_func: a #GSignalEmissionHook function.
886 * @hook_data: user data for @hook_func.
887 * @data_destroy: a #GDestroyNotify for @hook_data.
889 * Adds an emission hook for a signal, which will get called for any emission
890 * of that signal, independent of the instance. This is possible only
891 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
893 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
896 g_signal_add_emission_hook (guint signal_id,
898 GSignalEmissionHook hook_func,
900 GDestroyNotify data_destroy)
902 static gulong seq_hook_id = 1;
905 SignalHook *signal_hook;
907 g_return_val_if_fail (signal_id > 0, 0);
908 g_return_val_if_fail (hook_func != NULL, 0);
911 node = LOOKUP_SIGNAL_NODE (signal_id);
912 if (!node || node->destroyed)
914 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
918 if (node->flags & G_SIGNAL_NO_HOOKS)
920 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
924 if (detail && !(node->flags & G_SIGNAL_DETAILED))
926 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
930 if (!node->emission_hooks)
932 node->emission_hooks = g_new (GHookList, 1);
933 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
934 node->emission_hooks->finalize_hook = signal_finalize_hook;
936 hook = g_hook_alloc (node->emission_hooks);
937 hook->data = hook_data;
938 hook->func = (gpointer) hook_func;
939 hook->destroy = data_destroy;
940 signal_hook = SIGNAL_HOOK (hook);
941 signal_hook->detail = detail;
942 node->emission_hooks->seq_id = seq_hook_id;
943 g_hook_append (node->emission_hooks, hook);
944 seq_hook_id = node->emission_hooks->seq_id;
947 return hook->hook_id;
951 * g_signal_remove_emission_hook:
952 * @signal_id: the id of the signal
953 * @hook_id: the id of the emission hook, as returned by
954 * g_signal_add_emission_hook()
956 * Deletes an emission hook.
959 g_signal_remove_emission_hook (guint signal_id,
964 g_return_if_fail (signal_id > 0);
965 g_return_if_fail (hook_id > 0);
968 node = LOOKUP_SIGNAL_NODE (signal_id);
969 if (!node || node->destroyed)
970 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
971 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
972 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
977 signal_parse_name (const gchar *name,
980 gboolean force_quark)
982 const gchar *colon = strchr (name, ':');
987 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
988 if (signal_id && detail_p)
991 else if (colon[1] == ':')
994 guint l = colon - name;
998 memcpy (buffer, name, l);
1000 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1004 gchar *signal = g_new (gchar, l + 1);
1006 memcpy (signal, name, l);
1008 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1012 if (signal_id && detail_p)
1013 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1021 * g_signal_parse_name:
1022 * @detailed_signal: a string of the form "signal-name::detail".
1023 * @itype: The interface/instance type that introduced "signal-name".
1024 * @signal_id_p: Location to store the signal id.
1025 * @detail_p: Location to store the detail quark.
1026 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1028 * Internal function to parse a signal name into its @signal_id
1029 * and @detail quark.
1031 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1034 g_signal_parse_name (const gchar *detailed_signal,
1038 gboolean force_detail_quark)
1044 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1045 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1048 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1051 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1052 if (!node || node->destroyed ||
1053 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1057 *signal_id_p = signal_id;
1065 * g_signal_stop_emission_by_name:
1066 * @instance: the object whose signal handlers you wish to stop.
1067 * @detailed_signal: a string of the form "signal-name::detail".
1069 * Stops a signal's current emission.
1071 * This is just like g_signal_stop_emission() except it will look up the
1072 * signal id for you.
1075 g_signal_stop_emission_by_name (gpointer instance,
1076 const gchar *detailed_signal)
1082 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1083 g_return_if_fail (detailed_signal != NULL);
1086 itype = G_TYPE_FROM_INSTANCE (instance);
1087 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1090 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1092 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1093 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1094 else if (!g_type_is_a (itype, node->itype))
1095 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1098 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1099 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1103 if (emission->state == EMISSION_HOOK)
1104 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1105 node->name, instance);
1106 else if (emission->state == EMISSION_RUN)
1107 emission->state = EMISSION_STOP;
1110 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1111 node->name, instance);
1115 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1121 * @name: the signal's name.
1122 * @itype: the type that the signal operates on.
1124 * Given the name of the signal and the type of object it connects to, gets
1125 * the signal's identifying integer. Emitting the signal by number is
1126 * somewhat faster than using the name each time.
1128 * Also tries the ancestors of the given type.
1130 * See g_signal_new() for details on allowed signal names.
1132 * Returns: the signal's identifying number, or 0 if no signal was found.
1135 g_signal_lookup (const gchar *name,
1139 g_return_val_if_fail (name != NULL, 0);
1140 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1143 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1147 /* give elaborate warnings */
1148 if (!g_type_name (itype))
1149 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'",
1151 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1152 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1153 name, g_type_name (itype));
1154 else if (!g_type_class_peek (itype))
1155 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1156 name, g_type_name (itype));
1163 * g_signal_list_ids:
1164 * @itype: Instance or interface type.
1165 * @n_ids: Location to store the number of signal ids for @itype.
1167 * Lists the signals by id that a certain instance or interface type
1168 * created. Further information about the signals can be acquired through
1171 * Returns: Newly allocated array of signal IDs.
1174 g_signal_list_ids (GType itype,
1182 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1183 g_return_val_if_fail (n_ids != NULL, NULL);
1186 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1187 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1188 result = g_array_new (FALSE, FALSE, sizeof (guint));
1190 for (i = 0; i < n_nodes; i++)
1191 if (keys[i].itype == itype)
1193 const gchar *name = g_quark_to_string (keys[i].quark);
1195 /* Signal names with "_" in them are aliases to the same
1196 * name with "-" instead of "_".
1198 if (!strchr (name, '_'))
1199 g_array_append_val (result, keys[i].signal_id);
1201 *n_ids = result->len;
1205 /* give elaborate warnings */
1206 if (!g_type_name (itype))
1207 g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'",
1209 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1210 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1211 g_type_name (itype));
1212 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1213 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1214 g_type_name (itype));
1217 return (guint*) g_array_free (result, FALSE);
1222 * @signal_id: the signal's identifying number.
1224 * Given the signal's identifier, finds its name.
1226 * Two different signals may have the same name, if they have differing types.
1228 * Returns: the signal name, or %NULL if the signal number was invalid.
1230 G_CONST_RETURN gchar*
1231 g_signal_name (guint signal_id)
1237 node = LOOKUP_SIGNAL_NODE (signal_id);
1238 name = node ? node->name : NULL;
1241 return (char*) name;
1246 * @signal_id: The signal id of the signal to query information for.
1247 * @query: A user provided structure that is filled in with constant
1248 * values upon success.
1250 * Queries the signal system for in-depth information about a
1251 * specific signal. This function will fill in a user-provided
1252 * structure to hold signal-specific information. If an invalid
1253 * signal id is passed in, the @signal_id member of the #GSignalQuery
1254 * is 0. All members filled into the #GSignalQuery structure should
1255 * be considered constant and have to be left untouched.
1258 g_signal_query (guint signal_id,
1259 GSignalQuery *query)
1263 g_return_if_fail (query != NULL);
1266 node = LOOKUP_SIGNAL_NODE (signal_id);
1267 if (!node || node->destroyed)
1268 query->signal_id = 0;
1271 query->signal_id = node->signal_id;
1272 query->signal_name = node->name;
1273 query->itype = node->itype;
1274 query->signal_flags = node->flags;
1275 query->return_type = node->return_type;
1276 query->n_params = node->n_params;
1277 query->param_types = node->param_types;
1284 * @signal_name: the name for the signal
1285 * @itype: the type this signal pertains to. It will also pertain to
1286 * types which are derived from this type.
1287 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1288 * the default handler is to be invoked. You should at least specify
1289 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1290 * @class_offset: The offset of the function pointer in the class structure
1291 * for this type. Used to invoke a class method generically. Pass 0 to
1292 * not associate a class method with this signal.
1293 * @accumulator: the accumulator for this signal; may be %NULL.
1294 * @accu_data: user data for the @accumulator.
1295 * @c_marshaller: the function to translate arrays of parameter values to
1296 * signal emissions into C language callback invocations.
1297 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1298 * without a return value.
1299 * @n_params: the number of parameter types to follow.
1300 * @...: a list of types, one for each parameter.
1302 * Creates a new signal. (This is usually done in the class initializer.)
1304 * A signal name consists of segments consisting of ASCII letters and
1305 * digits, separated by either the '-' or '_' character. The first
1306 * character of a signal name must be a letter. Names which violate these
1307 * rules lead to undefined behaviour of the GSignal system.
1309 * When registering a signal and looking up a signal, either separator can
1310 * be used, but they cannot be mixed.
1312 * Returns: the signal id
1315 g_signal_new (const gchar *signal_name,
1317 GSignalFlags signal_flags,
1319 GSignalAccumulator accumulator,
1321 GSignalCMarshaller c_marshaller,
1329 g_return_val_if_fail (signal_name != NULL, 0);
1331 va_start (args, n_params);
1333 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1334 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1335 accumulator, accu_data, c_marshaller,
1336 return_type, n_params, args);
1340 /* optimize NOP emissions with NULL class handlers */
1341 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1342 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1347 node = LOOKUP_SIGNAL_NODE (signal_id);
1348 node->test_class_offset = class_offset;
1355 static inline ClassClosure*
1356 signal_find_class_closure (SignalNode *node,
1359 GBSearchArray *bsa = node->class_closure_bsa;
1366 /* cc->instance_type is 0 for default closure */
1368 key.instance_type = itype;
1369 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1370 while (!cc && key.instance_type)
1372 key.instance_type = g_type_parent (key.instance_type);
1373 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1381 static inline GClosure*
1382 signal_lookup_closure (SignalNode *node,
1383 GTypeInstance *instance)
1387 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1388 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1390 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1391 return cc ? cc->closure : NULL;
1395 signal_add_class_closure (SignalNode *node,
1401 /* can't optimize NOP emissions with overridden class closures */
1402 node->test_class_offset = 0;
1404 if (!node->class_closure_bsa)
1405 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1406 key.instance_type = itype;
1407 key.closure = g_closure_ref (closure);
1408 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1409 &g_class_closure_bconfig,
1411 g_closure_sink (closure);
1412 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1413 g_closure_set_marshal (closure, node->c_marshaller);
1418 * @signal_name: the name for the signal
1419 * @itype: the type this signal pertains to. It will also pertain to
1420 * types which are derived from this type.
1421 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1422 * the default handler is to be invoked. You should at least specify
1423 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1424 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1425 * @accumulator: the accumulator for this signal; may be %NULL.
1426 * @accu_data: user data for the @accumulator.
1427 * @c_marshaller: the function to translate arrays of parameter values to
1428 * signal emissions into C language callback invocations.
1429 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1430 * without a return value.
1431 * @n_params: the length of @param_types.
1432 * @param_types: an array types, one for each parameter.
1434 * Creates a new signal. (This is usually done in the class initializer.)
1436 * See g_signal_new() for details on allowed signal names.
1438 * Returns: the signal id
1441 g_signal_newv (const gchar *signal_name,
1443 GSignalFlags signal_flags,
1444 GClosure *class_closure,
1445 GSignalAccumulator accumulator,
1447 GSignalCMarshaller c_marshaller,
1456 g_return_val_if_fail (signal_name != NULL, 0);
1457 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1459 g_return_val_if_fail (param_types != NULL, 0);
1460 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1461 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1462 g_return_val_if_fail (accumulator == NULL, 0);
1464 g_return_val_if_fail (accu_data == NULL, 0);
1466 name = g_strdup (signal_name);
1467 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1471 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1472 node = LOOKUP_SIGNAL_NODE (signal_id);
1473 if (node && !node->destroyed)
1475 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1477 type_debug_name (node->itype),
1478 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1483 if (node && node->itype != itype)
1485 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1487 type_debug_name (itype),
1488 type_debug_name (node->itype));
1493 for (i = 0; i < n_params; i++)
1494 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1496 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1497 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1502 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1504 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1505 type_debug_name (return_type), type_debug_name (itype), name);
1510 if (return_type != G_TYPE_NONE &&
1511 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1513 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1514 type_debug_name (itype), name, type_debug_name (return_type));
1520 /* setup permanent portion of signal node */
1525 signal_id = g_n_signal_nodes++;
1526 node = g_new (SignalNode, 1);
1527 node->signal_id = signal_id;
1528 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1529 g_signal_nodes[signal_id] = node;
1530 node->itype = itype;
1533 key.quark = g_quark_from_string (node->name);
1534 key.signal_id = signal_id;
1535 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1536 g_strdelimit (name, "_", '-');
1537 node->name = g_intern_string (name);
1538 key.quark = g_quark_from_string (name);
1539 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1541 node->destroyed = FALSE;
1542 node->test_class_offset = 0;
1544 /* setup reinitializable portion */
1545 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1546 node->n_params = n_params;
1547 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1548 node->return_type = return_type;
1549 node->class_closure_bsa = NULL;
1552 node->accumulator = g_new (SignalAccumulator, 1);
1553 node->accumulator->func = accumulator;
1554 node->accumulator->data = accu_data;
1557 node->accumulator = NULL;
1558 node->c_marshaller = c_marshaller;
1559 node->emission_hooks = NULL;
1561 signal_add_class_closure (node, 0, class_closure);
1562 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1564 /* optimize NOP emissions */
1565 node->test_class_offset = TEST_CLASS_MAGIC;
1575 * g_signal_new_valist:
1576 * @signal_name: the name for the signal
1577 * @itype: the type this signal pertains to. It will also pertain to
1578 * types which are derived from this type.
1579 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1580 * the default handler is to be invoked. You should at least specify
1581 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1582 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1583 * @accumulator: the accumulator for this signal; may be %NULL.
1584 * @accu_data: user data for the @accumulator.
1585 * @c_marshaller: the function to translate arrays of parameter values to
1586 * signal emissions into C language callback invocations.
1587 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1588 * without a return value.
1589 * @n_params: the number of parameter types in @args.
1590 * @args: va_list of #GType, one for each parameter.
1592 * Creates a new signal. (This is usually done in the class initializer.)
1594 * See g_signal_new() for details on allowed signal names.
1596 * Returns: the signal id
1599 g_signal_new_valist (const gchar *signal_name,
1601 GSignalFlags signal_flags,
1602 GClosure *class_closure,
1603 GSignalAccumulator accumulator,
1605 GSignalCMarshaller c_marshaller,
1616 param_types = g_new (GType, n_params);
1618 for (i = 0; i < n_params; i++)
1619 param_types[i] = va_arg (args, GType);
1624 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1625 class_closure, accumulator, accu_data, c_marshaller,
1626 return_type, n_params, param_types);
1627 g_free (param_types);
1633 signal_destroy_R (SignalNode *signal_node)
1635 SignalNode node = *signal_node;
1637 signal_node->destroyed = TRUE;
1639 /* reentrancy caution, zero out real contents first */
1640 signal_node->test_class_offset = 0;
1641 signal_node->n_params = 0;
1642 signal_node->param_types = NULL;
1643 signal_node->return_type = 0;
1644 signal_node->class_closure_bsa = NULL;
1645 signal_node->accumulator = NULL;
1646 signal_node->c_marshaller = NULL;
1647 signal_node->emission_hooks = NULL;
1649 #ifdef G_ENABLE_DEBUG
1650 /* check current emissions */
1654 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1655 emission; emission = emission->next)
1656 if (emission->ihint.signal_id == node.signal_id)
1657 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1658 node.name, emission->instance);
1662 /* free contents that need to
1665 g_free (node.param_types);
1666 if (node.class_closure_bsa)
1670 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1672 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1674 g_closure_unref (cc->closure);
1676 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1678 g_free (node.accumulator);
1679 if (node.emission_hooks)
1681 g_hook_list_clear (node.emission_hooks);
1682 g_free (node.emission_hooks);
1688 * g_signal_override_class_closure:
1689 * @signal_id: the signal id
1690 * @instance_type: the instance type on which to override the class closure
1692 * @class_closure: the closure.
1694 * Overrides the class closure (i.e. the default handler) for the given signal
1695 * for emissions on instances of @instance_type. @instance_type must be derived
1696 * from the type to which the signal belongs.
1699 g_signal_override_class_closure (guint signal_id,
1700 GType instance_type,
1701 GClosure *class_closure)
1705 g_return_if_fail (signal_id > 0);
1706 g_return_if_fail (class_closure != NULL);
1709 node = LOOKUP_SIGNAL_NODE (signal_id);
1710 if (!g_type_is_a (instance_type, node->itype))
1711 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1714 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1716 if (cc && cc->instance_type == instance_type)
1717 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1719 signal_add_class_closure (node, instance_type, class_closure);
1725 * g_signal_chain_from_overridden:
1726 * @instance_and_params: the argument list of the signal emission. The first
1727 * element in the array is a #GValue for the instance the signal is being
1728 * emitted on. The rest are any arguments to be passed to the signal.
1729 * @return_value: Location for the return value.
1731 * Calls the original class closure of a signal. This function should only
1732 * be called from an overridden class closure; see
1733 * g_signal_override_class_closure().
1736 g_signal_chain_from_overridden (const GValue *instance_and_params,
1737 GValue *return_value)
1739 GType chain_type = 0, restore_type = 0;
1740 Emission *emission = NULL;
1741 GClosure *closure = NULL;
1745 g_return_if_fail (instance_and_params != NULL);
1746 instance = g_value_peek_pointer (instance_and_params);
1747 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1750 emission = emission_find_innermost (instance);
1753 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1755 g_assert (node != NULL); /* paranoid */
1757 /* we should probably do the same parameter checks as g_signal_emit() here.
1759 if (emission->chain_type != G_TYPE_NONE)
1761 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1763 g_assert (cc != NULL); /* closure currently in call stack */
1765 n_params = node->n_params;
1766 restore_type = cc->instance_type;
1767 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1768 if (cc && cc->instance_type != restore_type)
1770 closure = cc->closure;
1771 chain_type = cc->instance_type;
1775 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1778 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1781 emission->chain_type = chain_type;
1783 g_closure_invoke (closure,
1786 instance_and_params,
1789 emission->chain_type = restore_type;
1795 * g_signal_get_invocation_hint:
1796 * @instance: the instance to query
1798 * Returns the invocation hint of the innermost signal emission of instance.
1800 * Returns: the invocation hint of the innermost signal emission.
1802 GSignalInvocationHint*
1803 g_signal_get_invocation_hint (gpointer instance)
1805 Emission *emission = NULL;
1807 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1810 emission = emission_find_innermost (instance);
1813 return emission ? &emission->ihint : NULL;
1817 * g_signal_connect_closure_by_id:
1818 * @instance: the instance to connect to.
1819 * @signal_id: the id of the signal.
1820 * @detail: the detail.
1821 * @closure: the closure to connect.
1822 * @after: whether the handler should be called before or after the
1823 * default handler of the signal.
1825 * Connects a closure to a signal for a particular object.
1827 * Returns: the handler id
1830 g_signal_connect_closure_by_id (gpointer instance,
1837 gulong handler_seq_no = 0;
1839 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1840 g_return_val_if_fail (signal_id > 0, 0);
1841 g_return_val_if_fail (closure != NULL, 0);
1844 node = LOOKUP_SIGNAL_NODE (signal_id);
1847 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1848 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1849 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1850 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1853 Handler *handler = handler_new (after);
1855 handler_seq_no = handler->sequential_number;
1856 handler->detail = detail;
1857 handler->closure = g_closure_ref (closure);
1858 g_closure_sink (closure);
1859 handler_insert (signal_id, instance, handler);
1860 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1861 g_closure_set_marshal (closure, node->c_marshaller);
1865 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1868 return handler_seq_no;
1872 * g_signal_connect_closure:
1873 * @instance: the instance to connect to.
1874 * @detailed_signal: a string of the form "signal-name::detail".
1875 * @closure: the closure to connect.
1876 * @after: whether the handler should be called before or after the
1877 * default handler of the signal.
1879 * Connects a closure to a signal for a particular object.
1881 * Returns: the handler id
1884 g_signal_connect_closure (gpointer instance,
1885 const gchar *detailed_signal,
1890 gulong handler_seq_no = 0;
1894 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1895 g_return_val_if_fail (detailed_signal != NULL, 0);
1896 g_return_val_if_fail (closure != NULL, 0);
1899 itype = G_TYPE_FROM_INSTANCE (instance);
1900 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1903 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1905 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1906 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1907 else if (!g_type_is_a (itype, node->itype))
1908 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1911 Handler *handler = handler_new (after);
1913 handler_seq_no = handler->sequential_number;
1914 handler->detail = detail;
1915 handler->closure = g_closure_ref (closure);
1916 g_closure_sink (closure);
1917 handler_insert (signal_id, instance, handler);
1918 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1919 g_closure_set_marshal (handler->closure, node->c_marshaller);
1923 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1926 return handler_seq_no;
1930 * g_signal_connect_data:
1931 * @instance: the instance to connect to.
1932 * @detailed_signal: a string of the form "signal-name::detail".
1933 * @c_handler: the #GCallback to connect.
1934 * @data: data to pass to @c_handler calls.
1935 * @destroy_data: a #GClosureNotify for @data.
1936 * @connect_flags: a combination of #GConnectFlags.
1938 * Connects a #GCallback function to a signal for a particular object. Similar
1939 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
1940 * which will be called when the signal handler is disconnected and no longer
1941 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
1942 * <literal>..._swapped()</literal> variants of this function.
1944 * Returns: the handler id
1947 g_signal_connect_data (gpointer instance,
1948 const gchar *detailed_signal,
1949 GCallback c_handler,
1951 GClosureNotify destroy_data,
1952 GConnectFlags connect_flags)
1955 gulong handler_seq_no = 0;
1958 gboolean swapped, after;
1960 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1961 g_return_val_if_fail (detailed_signal != NULL, 0);
1962 g_return_val_if_fail (c_handler != NULL, 0);
1964 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1965 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1968 itype = G_TYPE_FROM_INSTANCE (instance);
1969 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1972 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1974 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1975 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1976 else if (!g_type_is_a (itype, node->itype))
1977 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1980 Handler *handler = handler_new (after);
1982 handler_seq_no = handler->sequential_number;
1983 handler->detail = detail;
1984 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1985 g_closure_sink (handler->closure);
1986 handler_insert (signal_id, instance, handler);
1987 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1988 g_closure_set_marshal (handler->closure, node->c_marshaller);
1992 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1995 return handler_seq_no;
1999 * g_signal_handler_block:
2000 * @instance: The instance to block the signal handler of.
2001 * @handler_id: Handler id of the handler to be blocked.
2003 * Blocks a handler of an instance so it will not be called during any
2004 * signal emissions unless it is unblocked again. Thus "blocking" a
2005 * signal handler means to temporarily deactive it, a signal handler
2006 * has to be unblocked exactly the same amount of times it has been
2007 * blocked before to become active again.
2009 * The @handler_id has to be a valid signal handler id, connected to a
2010 * signal of @instance.
2013 g_signal_handler_block (gpointer instance,
2018 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2019 g_return_if_fail (handler_id > 0);
2022 handler = handler_lookup (instance, handler_id, NULL);
2025 #ifndef G_DISABLE_CHECKS
2026 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2027 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2029 handler->block_count += 1;
2032 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2037 * g_signal_handler_unblock:
2038 * @instance: The instance to unblock the signal handler of.
2039 * @handler_id: Handler id of the handler to be unblocked.
2041 * Undoes the effect of a previous g_signal_handler_block() call. A
2042 * blocked handler is skipped during signal emissions and will not be
2043 * invoked, unblocking it (for exactly the amount of times it has been
2044 * blocked before) reverts its "blocked" state, so the handler will be
2045 * recognized by the signal system and is called upon future or
2046 * currently ongoing signal emissions (since the order in which
2047 * handlers are called during signal emissions is deterministic,
2048 * whether the unblocked handler in question is called as part of a
2049 * currently ongoing emission depends on how far that emission has
2052 * The @handler_id has to be a valid id of a signal handler that is
2053 * connected to a signal of @instance and is currently blocked.
2056 g_signal_handler_unblock (gpointer instance,
2061 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2062 g_return_if_fail (handler_id > 0);
2065 handler = handler_lookup (instance, handler_id, NULL);
2068 if (handler->block_count)
2069 handler->block_count -= 1;
2071 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2074 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2079 * g_signal_handler_disconnect:
2080 * @instance: The instance to remove the signal handler from.
2081 * @handler_id: Handler id of the handler to be disconnected.
2083 * Disconnects a handler from an instance so it will not be called during
2084 * any future or currently ongoing emissions of the signal it has been
2085 * connected to. The @handler_id becomes invalid and may be reused.
2087 * The @handler_id has to be a valid signal handler id, connected to a
2088 * signal of @instance.
2091 g_signal_handler_disconnect (gpointer instance,
2097 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2098 g_return_if_fail (handler_id > 0);
2101 handler = handler_lookup (instance, handler_id, &signal_id);
2104 handler->sequential_number = 0;
2105 handler->block_count = 1;
2106 handler_unref_R (signal_id, instance, handler);
2109 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2114 * g_signal_handler_is_connected:
2115 * @instance: The instance where a signal handler is sought.
2116 * @handler_id: the handler id.
2118 * Returns whether @handler_id is the id of a handler connected to @instance.
2120 * Returns: whether @handler_id identifies a handler connected to @instance.
2123 g_signal_handler_is_connected (gpointer instance,
2129 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2132 handler = handler_lookup (instance, handler_id, NULL);
2133 connected = handler != NULL;
2140 g_signal_handlers_destroy (gpointer instance)
2142 GBSearchArray *hlbsa;
2144 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2147 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2152 /* reentrancy caution, delete instance trace first */
2153 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2155 for (i = 0; i < hlbsa->n_nodes; i++)
2157 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2158 Handler *handler = hlist->handlers;
2162 Handler *tmp = handler;
2164 handler = tmp->next;
2165 tmp->block_count = 1;
2166 /* cruel unlink, this works because _all_ handlers vanish */
2169 if (tmp->sequential_number)
2171 tmp->sequential_number = 0;
2172 handler_unref_R (0, NULL, tmp);
2176 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2182 * g_signal_handler_find:
2183 * @instance: The instance owning the signal handler to be found.
2184 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2185 * and/or @data the handler has to match.
2186 * @signal_id: Signal the handler has to be connected to.
2187 * @detail: Signal detail the handler has to be connected to.
2188 * @closure: The closure the handler will invoke.
2189 * @func: The C closure callback of the handler (useless for non-C closures).
2190 * @data: The closure data of the handler's closure.
2192 * Finds the first signal handler that matches certain selection criteria.
2193 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2194 * flags, and the criteria values are passed as arguments.
2195 * The match @mask has to be non-0 for successful matches.
2196 * If no handler was found, 0 is returned.
2198 * Returns: A valid non-0 signal handler id for a successful match.
2201 g_signal_handler_find (gpointer instance,
2202 GSignalMatchType mask,
2209 gulong handler_seq_no = 0;
2211 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2212 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2214 if (mask & G_SIGNAL_MATCH_MASK)
2216 HandlerMatch *mlist;
2219 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2222 handler_seq_no = mlist->handler->sequential_number;
2223 handler_match_free1_R (mlist, instance);
2228 return handler_seq_no;
2232 signal_handlers_foreach_matched_R (gpointer instance,
2233 GSignalMatchType mask,
2239 void (*callback) (gpointer instance,
2240 gulong handler_seq_no))
2242 HandlerMatch *mlist;
2243 guint n_handlers = 0;
2245 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2249 if (mlist->handler->sequential_number)
2252 callback (instance, mlist->handler->sequential_number);
2255 mlist = handler_match_free1_R (mlist, instance);
2262 * g_signal_handlers_block_matched:
2263 * @instance: The instance to block handlers from.
2264 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2265 * and/or @data the handlers have to match.
2266 * @signal_id: Signal the handlers have to be connected to.
2267 * @detail: Signal detail the handlers have to be connected to.
2268 * @closure: The closure the handlers will invoke.
2269 * @func: The C closure callback of the handlers (useless for non-C closures).
2270 * @data: The closure data of the handlers' closures.
2272 * Blocks all handlers on an instance that match a certain selection criteria.
2273 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2274 * flags, and the criteria values are passed as arguments.
2275 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2276 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2277 * If no handlers were found, 0 is returned, the number of blocked handlers
2280 * Returns: The number of handlers that matched.
2283 g_signal_handlers_block_matched (gpointer instance,
2284 GSignalMatchType mask,
2291 guint n_handlers = 0;
2293 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2294 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2296 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2299 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2300 closure, func, data,
2301 g_signal_handler_block);
2309 * g_signal_handlers_unblock_matched:
2310 * @instance: The instance to unblock handlers from.
2311 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2312 * and/or @data the handlers have to match.
2313 * @signal_id: Signal the handlers have to be connected to.
2314 * @detail: Signal detail the handlers have to be connected to.
2315 * @closure: The closure the handlers will invoke.
2316 * @func: The C closure callback of the handlers (useless for non-C closures).
2317 * @data: The closure data of the handlers' closures.
2319 * Unblocks all handlers on an instance that match a certain selection
2320 * criteria. The criteria mask is passed as an OR-ed combination of
2321 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2322 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2323 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2324 * If no handlers were found, 0 is returned, the number of unblocked handlers
2325 * otherwise. The match criteria should not apply to any handlers that are
2326 * not currently blocked.
2328 * Returns: The number of handlers that matched.
2331 g_signal_handlers_unblock_matched (gpointer instance,
2332 GSignalMatchType mask,
2339 guint n_handlers = 0;
2341 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2342 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2344 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2347 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2348 closure, func, data,
2349 g_signal_handler_unblock);
2357 * g_signal_handlers_disconnect_matched:
2358 * @instance: The instance to remove handlers from.
2359 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2360 * and/or @data the handlers have to match.
2361 * @signal_id: Signal the handlers have to be connected to.
2362 * @detail: Signal detail the handlers have to be connected to.
2363 * @closure: The closure the handlers will invoke.
2364 * @func: The C closure callback of the handlers (useless for non-C closures).
2365 * @data: The closure data of the handlers' closures.
2367 * Disconnects all handlers on an instance that match a certain
2368 * selection criteria. The criteria mask is passed as an OR-ed
2369 * combination of #GSignalMatchType flags, and the criteria values are
2370 * passed as arguments. Passing at least one of the
2371 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2372 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2373 * matches. If no handlers were found, 0 is returned, the number of
2374 * disconnected handlers otherwise.
2376 * Returns: The number of handlers that matched.
2379 g_signal_handlers_disconnect_matched (gpointer instance,
2380 GSignalMatchType mask,
2387 guint n_handlers = 0;
2389 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2390 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2392 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2395 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2396 closure, func, data,
2397 g_signal_handler_disconnect);
2405 * g_signal_has_handler_pending:
2406 * @instance: the object whose signal handlers are sought.
2407 * @signal_id: the signal id.
2408 * @detail: the detail.
2409 * @may_be_blocked: whether blocked handlers should count as match.
2411 * Returns whether there are any handlers connected to @instance for the
2412 * given signal id and detail.
2414 * One example of when you might use this is when the arguments to the
2415 * signal are difficult to compute. A class implementor may opt to not
2416 * emit the signal if no one is attached anyway, thus saving the cost
2417 * of building the arguments.
2419 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2423 g_signal_has_handler_pending (gpointer instance,
2426 gboolean may_be_blocked)
2428 HandlerMatch *mlist;
2429 gboolean has_pending;
2431 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2432 g_return_val_if_fail (signal_id > 0, FALSE);
2437 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2439 if (!(node->flags & G_SIGNAL_DETAILED))
2441 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2446 mlist = handlers_find (instance,
2447 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2448 signal_id, detail, NULL, NULL, NULL, TRUE);
2452 handler_match_free1_R (mlist, instance);
2455 has_pending = FALSE;
2461 static inline gboolean
2462 signal_check_skip_emission (SignalNode *node,
2468 /* are we able to check for NULL class handlers? */
2469 if (!node->test_class_offset)
2472 /* are there emission hooks pending? */
2473 if (node->emission_hooks && node->emission_hooks->hooks)
2476 /* is there a non-NULL class handler? */
2477 if (node->test_class_offset != TEST_CLASS_MAGIC)
2479 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2481 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2485 /* are signals being debugged? */
2486 #ifdef G_ENABLE_DEBUG
2487 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2489 #endif /* G_ENABLE_DEBUG */
2491 /* is this a no-recurse signal already in emission? */
2492 if (node->flags & G_SIGNAL_NO_RECURSE &&
2493 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2496 /* do we have pending handlers? */
2497 hlist = handler_list_lookup (node->signal_id, instance);
2498 if (hlist && hlist->handlers)
2501 /* none of the above, no emission required */
2507 * @instance_and_params: argument list for the signal emission. The first
2508 * element in the array is a #GValue for the instance the signal is
2509 * being emitted on. The rest are any arguments to be passed to the
2511 * @signal_id: the signal id
2512 * @detail: the detail
2513 * @return_value: Location to store the return value of the signal emission.
2517 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2518 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2521 g_signal_emitv (const GValue *instance_and_params,
2524 GValue *return_value)
2528 #ifdef G_ENABLE_DEBUG
2529 const GValue *param_values;
2533 g_return_if_fail (instance_and_params != NULL);
2534 instance = g_value_peek_pointer (instance_and_params);
2535 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2536 g_return_if_fail (signal_id > 0);
2538 #ifdef G_ENABLE_DEBUG
2539 param_values = instance_and_params + 1;
2543 node = LOOKUP_SIGNAL_NODE (signal_id);
2544 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2546 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2550 #ifdef G_ENABLE_DEBUG
2551 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2553 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2557 for (i = 0; i < node->n_params; i++)
2558 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2560 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2562 type_debug_name (node->param_types[i]),
2565 G_VALUE_TYPE_NAME (param_values + i));
2569 if (node->return_type != G_TYPE_NONE)
2573 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2575 type_debug_name (node->return_type),
2580 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2582 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2584 type_debug_name (node->return_type),
2586 G_VALUE_TYPE_NAME (return_value));
2592 return_value = NULL;
2593 #endif /* G_ENABLE_DEBUG */
2595 /* optimize NOP emissions */
2596 if (signal_check_skip_emission (node, instance, detail))
2598 /* nothing to do to emit this signal */
2600 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2605 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2609 * g_signal_emit_valist:
2610 * @instance: the instance the signal is being emitted on.
2611 * @signal_id: the signal id
2612 * @detail: the detail
2613 * @var_args: a list of parameters to be passed to the signal, followed by a
2614 * location for the return value. If the return type of the signal
2615 * is #G_TYPE_NONE, the return value location can be omitted.
2619 * Note that g_signal_emit_valist() resets the return value to the default
2620 * if no handlers are connected, in contrast to g_signal_emitv().
2623 g_signal_emit_valist (gpointer instance,
2628 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2629 GType signal_return_type;
2630 GValue *param_values;
2634 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2635 g_return_if_fail (signal_id > 0);
2638 node = LOOKUP_SIGNAL_NODE (signal_id);
2639 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2641 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2645 #ifndef G_DISABLE_CHECKS
2646 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2648 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2652 #endif /* !G_DISABLE_CHECKS */
2654 /* optimize NOP emissions */
2655 if (signal_check_skip_emission (node, instance, detail))
2657 /* nothing to do to emit this signal */
2659 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2663 n_params = node->n_params;
2664 signal_return_type = node->return_type;
2665 if (node->n_params < MAX_STACK_VALUES)
2666 instance_and_params = stack_values;
2669 free_me = g_new (GValue, node->n_params + 1);
2670 instance_and_params = free_me;
2672 param_values = instance_and_params + 1;
2673 for (i = 0; i < node->n_params; i++)
2676 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2677 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2679 param_values[i].g_type = 0;
2681 g_value_init (param_values + i, ptype);
2682 G_VALUE_COLLECT (param_values + i,
2684 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2688 g_warning ("%s: %s", G_STRLOC, error);
2691 /* we purposely leak the value here, it might not be
2692 * in a sane state if an error condition occoured
2695 g_value_unset (param_values + i);
2703 instance_and_params->g_type = 0;
2704 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2705 g_value_set_instance (instance_and_params, instance);
2706 if (signal_return_type == G_TYPE_NONE)
2707 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2710 GValue return_value = { 0, };
2711 gchar *error = NULL;
2712 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2713 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2715 g_value_init (&return_value, rtype);
2717 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2719 G_VALUE_LCOPY (&return_value,
2721 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2724 g_value_unset (&return_value);
2727 g_warning ("%s: %s", G_STRLOC, error);
2730 /* we purposely leak the value here, it might not be
2731 * in a sane state if an error condition occured
2735 for (i = 0; i < n_params; i++)
2736 g_value_unset (param_values + i);
2737 g_value_unset (instance_and_params);
2744 * @instance: the instance the signal is being emitted on.
2745 * @signal_id: the signal id
2746 * @detail: the detail
2747 * @...: parameters to be passed to the signal, followed by a
2748 * location for the return value. If the return type of the signal
2749 * is #G_TYPE_NONE, the return value location can be omitted.
2753 * Note that g_signal_emit() resets the return value to the default
2754 * if no handlers are connected, in contrast to g_signal_emitv().
2757 g_signal_emit (gpointer instance,
2764 va_start (var_args, detail);
2765 g_signal_emit_valist (instance, signal_id, detail, var_args);
2770 * g_signal_emit_by_name:
2771 * @instance: the instance the signal is being emitted on.
2772 * @detailed_signal: a string of the form "signal-name::detail".
2773 * @...: parameters to be passed to the signal, followed by a
2774 * location for the return value. If the return type of the signal
2775 * is #G_TYPE_NONE, the return value location can be omitted.
2779 * Note that g_signal_emit_by_name() resets the return value to the default
2780 * if no handlers are connected, in contrast to g_signal_emitv().
2783 g_signal_emit_by_name (gpointer instance,
2784 const gchar *detailed_signal,
2790 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2791 g_return_if_fail (detailed_signal != NULL);
2794 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2801 va_start (var_args, detailed_signal);
2802 g_signal_emit_valist (instance, signal_id, detail, var_args);
2806 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2809 static inline gboolean
2810 accumulate (GSignalInvocationHint *ihint,
2811 GValue *return_accu,
2812 GValue *handler_return,
2813 SignalAccumulator *accumulator)
2815 gboolean continue_emission;
2820 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2821 g_value_reset (handler_return);
2823 return continue_emission;
2827 signal_emit_unlocked_R (SignalNode *node,
2830 GValue *emission_return,
2831 const GValue *instance_and_params)
2833 SignalAccumulator *accumulator;
2835 GClosure *class_closure;
2837 Handler *handler_list = NULL;
2838 GValue *return_accu, accu = { 0, };
2840 gulong max_sequential_handler_number;
2841 gboolean return_value_altered = FALSE;
2843 #ifdef G_ENABLE_DEBUG
2844 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2846 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2847 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2850 if (g_trap_instance_signals == instance)
2853 #endif /* G_ENABLE_DEBUG */
2856 signal_id = node->signal_id;
2857 if (node->flags & G_SIGNAL_NO_RECURSE)
2859 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2863 node->state = EMISSION_RESTART;
2865 return return_value_altered;
2868 accumulator = node->accumulator;
2872 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2873 return_accu = &accu;
2877 return_accu = emission_return;
2878 emission.instance = instance;
2879 emission.ihint.signal_id = node->signal_id;
2880 emission.ihint.detail = detail;
2881 emission.ihint.run_type = 0;
2883 emission.chain_type = G_TYPE_NONE;
2884 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2885 class_closure = signal_lookup_closure (node, instance);
2890 handler_unref_R (signal_id, instance, handler_list);
2891 max_sequential_handler_number = g_handler_sequential_number;
2892 hlist = handler_list_lookup (signal_id, instance);
2893 handler_list = hlist ? hlist->handlers : NULL;
2895 handler_ref (handler_list);
2897 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2899 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2901 emission.state = EMISSION_RUN;
2903 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2905 g_closure_invoke (class_closure,
2908 instance_and_params,
2910 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2911 emission.state == EMISSION_RUN)
2912 emission.state = EMISSION_STOP;
2914 emission.chain_type = G_TYPE_NONE;
2915 return_value_altered = TRUE;
2917 if (emission.state == EMISSION_STOP)
2919 else if (emission.state == EMISSION_RESTART)
2923 if (node->emission_hooks)
2925 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2928 emission.state = EMISSION_HOOK;
2929 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2932 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2934 if (!signal_hook->detail || signal_hook->detail == detail)
2936 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2938 was_in_call = G_HOOK_IN_CALL (hook);
2939 hook->flags |= G_HOOK_FLAG_IN_CALL;
2941 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2944 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2946 g_hook_destroy_link (node->emission_hooks, hook);
2948 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2951 if (emission.state == EMISSION_RESTART)
2957 Handler *handler = handler_list;
2959 emission.state = EMISSION_RUN;
2960 handler_ref (handler);
2967 handler_unref_R (signal_id, instance, handler_list);
2968 handler_list = handler;
2971 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2972 handler->sequential_number < max_sequential_handler_number)
2975 g_closure_invoke (handler->closure,
2978 instance_and_params,
2980 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2981 emission.state == EMISSION_RUN)
2982 emission.state = EMISSION_STOP;
2984 return_value_altered = TRUE;
2986 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2989 tmp = handler->next;
2993 handler_unref_R (signal_id, instance, handler_list);
2994 handler_list = handler;
2999 if (emission.state == EMISSION_STOP)
3001 else if (emission.state == EMISSION_RESTART)
3005 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3007 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3009 emission.state = EMISSION_RUN;
3011 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3013 g_closure_invoke (class_closure,
3016 instance_and_params,
3018 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3019 emission.state == EMISSION_RUN)
3020 emission.state = EMISSION_STOP;
3022 emission.chain_type = G_TYPE_NONE;
3023 return_value_altered = TRUE;
3025 if (emission.state == EMISSION_STOP)
3027 else if (emission.state == EMISSION_RESTART)
3033 Handler *handler = handler_list;
3035 emission.state = EMISSION_RUN;
3036 handler_ref (handler);
3041 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3042 handler->sequential_number < max_sequential_handler_number)
3045 g_closure_invoke (handler->closure,
3048 instance_and_params,
3050 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3051 emission.state == EMISSION_RUN)
3052 emission.state = EMISSION_STOP;
3054 return_value_altered = TRUE;
3056 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3059 tmp = handler->next;
3063 handler_unref_R (signal_id, instance, handler);
3068 if (emission.state == EMISSION_STOP)
3070 else if (emission.state == EMISSION_RESTART)
3076 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3078 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3080 gboolean need_unset = FALSE;
3082 emission.state = EMISSION_STOP;
3084 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3086 if (node->return_type != G_TYPE_NONE && !accumulator)
3088 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3091 g_closure_invoke (class_closure,
3092 node->return_type != G_TYPE_NONE ? &accu : NULL,
3094 instance_and_params,
3097 g_value_unset (&accu);
3099 emission.chain_type = G_TYPE_NONE;
3101 if (emission.state == EMISSION_RESTART)
3106 handler_unref_R (signal_id, instance, handler_list);
3108 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3111 g_value_unset (&accu);
3113 return return_value_altered;
3117 type_debug_name (GType type)
3121 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3122 return name ? name : "<unknown>";
3129 * g_signal_accumulator_true_handled:
3130 * @ihint: standard #GSignalAccumulator parameter
3131 * @return_accu: standard #GSignalAccumulator parameter
3132 * @handler_return: standard #GSignalAccumulator parameter
3133 * @dummy: standard #GSignalAccumulator parameter
3135 * A predefined #GSignalAccumulator for signals that return a
3136 * boolean values. The behavior that this accumulator gives is
3137 * that a return of %TRUE stops the signal emission: no further
3138 * callbacks will be invoked, while a return of %FALSE allows
3139 * the emission to coninue. The idea here is that a %TRUE return
3140 * indicates that the callback <emphasis>handled</emphasis> the signal,
3141 * and no further handling is needed.
3145 * Returns: standard #GSignalAccumulator result
3148 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3149 GValue *return_accu,
3150 const GValue *handler_return,
3153 gboolean continue_emission;
3154 gboolean signal_handled;
3156 signal_handled = g_value_get_boolean (handler_return);
3157 g_value_set_boolean (return_accu, signal_handled);
3158 continue_emission = !signal_handled;
3160 return continue_emission;
3163 /* --- compile standard marshallers --- */
3164 #include "gmarshal.c"
3166 #define __G_SIGNAL_C__
3167 #include "gobjectaliasdef.c"