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"
45 * @Short_description: A means for customization of object behaviour
46 * and a general purpose notification mechanism
50 * The basic concept of the signal system is that of the
51 * <emphasis>emission</emphasis> of a signal. Signals are introduced
52 * per-type and are identified through strings. Signals introduced
53 * for a parent type are available in derived types as well, so
54 * basically they are a per-type facility that is inherited. A signal
55 * emission mainly involves invocation of a certain set of callbacks
56 * in precisely defined manner. There are two main categories of such
57 * callbacks, per-object i'm referring to those types as "object
58 * types" in the following, simply because that is the context most
59 * users will encounter signals in.
61 * ones and user provided ones.
62 * The per-object callbacks are most often referred to as "object method
63 * handler" or "default (signal) handler", while user provided callbacks are
64 * usually just called "signal handler".
65 * The object method handler is provided at signal creation time (this most
66 * frequently happens at the end of an object class' creation), while user
67 * provided handlers are frequently connected and disconnected to/from a certain
68 * signal on certain object instances.
70 * A signal emission consists of five stages, unless prematurely stopped:
72 * <varlistentry><term></term><listitem><para>
73 * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
74 * </para></listitem></varlistentry>
75 * <varlistentry><term></term><listitem><para>
76 * 2 - Invocation of normal user-provided signal handlers (<emphasis>after</emphasis> flag %FALSE)
77 * </para></listitem></varlistentry>
78 * <varlistentry><term></term><listitem><para>
79 * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
80 * </para></listitem></varlistentry>
81 * <varlistentry><term></term><listitem><para>
82 * 4 - Invocation of user provided signal handlers, connected with an <emphasis>after</emphasis> flag of %TRUE
83 * </para></listitem></varlistentry>
84 * <varlistentry><term></term><listitem><para>
85 * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
86 * </para></listitem></varlistentry>
88 * The user-provided signal handlers are called in the order they were
90 * All handlers may prematurely stop a signal emission, and any number of
91 * handlers may be connected, disconnected, blocked or unblocked during
93 * There are certain criteria for skipping user handlers in stages 2 and 4
94 * of a signal emission.
95 * First, user handlers may be <emphasis>blocked</emphasis>, blocked handlers are omitted
96 * during callback invocation, to return from the "blocked" state, a
97 * handler has to get unblocked exactly the same amount of times
98 * it has been blocked before.
99 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
100 * "detail" argument passed in to g_signal_emit() has to match the detail
101 * argument of the signal handler currently subject to invocation.
102 * Specification of no detail argument for signal handlers (omission of the
103 * detail part of the signal specification upon connection) serves as a
104 * wildcard and matches any detail argument passed in to emission.
108 /* pre allocation configurations
110 #define MAX_STACK_VALUES (16)
112 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
113 #ifdef G_ENABLE_DEBUG
114 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
115 static volatile gpointer g_trace_instance_signals = NULL;
116 static volatile gpointer g_trap_instance_signals = NULL;
117 #endif /* G_ENABLE_DEBUG */
120 /* --- typedefs --- */
121 typedef struct _SignalNode SignalNode;
122 typedef struct _SignalKey SignalKey;
123 typedef struct _Emission Emission;
124 typedef struct _Handler Handler;
125 typedef struct _HandlerList HandlerList;
126 typedef struct _HandlerMatch HandlerMatch;
136 /* --- prototypes --- */
137 static inline guint signal_id_lookup (GQuark quark,
139 static void signal_destroy_R (SignalNode *signal_node);
140 static inline HandlerList* handler_list_ensure (guint signal_id,
142 static inline HandlerList* handler_list_lookup (guint signal_id,
144 static inline Handler* handler_new (gboolean after);
145 static void handler_insert (guint signal_id,
148 static Handler* handler_lookup (gpointer instance,
151 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
154 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
156 static HandlerMatch* handlers_find (gpointer instance,
157 GSignalMatchType mask,
163 gboolean one_and_only);
164 static inline void handler_ref (Handler *handler);
165 static inline void handler_unref_R (guint signal_id,
168 static gint handler_lists_cmp (gconstpointer node1,
169 gconstpointer node2);
170 static inline void emission_push (Emission **emission_list_p,
172 static inline void emission_pop (Emission **emission_list_p,
174 static inline Emission* emission_find (Emission *emission_list,
178 static gint class_closures_cmp (gconstpointer node1,
179 gconstpointer node2);
180 static gint signal_key_cmp (gconstpointer node1,
181 gconstpointer node2);
182 static gboolean signal_emit_unlocked_R (SignalNode *node,
185 GValue *return_value,
186 const GValue *instance_and_params);
187 static const gchar * type_debug_name (GType type);
190 /* --- structures --- */
193 GSignalAccumulator func;
201 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
205 /* permanent portion */
211 /* reinitializable portion */
212 guint test_class_offset : 12;
215 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
216 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
217 GBSearchArray *class_closure_bsa;
218 SignalAccumulator *accumulator;
219 GSignalCMarshaller c_marshaller;
220 GHookList *emission_hooks;
222 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
223 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
236 GSignalInvocationHint ihint;
245 Handler *tail_before; /* normal signal handlers are appended here */
246 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
251 gulong sequential_number;
256 guint block_count : 16;
257 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
270 GType instance_type; /* 0 for default closure */
275 /* --- variables --- */
276 static GBSearchArray *g_signal_key_bsa = NULL;
277 static const GBSearchConfig g_signal_key_bconfig = {
280 G_BSEARCH_ARRAY_ALIGN_POWER2,
282 static GBSearchConfig g_signal_hlbsa_bconfig = {
283 sizeof (HandlerList),
287 static GBSearchConfig g_class_closure_bconfig = {
288 sizeof (ClassClosure),
292 static GHashTable *g_handler_list_bsa_ht = NULL;
293 static Emission *g_recursive_emissions = NULL;
294 static Emission *g_restart_emissions = NULL;
295 static gulong g_handler_sequential_number = 1;
296 G_LOCK_DEFINE_STATIC (g_signal_mutex);
297 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
298 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
301 /* --- signal nodes --- */
302 static guint g_n_signal_nodes = 0;
303 static SignalNode **g_signal_nodes = NULL;
305 static inline SignalNode*
306 LOOKUP_SIGNAL_NODE (register guint signal_id)
308 if (signal_id < g_n_signal_nodes)
309 return g_signal_nodes[signal_id];
315 /* --- functions --- */
317 signal_id_lookup (GQuark quark,
320 GType *ifaces, type = itype;
326 /* try looking up signals for this type and its ancestors */
329 SignalKey *signal_key;
332 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
335 return signal_key->signal_id;
337 type = g_type_parent (type);
341 /* no luck, try interfaces it exports */
342 ifaces = g_type_interfaces (itype, &n_ifaces);
345 SignalKey *signal_key;
347 key.itype = ifaces[n_ifaces];
348 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
353 return signal_key->signal_id;
362 class_closures_cmp (gconstpointer node1,
365 const ClassClosure *c1 = node1, *c2 = node2;
367 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
371 handler_lists_cmp (gconstpointer node1,
374 const HandlerList *hlist1 = node1, *hlist2 = node2;
376 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
379 static inline HandlerList*
380 handler_list_ensure (guint signal_id,
383 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
386 key.signal_id = signal_id;
388 key.tail_before = NULL;
389 key.tail_after = NULL;
392 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
393 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
394 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
398 GBSearchArray *o = hlbsa;
400 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
402 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
404 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
407 static inline HandlerList*
408 handler_list_lookup (guint signal_id,
411 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
414 key.signal_id = signal_id;
416 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
420 handler_lookup (gpointer instance,
424 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
430 for (i = 0; i < hlbsa->n_nodes; i++)
432 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
435 for (handler = hlist->handlers; handler; handler = handler->next)
436 if (handler->sequential_number == handler_id)
439 *signal_id_p = hlist->signal_id;
449 static inline HandlerMatch*
450 handler_match_prepend (HandlerMatch *list,
456 node = g_slice_new (HandlerMatch);
457 node->handler = handler;
459 node->signal_id = signal_id;
460 handler_ref (handler);
464 static inline HandlerMatch*
465 handler_match_free1_R (HandlerMatch *node,
468 HandlerMatch *next = node->next;
470 handler_unref_R (node->signal_id, instance, node->handler);
471 g_slice_free (HandlerMatch, node);
477 handlers_find (gpointer instance,
478 GSignalMatchType mask,
484 gboolean one_and_only)
486 HandlerMatch *mlist = NULL;
488 if (mask & G_SIGNAL_MATCH_ID)
490 HandlerList *hlist = handler_list_lookup (signal_id, instance);
492 SignalNode *node = NULL;
494 if (mask & G_SIGNAL_MATCH_FUNC)
496 node = LOOKUP_SIGNAL_NODE (signal_id);
497 if (!node || !node->c_marshaller)
502 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
503 if (handler->sequential_number &&
504 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
505 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
506 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
507 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
508 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
509 handler->closure->meta_marshal == 0 &&
510 ((GCClosure*) handler->closure)->callback == func)))
512 mlist = handler_match_prepend (mlist, handler, signal_id);
519 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
526 for (i = 0; i < hlbsa->n_nodes; i++)
528 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
529 SignalNode *node = NULL;
532 if (!(mask & G_SIGNAL_MATCH_FUNC))
534 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
535 if (!node->c_marshaller)
539 for (handler = hlist->handlers; handler; handler = handler->next)
540 if (handler->sequential_number &&
541 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
542 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
543 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
544 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
545 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
546 handler->closure->meta_marshal == 0 &&
547 ((GCClosure*) handler->closure)->callback == func)))
549 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
560 static inline Handler*
561 handler_new (gboolean after)
563 Handler *handler = g_slice_new (Handler);
564 #ifndef G_DISABLE_CHECKS
565 if (g_handler_sequential_number < 1)
566 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
569 handler->sequential_number = g_handler_sequential_number++;
570 handler->prev = NULL;
571 handler->next = NULL;
573 handler->ref_count = 1;
574 handler->block_count = 0;
575 handler->after = after != FALSE;
576 handler->closure = NULL;
582 handler_ref (Handler *handler)
584 g_return_if_fail (handler->ref_count > 0);
586 g_atomic_int_inc (&handler->ref_count);
590 handler_unref_R (guint signal_id,
596 g_return_if_fail (handler->ref_count > 0);
598 is_zero = g_atomic_int_dec_and_test (&handler->ref_count);
600 if (G_UNLIKELY (is_zero))
602 HandlerList *hlist = NULL;
605 handler->next->prev = handler->prev;
606 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
607 handler->prev->next = handler->next;
610 hlist = handler_list_lookup (signal_id, instance);
611 hlist->handlers = handler->next;
616 /* check if we are removing the handler pointed to by tail_before */
617 if (!handler->after && (!handler->next || handler->next->after))
620 hlist = handler_list_lookup (signal_id, instance);
623 g_assert (hlist->tail_before == handler); /* paranoid */
624 hlist->tail_before = handler->prev;
628 /* check if we are removing the handler pointed to by tail_after */
632 hlist = handler_list_lookup (signal_id, instance);
635 g_assert (hlist->tail_after == handler); /* paranoid */
636 hlist->tail_after = handler->prev;
642 g_closure_unref (handler->closure);
644 g_slice_free (Handler, handler);
649 handler_insert (guint signal_id,
655 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
657 hlist = handler_list_ensure (signal_id, instance);
658 if (!hlist->handlers)
660 hlist->handlers = handler;
662 hlist->tail_before = handler;
664 else if (handler->after)
666 handler->prev = hlist->tail_after;
667 hlist->tail_after->next = handler;
671 if (hlist->tail_before)
673 handler->next = hlist->tail_before->next;
675 handler->next->prev = handler;
676 handler->prev = hlist->tail_before;
677 hlist->tail_before->next = handler;
679 else /* insert !after handler into a list of only after handlers */
681 handler->next = hlist->handlers;
683 handler->next->prev = handler;
684 hlist->handlers = handler;
686 hlist->tail_before = handler;
690 hlist->tail_after = handler;
694 emission_push (Emission **emission_list_p,
697 emission->next = *emission_list_p;
698 *emission_list_p = emission;
702 emission_pop (Emission **emission_list_p,
705 Emission *node, *last = NULL;
707 for (node = *emission_list_p; node; last = node, node = last->next)
708 if (node == emission)
711 last->next = node->next;
713 *emission_list_p = node->next;
716 g_assert_not_reached ();
719 static inline Emission*
720 emission_find (Emission *emission_list,
727 for (emission = emission_list; emission; emission = emission->next)
728 if (emission->instance == instance &&
729 emission->ihint.signal_id == signal_id &&
730 emission->ihint.detail == detail)
735 static inline Emission*
736 emission_find_innermost (gpointer instance)
738 Emission *emission, *s = NULL, *c = NULL;
740 for (emission = g_restart_emissions; emission; emission = emission->next)
741 if (emission->instance == instance)
746 for (emission = g_recursive_emissions; emission; emission = emission->next)
747 if (emission->instance == instance)
757 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
761 signal_key_cmp (gconstpointer node1,
764 const SignalKey *key1 = node1, *key2 = node2;
766 if (key1->itype == key2->itype)
767 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
769 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
776 if (!g_n_signal_nodes)
778 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
779 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
780 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
782 /* invalid (0) signal_id */
783 g_n_signal_nodes = 1;
784 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
785 g_signal_nodes[0] = NULL;
791 _g_signals_destroy (GType itype)
796 for (i = 1; i < g_n_signal_nodes; i++)
798 SignalNode *node = g_signal_nodes[i];
800 if (node->itype == itype)
803 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
805 type_debug_name (node->itype));
807 signal_destroy_R (node);
814 * g_signal_stop_emission:
815 * @instance: the object whose signal handlers you wish to stop.
816 * @signal_id: the signal identifier, as returned by g_signal_lookup().
817 * @detail: the detail which the signal was emitted with.
819 * Stops a signal's current emission.
821 * This will prevent the default method from running, if the signal was
822 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
825 * Prints a warning if used on a signal which isn't being emitted.
828 g_signal_stop_emission (gpointer instance,
834 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
835 g_return_if_fail (signal_id > 0);
838 node = LOOKUP_SIGNAL_NODE (signal_id);
839 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
841 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
845 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
847 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
848 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
852 if (emission->state == EMISSION_HOOK)
853 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
854 node->name, instance);
855 else if (emission->state == EMISSION_RUN)
856 emission->state = EMISSION_STOP;
859 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
860 node->name, instance);
863 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
868 signal_finalize_hook (GHookList *hook_list,
871 GDestroyNotify destroy = hook->destroy;
875 hook->destroy = NULL;
877 destroy (hook->data);
883 * g_signal_add_emission_hook:
884 * @signal_id: the signal identifier, as returned by g_signal_lookup().
885 * @detail: the detail on which to call the hook.
886 * @hook_func: a #GSignalEmissionHook function.
887 * @hook_data: user data for @hook_func.
888 * @data_destroy: a #GDestroyNotify for @hook_data.
890 * Adds an emission hook for a signal, which will get called for any emission
891 * of that signal, independent of the instance. This is possible only
892 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
894 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
897 g_signal_add_emission_hook (guint signal_id,
899 GSignalEmissionHook hook_func,
901 GDestroyNotify data_destroy)
903 static gulong seq_hook_id = 1;
906 SignalHook *signal_hook;
908 g_return_val_if_fail (signal_id > 0, 0);
909 g_return_val_if_fail (hook_func != NULL, 0);
912 node = LOOKUP_SIGNAL_NODE (signal_id);
913 if (!node || node->destroyed)
915 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
919 if (node->flags & G_SIGNAL_NO_HOOKS)
921 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
925 if (detail && !(node->flags & G_SIGNAL_DETAILED))
927 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
931 if (!node->emission_hooks)
933 node->emission_hooks = g_new (GHookList, 1);
934 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
935 node->emission_hooks->finalize_hook = signal_finalize_hook;
937 hook = g_hook_alloc (node->emission_hooks);
938 hook->data = hook_data;
939 hook->func = (gpointer) hook_func;
940 hook->destroy = data_destroy;
941 signal_hook = SIGNAL_HOOK (hook);
942 signal_hook->detail = detail;
943 node->emission_hooks->seq_id = seq_hook_id;
944 g_hook_append (node->emission_hooks, hook);
945 seq_hook_id = node->emission_hooks->seq_id;
948 return hook->hook_id;
952 * g_signal_remove_emission_hook:
953 * @signal_id: the id of the signal
954 * @hook_id: the id of the emission hook, as returned by
955 * g_signal_add_emission_hook()
957 * Deletes an emission hook.
960 g_signal_remove_emission_hook (guint signal_id,
965 g_return_if_fail (signal_id > 0);
966 g_return_if_fail (hook_id > 0);
969 node = LOOKUP_SIGNAL_NODE (signal_id);
970 if (!node || node->destroyed)
971 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
972 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
973 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
978 signal_parse_name (const gchar *name,
981 gboolean force_quark)
983 const gchar *colon = strchr (name, ':');
988 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
989 if (signal_id && detail_p)
992 else if (colon[1] == ':')
995 guint l = colon - name;
999 memcpy (buffer, name, l);
1001 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1005 gchar *signal = g_new (gchar, l + 1);
1007 memcpy (signal, name, l);
1009 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1013 if (signal_id && detail_p)
1014 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1022 * g_signal_parse_name:
1023 * @detailed_signal: a string of the form "signal-name::detail".
1024 * @itype: The interface/instance type that introduced "signal-name".
1025 * @signal_id_p: Location to store the signal id.
1026 * @detail_p: Location to store the detail quark.
1027 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1029 * Internal function to parse a signal name into its @signal_id
1030 * and @detail quark.
1032 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1035 g_signal_parse_name (const gchar *detailed_signal,
1039 gboolean force_detail_quark)
1045 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1046 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1049 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1052 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1053 if (!node || node->destroyed ||
1054 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1058 *signal_id_p = signal_id;
1066 * g_signal_stop_emission_by_name:
1067 * @instance: the object whose signal handlers you wish to stop.
1068 * @detailed_signal: a string of the form "signal-name::detail".
1070 * Stops a signal's current emission.
1072 * This is just like g_signal_stop_emission() except it will look up the
1073 * signal id for you.
1076 g_signal_stop_emission_by_name (gpointer instance,
1077 const gchar *detailed_signal)
1083 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1084 g_return_if_fail (detailed_signal != NULL);
1087 itype = G_TYPE_FROM_INSTANCE (instance);
1088 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1091 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1093 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1094 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1095 else if (!g_type_is_a (itype, node->itype))
1096 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1099 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1100 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1104 if (emission->state == EMISSION_HOOK)
1105 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1106 node->name, instance);
1107 else if (emission->state == EMISSION_RUN)
1108 emission->state = EMISSION_STOP;
1111 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1112 node->name, instance);
1116 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1122 * @name: the signal's name.
1123 * @itype: the type that the signal operates on.
1125 * Given the name of the signal and the type of object it connects to, gets
1126 * the signal's identifying integer. Emitting the signal by number is
1127 * somewhat faster than using the name each time.
1129 * Also tries the ancestors of the given type.
1131 * See g_signal_new() for details on allowed signal names.
1133 * Returns: the signal's identifying number, or 0 if no signal was found.
1136 g_signal_lookup (const gchar *name,
1140 g_return_val_if_fail (name != NULL, 0);
1141 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1144 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1148 /* give elaborate warnings */
1149 if (!g_type_name (itype))
1150 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'",
1152 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1153 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1154 name, g_type_name (itype));
1155 else if (!g_type_class_peek (itype))
1156 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1157 name, g_type_name (itype));
1164 * g_signal_list_ids:
1165 * @itype: Instance or interface type.
1166 * @n_ids: Location to store the number of signal ids for @itype.
1168 * Lists the signals by id that a certain instance or interface type
1169 * created. Further information about the signals can be acquired through
1172 * Returns: Newly allocated array of signal IDs.
1175 g_signal_list_ids (GType itype,
1183 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1184 g_return_val_if_fail (n_ids != NULL, NULL);
1187 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1188 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1189 result = g_array_new (FALSE, FALSE, sizeof (guint));
1191 for (i = 0; i < n_nodes; i++)
1192 if (keys[i].itype == itype)
1194 const gchar *name = g_quark_to_string (keys[i].quark);
1196 /* Signal names with "_" in them are aliases to the same
1197 * name with "-" instead of "_".
1199 if (!strchr (name, '_'))
1200 g_array_append_val (result, keys[i].signal_id);
1202 *n_ids = result->len;
1206 /* give elaborate warnings */
1207 if (!g_type_name (itype))
1208 g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'",
1210 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1211 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1212 g_type_name (itype));
1213 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1214 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1215 g_type_name (itype));
1218 return (guint*) g_array_free (result, FALSE);
1223 * @signal_id: the signal's identifying number.
1225 * Given the signal's identifier, finds its name.
1227 * Two different signals may have the same name, if they have differing types.
1229 * Returns: the signal name, or %NULL if the signal number was invalid.
1231 G_CONST_RETURN gchar*
1232 g_signal_name (guint signal_id)
1238 node = LOOKUP_SIGNAL_NODE (signal_id);
1239 name = node ? node->name : NULL;
1242 return (char*) name;
1247 * @signal_id: The signal id of the signal to query information for.
1248 * @query: A user provided structure that is filled in with constant
1249 * values upon success.
1251 * Queries the signal system for in-depth information about a
1252 * specific signal. This function will fill in a user-provided
1253 * structure to hold signal-specific information. If an invalid
1254 * signal id is passed in, the @signal_id member of the #GSignalQuery
1255 * is 0. All members filled into the #GSignalQuery structure should
1256 * be considered constant and have to be left untouched.
1259 g_signal_query (guint signal_id,
1260 GSignalQuery *query)
1264 g_return_if_fail (query != NULL);
1267 node = LOOKUP_SIGNAL_NODE (signal_id);
1268 if (!node || node->destroyed)
1269 query->signal_id = 0;
1272 query->signal_id = node->signal_id;
1273 query->signal_name = node->name;
1274 query->itype = node->itype;
1275 query->signal_flags = node->flags;
1276 query->return_type = node->return_type;
1277 query->n_params = node->n_params;
1278 query->param_types = node->param_types;
1285 * @signal_name: the name for the signal
1286 * @itype: the type this signal pertains to. It will also pertain to
1287 * types which are derived from this type.
1288 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1289 * the default handler is to be invoked. You should at least specify
1290 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1291 * @class_offset: The offset of the function pointer in the class structure
1292 * for this type. Used to invoke a class method generically. Pass 0 to
1293 * not associate a class method with this signal.
1294 * @accumulator: the accumulator for this signal; may be %NULL.
1295 * @accu_data: user data for the @accumulator.
1296 * @c_marshaller: the function to translate arrays of parameter values to
1297 * signal emissions into C language callback invocations.
1298 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1299 * without a return value.
1300 * @n_params: the number of parameter types to follow.
1301 * @...: a list of types, one for each parameter.
1303 * Creates a new signal. (This is usually done in the class initializer.)
1305 * A signal name consists of segments consisting of ASCII letters and
1306 * digits, separated by either the '-' or '_' character. The first
1307 * character of a signal name must be a letter. Names which violate these
1308 * rules lead to undefined behaviour of the GSignal system.
1310 * When registering a signal and looking up a signal, either separator can
1311 * be used, but they cannot be mixed.
1313 * Returns: the signal id
1316 g_signal_new (const gchar *signal_name,
1318 GSignalFlags signal_flags,
1320 GSignalAccumulator accumulator,
1322 GSignalCMarshaller c_marshaller,
1330 g_return_val_if_fail (signal_name != NULL, 0);
1332 va_start (args, n_params);
1334 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1335 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1336 accumulator, accu_data, c_marshaller,
1337 return_type, n_params, args);
1341 /* optimize NOP emissions with NULL class handlers */
1342 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1343 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1348 node = LOOKUP_SIGNAL_NODE (signal_id);
1349 node->test_class_offset = class_offset;
1356 static inline ClassClosure*
1357 signal_find_class_closure (SignalNode *node,
1360 GBSearchArray *bsa = node->class_closure_bsa;
1367 /* cc->instance_type is 0 for default closure */
1369 key.instance_type = itype;
1370 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1371 while (!cc && key.instance_type)
1373 key.instance_type = g_type_parent (key.instance_type);
1374 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1382 static inline GClosure*
1383 signal_lookup_closure (SignalNode *node,
1384 GTypeInstance *instance)
1388 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1389 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1391 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1392 return cc ? cc->closure : NULL;
1396 signal_add_class_closure (SignalNode *node,
1402 /* can't optimize NOP emissions with overridden class closures */
1403 node->test_class_offset = 0;
1405 if (!node->class_closure_bsa)
1406 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1407 key.instance_type = itype;
1408 key.closure = g_closure_ref (closure);
1409 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1410 &g_class_closure_bconfig,
1412 g_closure_sink (closure);
1413 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1414 g_closure_set_marshal (closure, node->c_marshaller);
1419 * @signal_name: the name for the signal
1420 * @itype: the type this signal pertains to. It will also pertain to
1421 * types which are derived from this type.
1422 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1423 * the default handler is to be invoked. You should at least specify
1424 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1425 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1426 * @accumulator: the accumulator for this signal; may be %NULL.
1427 * @accu_data: user data for the @accumulator.
1428 * @c_marshaller: the function to translate arrays of parameter values to
1429 * signal emissions into C language callback invocations.
1430 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1431 * without a return value.
1432 * @n_params: the length of @param_types.
1433 * @param_types: an array types, one for each parameter.
1435 * Creates a new signal. (This is usually done in the class initializer.)
1437 * See g_signal_new() for details on allowed signal names.
1439 * Returns: the signal id
1442 g_signal_newv (const gchar *signal_name,
1444 GSignalFlags signal_flags,
1445 GClosure *class_closure,
1446 GSignalAccumulator accumulator,
1448 GSignalCMarshaller c_marshaller,
1457 g_return_val_if_fail (signal_name != NULL, 0);
1458 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1460 g_return_val_if_fail (param_types != NULL, 0);
1461 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1462 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1463 g_return_val_if_fail (accumulator == NULL, 0);
1465 g_return_val_if_fail (accu_data == NULL, 0);
1467 name = g_strdup (signal_name);
1468 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1472 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1473 node = LOOKUP_SIGNAL_NODE (signal_id);
1474 if (node && !node->destroyed)
1476 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1478 type_debug_name (node->itype),
1479 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1484 if (node && node->itype != itype)
1486 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1488 type_debug_name (itype),
1489 type_debug_name (node->itype));
1494 for (i = 0; i < n_params; i++)
1495 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1497 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1498 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1503 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1505 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1506 type_debug_name (return_type), type_debug_name (itype), name);
1511 if (return_type != G_TYPE_NONE &&
1512 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1514 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1515 type_debug_name (itype), name, type_debug_name (return_type));
1521 /* setup permanent portion of signal node */
1526 signal_id = g_n_signal_nodes++;
1527 node = g_new (SignalNode, 1);
1528 node->signal_id = signal_id;
1529 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1530 g_signal_nodes[signal_id] = node;
1531 node->itype = itype;
1534 key.quark = g_quark_from_string (node->name);
1535 key.signal_id = signal_id;
1536 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1537 g_strdelimit (name, "_", '-');
1538 node->name = g_intern_string (name);
1539 key.quark = g_quark_from_string (name);
1540 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1542 node->destroyed = FALSE;
1543 node->test_class_offset = 0;
1545 /* setup reinitializable portion */
1546 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1547 node->n_params = n_params;
1548 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1549 node->return_type = return_type;
1550 node->class_closure_bsa = NULL;
1553 node->accumulator = g_new (SignalAccumulator, 1);
1554 node->accumulator->func = accumulator;
1555 node->accumulator->data = accu_data;
1558 node->accumulator = NULL;
1559 node->c_marshaller = c_marshaller;
1560 node->emission_hooks = NULL;
1562 signal_add_class_closure (node, 0, class_closure);
1563 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1565 /* optimize NOP emissions */
1566 node->test_class_offset = TEST_CLASS_MAGIC;
1576 * g_signal_new_valist:
1577 * @signal_name: the name for the signal
1578 * @itype: the type this signal pertains to. It will also pertain to
1579 * types which are derived from this type.
1580 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1581 * the default handler is to be invoked. You should at least specify
1582 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1583 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1584 * @accumulator: the accumulator for this signal; may be %NULL.
1585 * @accu_data: user data for the @accumulator.
1586 * @c_marshaller: the function to translate arrays of parameter values to
1587 * signal emissions into C language callback invocations.
1588 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1589 * without a return value.
1590 * @n_params: the number of parameter types in @args.
1591 * @args: va_list of #GType, one for each parameter.
1593 * Creates a new signal. (This is usually done in the class initializer.)
1595 * See g_signal_new() for details on allowed signal names.
1597 * Returns: the signal id
1600 g_signal_new_valist (const gchar *signal_name,
1602 GSignalFlags signal_flags,
1603 GClosure *class_closure,
1604 GSignalAccumulator accumulator,
1606 GSignalCMarshaller c_marshaller,
1617 param_types = g_new (GType, n_params);
1619 for (i = 0; i < n_params; i++)
1620 param_types[i] = va_arg (args, GType);
1625 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1626 class_closure, accumulator, accu_data, c_marshaller,
1627 return_type, n_params, param_types);
1628 g_free (param_types);
1634 signal_destroy_R (SignalNode *signal_node)
1636 SignalNode node = *signal_node;
1638 signal_node->destroyed = TRUE;
1640 /* reentrancy caution, zero out real contents first */
1641 signal_node->test_class_offset = 0;
1642 signal_node->n_params = 0;
1643 signal_node->param_types = NULL;
1644 signal_node->return_type = 0;
1645 signal_node->class_closure_bsa = NULL;
1646 signal_node->accumulator = NULL;
1647 signal_node->c_marshaller = NULL;
1648 signal_node->emission_hooks = NULL;
1650 #ifdef G_ENABLE_DEBUG
1651 /* check current emissions */
1655 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1656 emission; emission = emission->next)
1657 if (emission->ihint.signal_id == node.signal_id)
1658 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1659 node.name, emission->instance);
1663 /* free contents that need to
1666 g_free (node.param_types);
1667 if (node.class_closure_bsa)
1671 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1673 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1675 g_closure_unref (cc->closure);
1677 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1679 g_free (node.accumulator);
1680 if (node.emission_hooks)
1682 g_hook_list_clear (node.emission_hooks);
1683 g_free (node.emission_hooks);
1689 * g_signal_override_class_closure:
1690 * @signal_id: the signal id
1691 * @instance_type: the instance type on which to override the class closure
1693 * @class_closure: the closure.
1695 * Overrides the class closure (i.e. the default handler) for the given signal
1696 * for emissions on instances of @instance_type. @instance_type must be derived
1697 * from the type to which the signal belongs.
1700 g_signal_override_class_closure (guint signal_id,
1701 GType instance_type,
1702 GClosure *class_closure)
1706 g_return_if_fail (signal_id > 0);
1707 g_return_if_fail (class_closure != NULL);
1710 node = LOOKUP_SIGNAL_NODE (signal_id);
1711 if (!g_type_is_a (instance_type, node->itype))
1712 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1715 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1717 if (cc && cc->instance_type == instance_type)
1718 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1720 signal_add_class_closure (node, instance_type, class_closure);
1726 * g_signal_chain_from_overridden:
1727 * @instance_and_params: the argument list of the signal emission. The first
1728 * element in the array is a #GValue for the instance the signal is being
1729 * emitted on. The rest are any arguments to be passed to the signal.
1730 * @return_value: Location for the return value.
1732 * Calls the original class closure of a signal. This function should only
1733 * be called from an overridden class closure; see
1734 * g_signal_override_class_closure().
1737 g_signal_chain_from_overridden (const GValue *instance_and_params,
1738 GValue *return_value)
1740 GType chain_type = 0, restore_type = 0;
1741 Emission *emission = NULL;
1742 GClosure *closure = NULL;
1746 g_return_if_fail (instance_and_params != NULL);
1747 instance = g_value_peek_pointer (instance_and_params);
1748 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1751 emission = emission_find_innermost (instance);
1754 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1756 g_assert (node != NULL); /* paranoid */
1758 /* we should probably do the same parameter checks as g_signal_emit() here.
1760 if (emission->chain_type != G_TYPE_NONE)
1762 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1764 g_assert (cc != NULL); /* closure currently in call stack */
1766 n_params = node->n_params;
1767 restore_type = cc->instance_type;
1768 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1769 if (cc && cc->instance_type != restore_type)
1771 closure = cc->closure;
1772 chain_type = cc->instance_type;
1776 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1779 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1782 emission->chain_type = chain_type;
1784 g_closure_invoke (closure,
1787 instance_and_params,
1790 emission->chain_type = restore_type;
1796 * g_signal_get_invocation_hint:
1797 * @instance: the instance to query
1799 * Returns the invocation hint of the innermost signal emission of instance.
1801 * Returns: the invocation hint of the innermost signal emission.
1803 GSignalInvocationHint*
1804 g_signal_get_invocation_hint (gpointer instance)
1806 Emission *emission = NULL;
1808 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1811 emission = emission_find_innermost (instance);
1814 return emission ? &emission->ihint : NULL;
1818 * g_signal_connect_closure_by_id:
1819 * @instance: the instance to connect to.
1820 * @signal_id: the id of the signal.
1821 * @detail: the detail.
1822 * @closure: the closure to connect.
1823 * @after: whether the handler should be called before or after the
1824 * default handler of the signal.
1826 * Connects a closure to a signal for a particular object.
1828 * Returns: the handler id
1831 g_signal_connect_closure_by_id (gpointer instance,
1838 gulong handler_seq_no = 0;
1840 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1841 g_return_val_if_fail (signal_id > 0, 0);
1842 g_return_val_if_fail (closure != NULL, 0);
1845 node = LOOKUP_SIGNAL_NODE (signal_id);
1848 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1849 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1850 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1851 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1854 Handler *handler = handler_new (after);
1856 handler_seq_no = handler->sequential_number;
1857 handler->detail = detail;
1858 handler->closure = g_closure_ref (closure);
1859 g_closure_sink (closure);
1860 handler_insert (signal_id, instance, handler);
1861 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1862 g_closure_set_marshal (closure, node->c_marshaller);
1866 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1869 return handler_seq_no;
1873 * g_signal_connect_closure:
1874 * @instance: the instance to connect to.
1875 * @detailed_signal: a string of the form "signal-name::detail".
1876 * @closure: the closure to connect.
1877 * @after: whether the handler should be called before or after the
1878 * default handler of the signal.
1880 * Connects a closure to a signal for a particular object.
1882 * Returns: the handler id
1885 g_signal_connect_closure (gpointer instance,
1886 const gchar *detailed_signal,
1891 gulong handler_seq_no = 0;
1895 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1896 g_return_val_if_fail (detailed_signal != NULL, 0);
1897 g_return_val_if_fail (closure != NULL, 0);
1900 itype = G_TYPE_FROM_INSTANCE (instance);
1901 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1904 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1906 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1907 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1908 else if (!g_type_is_a (itype, node->itype))
1909 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1912 Handler *handler = handler_new (after);
1914 handler_seq_no = handler->sequential_number;
1915 handler->detail = detail;
1916 handler->closure = g_closure_ref (closure);
1917 g_closure_sink (closure);
1918 handler_insert (signal_id, instance, handler);
1919 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1920 g_closure_set_marshal (handler->closure, node->c_marshaller);
1924 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1927 return handler_seq_no;
1931 * g_signal_connect_data:
1932 * @instance: the instance to connect to.
1933 * @detailed_signal: a string of the form "signal-name::detail".
1934 * @c_handler: the #GCallback to connect.
1935 * @data: data to pass to @c_handler calls.
1936 * @destroy_data: a #GClosureNotify for @data.
1937 * @connect_flags: a combination of #GConnectFlags.
1939 * Connects a #GCallback function to a signal for a particular object. Similar
1940 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
1941 * which will be called when the signal handler is disconnected and no longer
1942 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
1943 * <literal>..._swapped()</literal> variants of this function.
1945 * Returns: the handler id
1948 g_signal_connect_data (gpointer instance,
1949 const gchar *detailed_signal,
1950 GCallback c_handler,
1952 GClosureNotify destroy_data,
1953 GConnectFlags connect_flags)
1956 gulong handler_seq_no = 0;
1959 gboolean swapped, after;
1961 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1962 g_return_val_if_fail (detailed_signal != NULL, 0);
1963 g_return_val_if_fail (c_handler != NULL, 0);
1965 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1966 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1969 itype = G_TYPE_FROM_INSTANCE (instance);
1970 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1973 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1975 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1976 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1977 else if (!g_type_is_a (itype, node->itype))
1978 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1981 Handler *handler = handler_new (after);
1983 handler_seq_no = handler->sequential_number;
1984 handler->detail = detail;
1985 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1986 g_closure_sink (handler->closure);
1987 handler_insert (signal_id, instance, handler);
1988 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1989 g_closure_set_marshal (handler->closure, node->c_marshaller);
1993 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1996 return handler_seq_no;
2000 * g_signal_handler_block:
2001 * @instance: The instance to block the signal handler of.
2002 * @handler_id: Handler id of the handler to be blocked.
2004 * Blocks a handler of an instance so it will not be called during any
2005 * signal emissions unless it is unblocked again. Thus "blocking" a
2006 * signal handler means to temporarily deactive it, a signal handler
2007 * has to be unblocked exactly the same amount of times it has been
2008 * blocked before to become active again.
2010 * The @handler_id has to be a valid signal handler id, connected to a
2011 * signal of @instance.
2014 g_signal_handler_block (gpointer instance,
2019 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2020 g_return_if_fail (handler_id > 0);
2023 handler = handler_lookup (instance, handler_id, NULL);
2026 #ifndef G_DISABLE_CHECKS
2027 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2028 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2030 handler->block_count += 1;
2033 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2038 * g_signal_handler_unblock:
2039 * @instance: The instance to unblock the signal handler of.
2040 * @handler_id: Handler id of the handler to be unblocked.
2042 * Undoes the effect of a previous g_signal_handler_block() call. A
2043 * blocked handler is skipped during signal emissions and will not be
2044 * invoked, unblocking it (for exactly the amount of times it has been
2045 * blocked before) reverts its "blocked" state, so the handler will be
2046 * recognized by the signal system and is called upon future or
2047 * currently ongoing signal emissions (since the order in which
2048 * handlers are called during signal emissions is deterministic,
2049 * whether the unblocked handler in question is called as part of a
2050 * currently ongoing emission depends on how far that emission has
2053 * The @handler_id has to be a valid id of a signal handler that is
2054 * connected to a signal of @instance and is currently blocked.
2057 g_signal_handler_unblock (gpointer instance,
2062 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2063 g_return_if_fail (handler_id > 0);
2066 handler = handler_lookup (instance, handler_id, NULL);
2069 if (handler->block_count)
2070 handler->block_count -= 1;
2072 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2075 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2080 * g_signal_handler_disconnect:
2081 * @instance: The instance to remove the signal handler from.
2082 * @handler_id: Handler id of the handler to be disconnected.
2084 * Disconnects a handler from an instance so it will not be called during
2085 * any future or currently ongoing emissions of the signal it has been
2086 * connected to. The @handler_id becomes invalid and may be reused.
2088 * The @handler_id has to be a valid signal handler id, connected to a
2089 * signal of @instance.
2092 g_signal_handler_disconnect (gpointer instance,
2098 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2099 g_return_if_fail (handler_id > 0);
2102 handler = handler_lookup (instance, handler_id, &signal_id);
2105 handler->sequential_number = 0;
2106 handler->block_count = 1;
2107 handler_unref_R (signal_id, instance, handler);
2110 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2115 * g_signal_handler_is_connected:
2116 * @instance: The instance where a signal handler is sought.
2117 * @handler_id: the handler id.
2119 * Returns whether @handler_id is the id of a handler connected to @instance.
2121 * Returns: whether @handler_id identifies a handler connected to @instance.
2124 g_signal_handler_is_connected (gpointer instance,
2130 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2133 handler = handler_lookup (instance, handler_id, NULL);
2134 connected = handler != NULL;
2141 g_signal_handlers_destroy (gpointer instance)
2143 GBSearchArray *hlbsa;
2145 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2148 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2153 /* reentrancy caution, delete instance trace first */
2154 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2156 for (i = 0; i < hlbsa->n_nodes; i++)
2158 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2159 Handler *handler = hlist->handlers;
2163 Handler *tmp = handler;
2165 handler = tmp->next;
2166 tmp->block_count = 1;
2167 /* cruel unlink, this works because _all_ handlers vanish */
2170 if (tmp->sequential_number)
2172 tmp->sequential_number = 0;
2173 handler_unref_R (0, NULL, tmp);
2177 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2183 * g_signal_handler_find:
2184 * @instance: The instance owning the signal handler to be found.
2185 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2186 * and/or @data the handler has to match.
2187 * @signal_id: Signal the handler has to be connected to.
2188 * @detail: Signal detail the handler has to be connected to.
2189 * @closure: The closure the handler will invoke.
2190 * @func: The C closure callback of the handler (useless for non-C closures).
2191 * @data: The closure data of the handler's closure.
2193 * Finds the first signal handler that matches certain selection criteria.
2194 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2195 * flags, and the criteria values are passed as arguments.
2196 * The match @mask has to be non-0 for successful matches.
2197 * If no handler was found, 0 is returned.
2199 * Returns: A valid non-0 signal handler id for a successful match.
2202 g_signal_handler_find (gpointer instance,
2203 GSignalMatchType mask,
2210 gulong handler_seq_no = 0;
2212 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2213 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2215 if (mask & G_SIGNAL_MATCH_MASK)
2217 HandlerMatch *mlist;
2220 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2223 handler_seq_no = mlist->handler->sequential_number;
2224 handler_match_free1_R (mlist, instance);
2229 return handler_seq_no;
2233 signal_handlers_foreach_matched_R (gpointer instance,
2234 GSignalMatchType mask,
2240 void (*callback) (gpointer instance,
2241 gulong handler_seq_no))
2243 HandlerMatch *mlist;
2244 guint n_handlers = 0;
2246 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2250 if (mlist->handler->sequential_number)
2253 callback (instance, mlist->handler->sequential_number);
2256 mlist = handler_match_free1_R (mlist, instance);
2263 * g_signal_handlers_block_matched:
2264 * @instance: The instance to block handlers from.
2265 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2266 * and/or @data the handlers have to match.
2267 * @signal_id: Signal the handlers have to be connected to.
2268 * @detail: Signal detail the handlers have to be connected to.
2269 * @closure: The closure the handlers will invoke.
2270 * @func: The C closure callback of the handlers (useless for non-C closures).
2271 * @data: The closure data of the handlers' closures.
2273 * Blocks all handlers on an instance that match a certain selection criteria.
2274 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2275 * flags, and the criteria values are passed as arguments.
2276 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2277 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2278 * If no handlers were found, 0 is returned, the number of blocked handlers
2281 * Returns: The number of handlers that matched.
2284 g_signal_handlers_block_matched (gpointer instance,
2285 GSignalMatchType mask,
2292 guint n_handlers = 0;
2294 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2295 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2297 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2300 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2301 closure, func, data,
2302 g_signal_handler_block);
2310 * g_signal_handlers_unblock_matched:
2311 * @instance: The instance to unblock handlers from.
2312 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2313 * and/or @data the handlers have to match.
2314 * @signal_id: Signal the handlers have to be connected to.
2315 * @detail: Signal detail the handlers have to be connected to.
2316 * @closure: The closure the handlers will invoke.
2317 * @func: The C closure callback of the handlers (useless for non-C closures).
2318 * @data: The closure data of the handlers' closures.
2320 * Unblocks all handlers on an instance that match a certain selection
2321 * criteria. The criteria mask is passed as an OR-ed combination of
2322 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2323 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2324 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2325 * If no handlers were found, 0 is returned, the number of unblocked handlers
2326 * otherwise. The match criteria should not apply to any handlers that are
2327 * not currently blocked.
2329 * Returns: The number of handlers that matched.
2332 g_signal_handlers_unblock_matched (gpointer instance,
2333 GSignalMatchType mask,
2340 guint n_handlers = 0;
2342 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2343 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2345 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2348 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2349 closure, func, data,
2350 g_signal_handler_unblock);
2358 * g_signal_handlers_disconnect_matched:
2359 * @instance: The instance to remove handlers from.
2360 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2361 * and/or @data the handlers have to match.
2362 * @signal_id: Signal the handlers have to be connected to.
2363 * @detail: Signal detail the handlers have to be connected to.
2364 * @closure: The closure the handlers will invoke.
2365 * @func: The C closure callback of the handlers (useless for non-C closures).
2366 * @data: The closure data of the handlers' closures.
2368 * Disconnects all handlers on an instance that match a certain
2369 * selection criteria. The criteria mask is passed as an OR-ed
2370 * combination of #GSignalMatchType flags, and the criteria values are
2371 * passed as arguments. Passing at least one of the
2372 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2373 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2374 * matches. If no handlers were found, 0 is returned, the number of
2375 * disconnected handlers otherwise.
2377 * Returns: The number of handlers that matched.
2380 g_signal_handlers_disconnect_matched (gpointer instance,
2381 GSignalMatchType mask,
2388 guint n_handlers = 0;
2390 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2391 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2393 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2396 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2397 closure, func, data,
2398 g_signal_handler_disconnect);
2406 * g_signal_has_handler_pending:
2407 * @instance: the object whose signal handlers are sought.
2408 * @signal_id: the signal id.
2409 * @detail: the detail.
2410 * @may_be_blocked: whether blocked handlers should count as match.
2412 * Returns whether there are any handlers connected to @instance for the
2413 * given signal id and detail.
2415 * One example of when you might use this is when the arguments to the
2416 * signal are difficult to compute. A class implementor may opt to not
2417 * emit the signal if no one is attached anyway, thus saving the cost
2418 * of building the arguments.
2420 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2424 g_signal_has_handler_pending (gpointer instance,
2427 gboolean may_be_blocked)
2429 HandlerMatch *mlist;
2430 gboolean has_pending;
2432 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2433 g_return_val_if_fail (signal_id > 0, FALSE);
2438 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2440 if (!(node->flags & G_SIGNAL_DETAILED))
2442 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2447 mlist = handlers_find (instance,
2448 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2449 signal_id, detail, NULL, NULL, NULL, TRUE);
2453 handler_match_free1_R (mlist, instance);
2456 has_pending = FALSE;
2462 static inline gboolean
2463 signal_check_skip_emission (SignalNode *node,
2469 /* are we able to check for NULL class handlers? */
2470 if (!node->test_class_offset)
2473 /* are there emission hooks pending? */
2474 if (node->emission_hooks && node->emission_hooks->hooks)
2477 /* is there a non-NULL class handler? */
2478 if (node->test_class_offset != TEST_CLASS_MAGIC)
2480 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2482 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2486 /* are signals being debugged? */
2487 #ifdef G_ENABLE_DEBUG
2488 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2490 #endif /* G_ENABLE_DEBUG */
2492 /* is this a no-recurse signal already in emission? */
2493 if (node->flags & G_SIGNAL_NO_RECURSE &&
2494 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2497 /* do we have pending handlers? */
2498 hlist = handler_list_lookup (node->signal_id, instance);
2499 if (hlist && hlist->handlers)
2502 /* none of the above, no emission required */
2508 * @instance_and_params: argument list for the signal emission. The first
2509 * element in the array is a #GValue for the instance the signal is
2510 * being emitted on. The rest are any arguments to be passed to the
2512 * @signal_id: the signal id
2513 * @detail: the detail
2514 * @return_value: Location to store the return value of the signal emission.
2518 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2519 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2522 g_signal_emitv (const GValue *instance_and_params,
2525 GValue *return_value)
2529 #ifdef G_ENABLE_DEBUG
2530 const GValue *param_values;
2534 g_return_if_fail (instance_and_params != NULL);
2535 instance = g_value_peek_pointer (instance_and_params);
2536 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2537 g_return_if_fail (signal_id > 0);
2539 #ifdef G_ENABLE_DEBUG
2540 param_values = instance_and_params + 1;
2544 node = LOOKUP_SIGNAL_NODE (signal_id);
2545 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2547 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2551 #ifdef G_ENABLE_DEBUG
2552 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2554 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2558 for (i = 0; i < node->n_params; i++)
2559 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2561 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2563 type_debug_name (node->param_types[i]),
2566 G_VALUE_TYPE_NAME (param_values + i));
2570 if (node->return_type != G_TYPE_NONE)
2574 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2576 type_debug_name (node->return_type),
2581 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2583 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2585 type_debug_name (node->return_type),
2587 G_VALUE_TYPE_NAME (return_value));
2593 return_value = NULL;
2594 #endif /* G_ENABLE_DEBUG */
2596 /* optimize NOP emissions */
2597 if (signal_check_skip_emission (node, instance, detail))
2599 /* nothing to do to emit this signal */
2601 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2606 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2610 * g_signal_emit_valist:
2611 * @instance: the instance the signal is being emitted on.
2612 * @signal_id: the signal id
2613 * @detail: the detail
2614 * @var_args: a list of parameters to be passed to the signal, followed by a
2615 * location for the return value. If the return type of the signal
2616 * is #G_TYPE_NONE, the return value location can be omitted.
2620 * Note that g_signal_emit_valist() resets the return value to the default
2621 * if no handlers are connected, in contrast to g_signal_emitv().
2624 g_signal_emit_valist (gpointer instance,
2629 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2630 GType signal_return_type;
2631 GValue *param_values;
2635 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2636 g_return_if_fail (signal_id > 0);
2639 node = LOOKUP_SIGNAL_NODE (signal_id);
2640 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2642 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2646 #ifndef G_DISABLE_CHECKS
2647 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2649 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2653 #endif /* !G_DISABLE_CHECKS */
2655 /* optimize NOP emissions */
2656 if (signal_check_skip_emission (node, instance, detail))
2658 /* nothing to do to emit this signal */
2660 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2664 n_params = node->n_params;
2665 signal_return_type = node->return_type;
2666 if (node->n_params < MAX_STACK_VALUES)
2667 instance_and_params = stack_values;
2670 free_me = g_new (GValue, node->n_params + 1);
2671 instance_and_params = free_me;
2673 param_values = instance_and_params + 1;
2674 for (i = 0; i < node->n_params; i++)
2677 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2678 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2680 param_values[i].g_type = 0;
2682 g_value_init (param_values + i, ptype);
2683 G_VALUE_COLLECT (param_values + i,
2685 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2689 g_warning ("%s: %s", G_STRLOC, error);
2692 /* we purposely leak the value here, it might not be
2693 * in a sane state if an error condition occoured
2696 g_value_unset (param_values + i);
2704 instance_and_params->g_type = 0;
2705 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2706 g_value_set_instance (instance_and_params, instance);
2707 if (signal_return_type == G_TYPE_NONE)
2708 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2711 GValue return_value = { 0, };
2712 gchar *error = NULL;
2713 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2714 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2716 g_value_init (&return_value, rtype);
2718 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2720 G_VALUE_LCOPY (&return_value,
2722 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2725 g_value_unset (&return_value);
2728 g_warning ("%s: %s", G_STRLOC, error);
2731 /* we purposely leak the value here, it might not be
2732 * in a sane state if an error condition occured
2736 for (i = 0; i < n_params; i++)
2737 g_value_unset (param_values + i);
2738 g_value_unset (instance_and_params);
2745 * @instance: the instance the signal is being emitted on.
2746 * @signal_id: the signal id
2747 * @detail: the detail
2748 * @...: parameters to be passed to the signal, followed by a
2749 * location for the return value. If the return type of the signal
2750 * is #G_TYPE_NONE, the return value location can be omitted.
2754 * Note that g_signal_emit() resets the return value to the default
2755 * if no handlers are connected, in contrast to g_signal_emitv().
2758 g_signal_emit (gpointer instance,
2765 va_start (var_args, detail);
2766 g_signal_emit_valist (instance, signal_id, detail, var_args);
2771 * g_signal_emit_by_name:
2772 * @instance: the instance the signal is being emitted on.
2773 * @detailed_signal: a string of the form "signal-name::detail".
2774 * @...: parameters to be passed to the signal, followed by a
2775 * location for the return value. If the return type of the signal
2776 * is #G_TYPE_NONE, the return value location can be omitted.
2780 * Note that g_signal_emit_by_name() resets the return value to the default
2781 * if no handlers are connected, in contrast to g_signal_emitv().
2784 g_signal_emit_by_name (gpointer instance,
2785 const gchar *detailed_signal,
2791 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2792 g_return_if_fail (detailed_signal != NULL);
2795 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2802 va_start (var_args, detailed_signal);
2803 g_signal_emit_valist (instance, signal_id, detail, var_args);
2807 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2810 static inline gboolean
2811 accumulate (GSignalInvocationHint *ihint,
2812 GValue *return_accu,
2813 GValue *handler_return,
2814 SignalAccumulator *accumulator)
2816 gboolean continue_emission;
2821 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2822 g_value_reset (handler_return);
2824 return continue_emission;
2828 signal_emit_unlocked_R (SignalNode *node,
2831 GValue *emission_return,
2832 const GValue *instance_and_params)
2834 SignalAccumulator *accumulator;
2836 GClosure *class_closure;
2838 Handler *handler_list = NULL;
2839 GValue *return_accu, accu = { 0, };
2841 gulong max_sequential_handler_number;
2842 gboolean return_value_altered = FALSE;
2844 #ifdef G_ENABLE_DEBUG
2845 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2847 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2848 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2851 if (g_trap_instance_signals == instance)
2854 #endif /* G_ENABLE_DEBUG */
2857 signal_id = node->signal_id;
2858 if (node->flags & G_SIGNAL_NO_RECURSE)
2860 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2864 node->state = EMISSION_RESTART;
2866 return return_value_altered;
2869 accumulator = node->accumulator;
2873 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2874 return_accu = &accu;
2878 return_accu = emission_return;
2879 emission.instance = instance;
2880 emission.ihint.signal_id = node->signal_id;
2881 emission.ihint.detail = detail;
2882 emission.ihint.run_type = 0;
2884 emission.chain_type = G_TYPE_NONE;
2885 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2886 class_closure = signal_lookup_closure (node, instance);
2891 handler_unref_R (signal_id, instance, handler_list);
2892 max_sequential_handler_number = g_handler_sequential_number;
2893 hlist = handler_list_lookup (signal_id, instance);
2894 handler_list = hlist ? hlist->handlers : NULL;
2896 handler_ref (handler_list);
2898 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2900 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2902 emission.state = EMISSION_RUN;
2904 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2906 g_closure_invoke (class_closure,
2909 instance_and_params,
2911 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2912 emission.state == EMISSION_RUN)
2913 emission.state = EMISSION_STOP;
2915 emission.chain_type = G_TYPE_NONE;
2916 return_value_altered = TRUE;
2918 if (emission.state == EMISSION_STOP)
2920 else if (emission.state == EMISSION_RESTART)
2924 if (node->emission_hooks)
2926 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2929 emission.state = EMISSION_HOOK;
2930 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2933 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2935 if (!signal_hook->detail || signal_hook->detail == detail)
2937 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2939 was_in_call = G_HOOK_IN_CALL (hook);
2940 hook->flags |= G_HOOK_FLAG_IN_CALL;
2942 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2945 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2947 g_hook_destroy_link (node->emission_hooks, hook);
2949 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2952 if (emission.state == EMISSION_RESTART)
2958 Handler *handler = handler_list;
2960 emission.state = EMISSION_RUN;
2961 handler_ref (handler);
2968 handler_unref_R (signal_id, instance, handler_list);
2969 handler_list = handler;
2972 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2973 handler->sequential_number < max_sequential_handler_number)
2976 g_closure_invoke (handler->closure,
2979 instance_and_params,
2981 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2982 emission.state == EMISSION_RUN)
2983 emission.state = EMISSION_STOP;
2985 return_value_altered = TRUE;
2987 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2990 tmp = handler->next;
2994 handler_unref_R (signal_id, instance, handler_list);
2995 handler_list = handler;
3000 if (emission.state == EMISSION_STOP)
3002 else if (emission.state == EMISSION_RESTART)
3006 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3008 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3010 emission.state = EMISSION_RUN;
3012 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3014 g_closure_invoke (class_closure,
3017 instance_and_params,
3019 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3020 emission.state == EMISSION_RUN)
3021 emission.state = EMISSION_STOP;
3023 emission.chain_type = G_TYPE_NONE;
3024 return_value_altered = TRUE;
3026 if (emission.state == EMISSION_STOP)
3028 else if (emission.state == EMISSION_RESTART)
3034 Handler *handler = handler_list;
3036 emission.state = EMISSION_RUN;
3037 handler_ref (handler);
3042 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3043 handler->sequential_number < max_sequential_handler_number)
3046 g_closure_invoke (handler->closure,
3049 instance_and_params,
3051 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3052 emission.state == EMISSION_RUN)
3053 emission.state = EMISSION_STOP;
3055 return_value_altered = TRUE;
3057 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3060 tmp = handler->next;
3064 handler_unref_R (signal_id, instance, handler);
3069 if (emission.state == EMISSION_STOP)
3071 else if (emission.state == EMISSION_RESTART)
3077 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3079 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3081 gboolean need_unset = FALSE;
3083 emission.state = EMISSION_STOP;
3085 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3087 if (node->return_type != G_TYPE_NONE && !accumulator)
3089 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3092 g_closure_invoke (class_closure,
3093 node->return_type != G_TYPE_NONE ? &accu : NULL,
3095 instance_and_params,
3098 g_value_unset (&accu);
3100 emission.chain_type = G_TYPE_NONE;
3102 if (emission.state == EMISSION_RESTART)
3107 handler_unref_R (signal_id, instance, handler_list);
3109 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3112 g_value_unset (&accu);
3114 return return_value_altered;
3118 type_debug_name (GType type)
3122 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3123 return name ? name : "<unknown>";
3130 * g_signal_accumulator_true_handled:
3131 * @ihint: standard #GSignalAccumulator parameter
3132 * @return_accu: standard #GSignalAccumulator parameter
3133 * @handler_return: standard #GSignalAccumulator parameter
3134 * @dummy: standard #GSignalAccumulator parameter
3136 * A predefined #GSignalAccumulator for signals that return a
3137 * boolean values. The behavior that this accumulator gives is
3138 * that a return of %TRUE stops the signal emission: no further
3139 * callbacks will be invoked, while a return of %FALSE allows
3140 * the emission to coninue. The idea here is that a %TRUE return
3141 * indicates that the callback <emphasis>handled</emphasis> the signal,
3142 * and no further handling is needed.
3146 * Returns: standard #GSignalAccumulator result
3149 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3150 GValue *return_accu,
3151 const GValue *handler_return,
3154 gboolean continue_emission;
3155 gboolean signal_handled;
3157 signal_handled = g_value_get_boolean (handler_return);
3158 g_value_set_boolean (return_accu, signal_handled);
3159 continue_emission = !signal_handled;
3161 return continue_emission;
3164 /* --- compile standard marshallers --- */
3165 #include "gmarshal.c"
3167 #define __G_SIGNAL_C__
3168 #include "gobjectaliasdef.c"