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 and a general purpose notification mechanism
47 * The basic concept of the signal system is that of the
48 * <emphasis>emission</emphasis> of a signal. Signals are introduced
49 * per-type and are identified through strings. Signals introduced
50 * for a parent type are available in derived types as well, so
51 * basically they are a per-type facility that is inherited. A signal
52 * emission mainly involves invocation of a certain set of callbacks
53 * in precisely defined manner. There are two main categories of such
54 * callbacks, per-object i'm referring to those types as "object
55 * types" in the following, simply because that is the context most
56 * users will encounter signals in.
58 * ones and user provided ones.
59 * The per-object callbacks are most often referred to as "object method
60 * handler" or "default (signal) handler", while user provided callbacks are
61 * usually just called "signal handler".
62 * The object method handler is provided at signal creation time (this most
63 * frequently happens at the end of an object class' creation), while user
64 * provided handlers are frequently connected and disconnected to/from a certain
65 * signal on certain object instances.
67 * A signal emission consists of five stages, unless prematurely stopped:
69 * <varlistentry><term></term><listitem><para>
70 * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
71 * </para></listitem></varlistentry>
72 * <varlistentry><term></term><listitem><para>
73 * 2 - Invocation of normal user-provided signal handlers (<emphasis>after</emphasis> flag %FALSE)
74 * </para></listitem></varlistentry>
75 * <varlistentry><term></term><listitem><para>
76 * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
77 * </para></listitem></varlistentry>
78 * <varlistentry><term></term><listitem><para>
79 * 4 - Invocation of user provided signal handlers, connected with an <emphasis>after</emphasis> flag of %TRUE
80 * </para></listitem></varlistentry>
81 * <varlistentry><term></term><listitem><para>
82 * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
83 * </para></listitem></varlistentry>
85 * The user-provided signal handlers are called in the order they were
87 * All handlers may prematurely stop a signal emission, and any number of
88 * handlers may be connected, disconnected, blocked or unblocked during
90 * There are certain criteria for skipping user handlers in stages 2 and 4
91 * of a signal emission.
92 * First, user handlers may be <emphasis>blocked</emphasis>, blocked handlers are omitted
93 * during callback invocation, to return from the "blocked" state, a
94 * handler has to get unblocked exactly the same amount of times
95 * it has been blocked before.
96 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
97 * "detail" argument passed in to g_signal_emit() has to match the detail
98 * argument of the signal handler currently subject to invocation.
99 * Specification of no detail argument for signal handlers (omission of the
100 * detail part of the signal specification upon connection) serves as a
101 * wildcard and matches any detail argument passed in to emission.
105 /* pre allocation configurations
107 #define MAX_STACK_VALUES (16)
109 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
110 #ifdef G_ENABLE_DEBUG
111 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
112 static volatile gpointer g_trace_instance_signals = NULL;
113 static volatile gpointer g_trap_instance_signals = NULL;
114 #endif /* G_ENABLE_DEBUG */
117 /* --- typedefs --- */
118 typedef struct _SignalNode SignalNode;
119 typedef struct _SignalKey SignalKey;
120 typedef struct _Emission Emission;
121 typedef struct _Handler Handler;
122 typedef struct _HandlerList HandlerList;
123 typedef struct _HandlerMatch HandlerMatch;
133 /* --- prototypes --- */
134 static inline guint signal_id_lookup (GQuark quark,
136 static void signal_destroy_R (SignalNode *signal_node);
137 static inline HandlerList* handler_list_ensure (guint signal_id,
139 static inline HandlerList* handler_list_lookup (guint signal_id,
141 static inline Handler* handler_new (gboolean after);
142 static void handler_insert (guint signal_id,
145 static Handler* handler_lookup (gpointer instance,
148 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
151 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
153 static HandlerMatch* handlers_find (gpointer instance,
154 GSignalMatchType mask,
160 gboolean one_and_only);
161 static inline void handler_ref (Handler *handler);
162 static inline void handler_unref_R (guint signal_id,
165 static gint handler_lists_cmp (gconstpointer node1,
166 gconstpointer node2);
167 static inline void emission_push (Emission **emission_list_p,
169 static inline void emission_pop (Emission **emission_list_p,
171 static inline Emission* emission_find (Emission *emission_list,
175 static gint class_closures_cmp (gconstpointer node1,
176 gconstpointer node2);
177 static gint signal_key_cmp (gconstpointer node1,
178 gconstpointer node2);
179 static gboolean signal_emit_unlocked_R (SignalNode *node,
182 GValue *return_value,
183 const GValue *instance_and_params);
184 static const gchar * type_debug_name (GType type);
187 /* --- structures --- */
190 GSignalAccumulator func;
198 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
202 /* permanent portion */
208 /* reinitializable portion */
209 guint test_class_offset : 12;
212 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
213 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
214 GBSearchArray *class_closure_bsa;
215 SignalAccumulator *accumulator;
216 GSignalCMarshaller c_marshaller;
217 GHookList *emission_hooks;
219 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
220 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
233 GSignalInvocationHint ihint;
242 Handler *tail_before; /* normal signal handlers are appended here */
243 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
248 gulong sequential_number;
253 guint block_count : 16;
254 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
267 GType instance_type; /* 0 for default closure */
272 /* --- variables --- */
273 static GBSearchArray *g_signal_key_bsa = NULL;
274 static const GBSearchConfig g_signal_key_bconfig = {
277 G_BSEARCH_ARRAY_ALIGN_POWER2,
279 static GBSearchConfig g_signal_hlbsa_bconfig = {
280 sizeof (HandlerList),
284 static GBSearchConfig g_class_closure_bconfig = {
285 sizeof (ClassClosure),
289 static GHashTable *g_handler_list_bsa_ht = NULL;
290 static Emission *g_recursive_emissions = NULL;
291 static Emission *g_restart_emissions = NULL;
292 static gulong g_handler_sequential_number = 1;
293 G_LOCK_DEFINE_STATIC (g_signal_mutex);
294 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
295 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
298 /* --- signal nodes --- */
299 static guint g_n_signal_nodes = 0;
300 static SignalNode **g_signal_nodes = NULL;
302 static inline SignalNode*
303 LOOKUP_SIGNAL_NODE (register guint signal_id)
305 if (signal_id < g_n_signal_nodes)
306 return g_signal_nodes[signal_id];
312 /* --- functions --- */
314 signal_id_lookup (GQuark quark,
317 GType *ifaces, type = itype;
323 /* try looking up signals for this type and its ancestors */
326 SignalKey *signal_key;
329 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
332 return signal_key->signal_id;
334 type = g_type_parent (type);
338 /* no luck, try interfaces it exports */
339 ifaces = g_type_interfaces (itype, &n_ifaces);
342 SignalKey *signal_key;
344 key.itype = ifaces[n_ifaces];
345 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
350 return signal_key->signal_id;
359 class_closures_cmp (gconstpointer node1,
362 const ClassClosure *c1 = node1, *c2 = node2;
364 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
368 handler_lists_cmp (gconstpointer node1,
371 const HandlerList *hlist1 = node1, *hlist2 = node2;
373 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
376 static inline HandlerList*
377 handler_list_ensure (guint signal_id,
380 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
383 key.signal_id = signal_id;
385 key.tail_before = NULL;
386 key.tail_after = NULL;
389 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
390 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
391 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
395 GBSearchArray *o = hlbsa;
397 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
399 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
401 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
404 static inline HandlerList*
405 handler_list_lookup (guint signal_id,
408 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
411 key.signal_id = signal_id;
413 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
417 handler_lookup (gpointer instance,
421 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
427 for (i = 0; i < hlbsa->n_nodes; i++)
429 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
432 for (handler = hlist->handlers; handler; handler = handler->next)
433 if (handler->sequential_number == handler_id)
436 *signal_id_p = hlist->signal_id;
446 static inline HandlerMatch*
447 handler_match_prepend (HandlerMatch *list,
453 node = g_slice_new (HandlerMatch);
454 node->handler = handler;
456 node->signal_id = signal_id;
457 handler_ref (handler);
461 static inline HandlerMatch*
462 handler_match_free1_R (HandlerMatch *node,
465 HandlerMatch *next = node->next;
467 handler_unref_R (node->signal_id, instance, node->handler);
468 g_slice_free (HandlerMatch, node);
474 handlers_find (gpointer instance,
475 GSignalMatchType mask,
481 gboolean one_and_only)
483 HandlerMatch *mlist = NULL;
485 if (mask & G_SIGNAL_MATCH_ID)
487 HandlerList *hlist = handler_list_lookup (signal_id, instance);
489 SignalNode *node = NULL;
491 if (mask & G_SIGNAL_MATCH_FUNC)
493 node = LOOKUP_SIGNAL_NODE (signal_id);
494 if (!node || !node->c_marshaller)
499 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
500 if (handler->sequential_number &&
501 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
502 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
503 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
504 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
505 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
506 handler->closure->meta_marshal == 0 &&
507 ((GCClosure*) handler->closure)->callback == func)))
509 mlist = handler_match_prepend (mlist, handler, signal_id);
516 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
523 for (i = 0; i < hlbsa->n_nodes; i++)
525 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
526 SignalNode *node = NULL;
529 if (!(mask & G_SIGNAL_MATCH_FUNC))
531 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
532 if (!node->c_marshaller)
536 for (handler = hlist->handlers; handler; handler = handler->next)
537 if (handler->sequential_number &&
538 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
539 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
540 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
541 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
542 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
543 handler->closure->meta_marshal == 0 &&
544 ((GCClosure*) handler->closure)->callback == func)))
546 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
557 static inline Handler*
558 handler_new (gboolean after)
560 Handler *handler = g_slice_new (Handler);
561 #ifndef G_DISABLE_CHECKS
562 if (g_handler_sequential_number < 1)
563 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
566 handler->sequential_number = g_handler_sequential_number++;
567 handler->prev = NULL;
568 handler->next = NULL;
570 handler->ref_count = 1;
571 handler->block_count = 0;
572 handler->after = after != FALSE;
573 handler->closure = NULL;
579 handler_ref (Handler *handler)
581 g_return_if_fail (handler->ref_count > 0);
583 g_atomic_int_inc (&handler->ref_count);
587 handler_unref_R (guint signal_id,
593 g_return_if_fail (handler->ref_count > 0);
595 is_zero = g_atomic_int_dec_and_test (&handler->ref_count);
597 if (G_UNLIKELY (is_zero))
599 HandlerList *hlist = NULL;
602 handler->next->prev = handler->prev;
603 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
604 handler->prev->next = handler->next;
607 hlist = handler_list_lookup (signal_id, instance);
608 hlist->handlers = handler->next;
613 /* check if we are removing the handler pointed to by tail_before */
614 if (!handler->after && (!handler->next || handler->next->after))
617 hlist = handler_list_lookup (signal_id, instance);
620 g_assert (hlist->tail_before == handler); /* paranoid */
621 hlist->tail_before = handler->prev;
625 /* check if we are removing the handler pointed to by tail_after */
629 hlist = handler_list_lookup (signal_id, instance);
632 g_assert (hlist->tail_after == handler); /* paranoid */
633 hlist->tail_after = handler->prev;
639 g_closure_unref (handler->closure);
641 g_slice_free (Handler, handler);
646 handler_insert (guint signal_id,
652 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
654 hlist = handler_list_ensure (signal_id, instance);
655 if (!hlist->handlers)
657 hlist->handlers = handler;
659 hlist->tail_before = handler;
661 else if (handler->after)
663 handler->prev = hlist->tail_after;
664 hlist->tail_after->next = handler;
668 if (hlist->tail_before)
670 handler->next = hlist->tail_before->next;
672 handler->next->prev = handler;
673 handler->prev = hlist->tail_before;
674 hlist->tail_before->next = handler;
676 else /* insert !after handler into a list of only after handlers */
678 handler->next = hlist->handlers;
680 handler->next->prev = handler;
681 hlist->handlers = handler;
683 hlist->tail_before = handler;
687 hlist->tail_after = handler;
691 emission_push (Emission **emission_list_p,
694 emission->next = *emission_list_p;
695 *emission_list_p = emission;
699 emission_pop (Emission **emission_list_p,
702 Emission *node, *last = NULL;
704 for (node = *emission_list_p; node; last = node, node = last->next)
705 if (node == emission)
708 last->next = node->next;
710 *emission_list_p = node->next;
713 g_assert_not_reached ();
716 static inline Emission*
717 emission_find (Emission *emission_list,
724 for (emission = emission_list; emission; emission = emission->next)
725 if (emission->instance == instance &&
726 emission->ihint.signal_id == signal_id &&
727 emission->ihint.detail == detail)
732 static inline Emission*
733 emission_find_innermost (gpointer instance)
735 Emission *emission, *s = NULL, *c = NULL;
737 for (emission = g_restart_emissions; emission; emission = emission->next)
738 if (emission->instance == instance)
743 for (emission = g_recursive_emissions; emission; emission = emission->next)
744 if (emission->instance == instance)
754 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
758 signal_key_cmp (gconstpointer node1,
761 const SignalKey *key1 = node1, *key2 = node2;
763 if (key1->itype == key2->itype)
764 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
766 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
773 if (!g_n_signal_nodes)
775 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
776 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
777 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
779 /* invalid (0) signal_id */
780 g_n_signal_nodes = 1;
781 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
782 g_signal_nodes[0] = NULL;
788 _g_signals_destroy (GType itype)
793 for (i = 1; i < g_n_signal_nodes; i++)
795 SignalNode *node = g_signal_nodes[i];
797 if (node->itype == itype)
800 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
802 type_debug_name (node->itype));
804 signal_destroy_R (node);
811 * g_signal_stop_emission:
812 * @instance: the object whose signal handlers you wish to stop.
813 * @signal_id: the signal identifier, as returned by g_signal_lookup().
814 * @detail: the detail which the signal was emitted with.
816 * Stops a signal's current emission.
818 * This will prevent the default method from running, if the signal was
819 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
822 * Prints a warning if used on a signal which isn't being emitted.
825 g_signal_stop_emission (gpointer instance,
831 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
832 g_return_if_fail (signal_id > 0);
835 node = LOOKUP_SIGNAL_NODE (signal_id);
836 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
838 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
842 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
844 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
845 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
849 if (emission->state == EMISSION_HOOK)
850 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
851 node->name, instance);
852 else if (emission->state == EMISSION_RUN)
853 emission->state = EMISSION_STOP;
856 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
857 node->name, instance);
860 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
865 signal_finalize_hook (GHookList *hook_list,
868 GDestroyNotify destroy = hook->destroy;
872 hook->destroy = NULL;
874 destroy (hook->data);
880 * g_signal_add_emission_hook:
881 * @signal_id: the signal identifier, as returned by g_signal_lookup().
882 * @detail: the detail on which to call the hook.
883 * @hook_func: a #GSignalEmissionHook function.
884 * @hook_data: user data for @hook_func.
885 * @data_destroy: a #GDestroyNotify for @hook_data.
887 * Adds an emission hook for a signal, which will get called for any emission
888 * of that signal, independent of the instance. This is possible only
889 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
891 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
894 g_signal_add_emission_hook (guint signal_id,
896 GSignalEmissionHook hook_func,
898 GDestroyNotify data_destroy)
900 static gulong seq_hook_id = 1;
903 SignalHook *signal_hook;
905 g_return_val_if_fail (signal_id > 0, 0);
906 g_return_val_if_fail (hook_func != NULL, 0);
909 node = LOOKUP_SIGNAL_NODE (signal_id);
910 if (!node || node->destroyed)
912 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
916 if (node->flags & G_SIGNAL_NO_HOOKS)
918 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
922 if (detail && !(node->flags & G_SIGNAL_DETAILED))
924 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
928 if (!node->emission_hooks)
930 node->emission_hooks = g_new (GHookList, 1);
931 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
932 node->emission_hooks->finalize_hook = signal_finalize_hook;
934 hook = g_hook_alloc (node->emission_hooks);
935 hook->data = hook_data;
936 hook->func = (gpointer) hook_func;
937 hook->destroy = data_destroy;
938 signal_hook = SIGNAL_HOOK (hook);
939 signal_hook->detail = detail;
940 node->emission_hooks->seq_id = seq_hook_id;
941 g_hook_append (node->emission_hooks, hook);
942 seq_hook_id = node->emission_hooks->seq_id;
945 return hook->hook_id;
949 * g_signal_remove_emission_hook:
950 * @signal_id: the id of the signal
951 * @hook_id: the id of the emission hook, as returned by
952 * g_signal_add_emission_hook()
954 * Deletes an emission hook.
957 g_signal_remove_emission_hook (guint signal_id,
962 g_return_if_fail (signal_id > 0);
963 g_return_if_fail (hook_id > 0);
966 node = LOOKUP_SIGNAL_NODE (signal_id);
967 if (!node || node->destroyed)
968 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
969 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
970 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
975 signal_parse_name (const gchar *name,
978 gboolean force_quark)
980 const gchar *colon = strchr (name, ':');
985 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
986 if (signal_id && detail_p)
989 else if (colon[1] == ':')
992 guint l = colon - name;
996 memcpy (buffer, name, l);
998 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1002 gchar *signal = g_new (gchar, l + 1);
1004 memcpy (signal, name, l);
1006 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1010 if (signal_id && detail_p)
1011 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1019 * g_signal_parse_name:
1020 * @detailed_signal: a string of the form "signal-name::detail".
1021 * @itype: The interface/instance type that introduced "signal-name".
1022 * @signal_id_p: Location to store the signal id.
1023 * @detail_p: Location to store the detail quark.
1024 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1026 * Internal function to parse a signal name into its @signal_id
1027 * and @detail quark.
1029 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1032 g_signal_parse_name (const gchar *detailed_signal,
1036 gboolean force_detail_quark)
1042 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1043 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1046 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1049 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1050 if (!node || node->destroyed ||
1051 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1055 *signal_id_p = signal_id;
1063 * g_signal_stop_emission_by_name:
1064 * @instance: the object whose signal handlers you wish to stop.
1065 * @detailed_signal: a string of the form "signal-name::detail".
1067 * Stops a signal's current emission.
1069 * This is just like g_signal_stop_emission() except it will look up the
1070 * signal id for you.
1073 g_signal_stop_emission_by_name (gpointer instance,
1074 const gchar *detailed_signal)
1080 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1081 g_return_if_fail (detailed_signal != NULL);
1084 itype = G_TYPE_FROM_INSTANCE (instance);
1085 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1088 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1090 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1091 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1092 else if (!g_type_is_a (itype, node->itype))
1093 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1096 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1097 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1101 if (emission->state == EMISSION_HOOK)
1102 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1103 node->name, instance);
1104 else if (emission->state == EMISSION_RUN)
1105 emission->state = EMISSION_STOP;
1108 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1109 node->name, instance);
1113 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1119 * @name: the signal's name.
1120 * @itype: the type that the signal operates on.
1122 * Given the name of the signal and the type of object it connects to, gets
1123 * the signal's identifying integer. Emitting the signal by number is
1124 * somewhat faster than using the name each time.
1126 * Also tries the ancestors of the given type.
1128 * See g_signal_new() for details on allowed signal names.
1130 * Returns: the signal's identifying number, or 0 if no signal was found.
1133 g_signal_lookup (const gchar *name,
1137 g_return_val_if_fail (name != NULL, 0);
1138 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1141 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1145 /* give elaborate warnings */
1146 if (!g_type_name (itype))
1147 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'",
1149 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1150 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1151 name, g_type_name (itype));
1152 else if (!g_type_class_peek (itype))
1153 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1154 name, g_type_name (itype));
1161 * g_signal_list_ids:
1162 * @itype: Instance or interface type.
1163 * @n_ids: Location to store the number of signal ids for @itype.
1165 * Lists the signals by id that a certain instance or interface type
1166 * created. Further information about the signals can be acquired through
1169 * Returns: Newly allocated array of signal IDs.
1172 g_signal_list_ids (GType itype,
1180 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1181 g_return_val_if_fail (n_ids != NULL, NULL);
1184 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1185 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1186 result = g_array_new (FALSE, FALSE, sizeof (guint));
1188 for (i = 0; i < n_nodes; i++)
1189 if (keys[i].itype == itype)
1191 const gchar *name = g_quark_to_string (keys[i].quark);
1193 /* Signal names with "_" in them are aliases to the same
1194 * name with "-" instead of "_".
1196 if (!strchr (name, '_'))
1197 g_array_append_val (result, keys[i].signal_id);
1199 *n_ids = result->len;
1203 /* give elaborate warnings */
1204 if (!g_type_name (itype))
1205 g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'",
1207 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1208 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1209 g_type_name (itype));
1210 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1211 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1212 g_type_name (itype));
1215 return (guint*) g_array_free (result, FALSE);
1220 * @signal_id: the signal's identifying number.
1222 * Given the signal's identifier, finds its name.
1224 * Two different signals may have the same name, if they have differing types.
1226 * Returns: the signal name, or %NULL if the signal number was invalid.
1228 G_CONST_RETURN gchar*
1229 g_signal_name (guint signal_id)
1235 node = LOOKUP_SIGNAL_NODE (signal_id);
1236 name = node ? node->name : NULL;
1239 return (char*) name;
1244 * @signal_id: The signal id of the signal to query information for.
1245 * @query: A user provided structure that is filled in with constant
1246 * values upon success.
1248 * Queries the signal system for in-depth information about a
1249 * specific signal. This function will fill in a user-provided
1250 * structure to hold signal-specific information. If an invalid
1251 * signal id is passed in, the @signal_id member of the #GSignalQuery
1252 * is 0. All members filled into the #GSignalQuery structure should
1253 * be considered constant and have to be left untouched.
1256 g_signal_query (guint signal_id,
1257 GSignalQuery *query)
1261 g_return_if_fail (query != NULL);
1264 node = LOOKUP_SIGNAL_NODE (signal_id);
1265 if (!node || node->destroyed)
1266 query->signal_id = 0;
1269 query->signal_id = node->signal_id;
1270 query->signal_name = node->name;
1271 query->itype = node->itype;
1272 query->signal_flags = node->flags;
1273 query->return_type = node->return_type;
1274 query->n_params = node->n_params;
1275 query->param_types = node->param_types;
1282 * @signal_name: the name for the signal
1283 * @itype: the type this signal pertains to. It will also pertain to
1284 * types which are derived from this type.
1285 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1286 * the default handler is to be invoked. You should at least specify
1287 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1288 * @class_offset: The offset of the function pointer in the class structure
1289 * for this type. Used to invoke a class method generically. Pass 0 to
1290 * not associate a class method with this signal.
1291 * @accumulator: the accumulator for this signal; may be %NULL.
1292 * @accu_data: user data for the @accumulator.
1293 * @c_marshaller: the function to translate arrays of parameter values to
1294 * signal emissions into C language callback invocations.
1295 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1296 * without a return value.
1297 * @n_params: the number of parameter types to follow.
1298 * @...: a list of types, one for each parameter.
1300 * Creates a new signal. (This is usually done in the class initializer.)
1302 * A signal name consists of segments consisting of ASCII letters and
1303 * digits, separated by either the '-' or '_' character. The first
1304 * character of a signal name must be a letter. Names which violate these
1305 * rules lead to undefined behaviour of the GSignal system.
1307 * When registering a signal and looking up a signal, either separator can
1308 * be used, but they cannot be mixed.
1310 * Returns: the signal id
1313 g_signal_new (const gchar *signal_name,
1315 GSignalFlags signal_flags,
1317 GSignalAccumulator accumulator,
1319 GSignalCMarshaller c_marshaller,
1327 g_return_val_if_fail (signal_name != NULL, 0);
1329 va_start (args, n_params);
1331 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1332 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1333 accumulator, accu_data, c_marshaller,
1334 return_type, n_params, args);
1338 /* optimize NOP emissions with NULL class handlers */
1339 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1340 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1345 node = LOOKUP_SIGNAL_NODE (signal_id);
1346 node->test_class_offset = class_offset;
1353 static inline ClassClosure*
1354 signal_find_class_closure (SignalNode *node,
1357 GBSearchArray *bsa = node->class_closure_bsa;
1364 /* cc->instance_type is 0 for default closure */
1366 key.instance_type = itype;
1367 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1368 while (!cc && key.instance_type)
1370 key.instance_type = g_type_parent (key.instance_type);
1371 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1379 static inline GClosure*
1380 signal_lookup_closure (SignalNode *node,
1381 GTypeInstance *instance)
1385 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1386 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1388 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1389 return cc ? cc->closure : NULL;
1393 signal_add_class_closure (SignalNode *node,
1399 /* can't optimize NOP emissions with overridden class closures */
1400 node->test_class_offset = 0;
1402 if (!node->class_closure_bsa)
1403 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1404 key.instance_type = itype;
1405 key.closure = g_closure_ref (closure);
1406 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1407 &g_class_closure_bconfig,
1409 g_closure_sink (closure);
1410 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1411 g_closure_set_marshal (closure, node->c_marshaller);
1416 * @signal_name: the name for the signal
1417 * @itype: the type this signal pertains to. It will also pertain to
1418 * types which are derived from this type.
1419 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1420 * the default handler is to be invoked. You should at least specify
1421 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1422 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1423 * @accumulator: the accumulator for this signal; may be %NULL.
1424 * @accu_data: user data for the @accumulator.
1425 * @c_marshaller: the function to translate arrays of parameter values to
1426 * signal emissions into C language callback invocations.
1427 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1428 * without a return value.
1429 * @n_params: the length of @param_types.
1430 * @param_types: an array types, one for each parameter.
1432 * Creates a new signal. (This is usually done in the class initializer.)
1434 * See g_signal_new() for details on allowed signal names.
1436 * Returns: the signal id
1439 g_signal_newv (const gchar *signal_name,
1441 GSignalFlags signal_flags,
1442 GClosure *class_closure,
1443 GSignalAccumulator accumulator,
1445 GSignalCMarshaller c_marshaller,
1454 g_return_val_if_fail (signal_name != NULL, 0);
1455 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1457 g_return_val_if_fail (param_types != NULL, 0);
1458 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1459 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1460 g_return_val_if_fail (accumulator == NULL, 0);
1462 g_return_val_if_fail (accu_data == NULL, 0);
1464 name = g_strdup (signal_name);
1465 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1469 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1470 node = LOOKUP_SIGNAL_NODE (signal_id);
1471 if (node && !node->destroyed)
1473 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1475 type_debug_name (node->itype),
1476 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1481 if (node && node->itype != itype)
1483 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1485 type_debug_name (itype),
1486 type_debug_name (node->itype));
1491 for (i = 0; i < n_params; i++)
1492 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1494 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1495 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1500 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1502 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1503 type_debug_name (return_type), type_debug_name (itype), name);
1508 if (return_type != G_TYPE_NONE &&
1509 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1511 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1512 type_debug_name (itype), name, type_debug_name (return_type));
1518 /* setup permanent portion of signal node */
1523 signal_id = g_n_signal_nodes++;
1524 node = g_new (SignalNode, 1);
1525 node->signal_id = signal_id;
1526 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1527 g_signal_nodes[signal_id] = node;
1528 node->itype = itype;
1531 key.quark = g_quark_from_string (node->name);
1532 key.signal_id = signal_id;
1533 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1534 g_strdelimit (name, "_", '-');
1535 node->name = g_intern_string (name);
1536 key.quark = g_quark_from_string (name);
1537 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1539 node->destroyed = FALSE;
1540 node->test_class_offset = 0;
1542 /* setup reinitializable portion */
1543 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1544 node->n_params = n_params;
1545 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1546 node->return_type = return_type;
1547 node->class_closure_bsa = NULL;
1550 node->accumulator = g_new (SignalAccumulator, 1);
1551 node->accumulator->func = accumulator;
1552 node->accumulator->data = accu_data;
1555 node->accumulator = NULL;
1556 node->c_marshaller = c_marshaller;
1557 node->emission_hooks = NULL;
1559 signal_add_class_closure (node, 0, class_closure);
1560 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1562 /* optimize NOP emissions */
1563 node->test_class_offset = TEST_CLASS_MAGIC;
1573 * g_signal_new_valist:
1574 * @signal_name: the name for the signal
1575 * @itype: the type this signal pertains to. It will also pertain to
1576 * types which are derived from this type.
1577 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1578 * the default handler is to be invoked. You should at least specify
1579 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1580 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1581 * @accumulator: the accumulator for this signal; may be %NULL.
1582 * @accu_data: user data for the @accumulator.
1583 * @c_marshaller: the function to translate arrays of parameter values to
1584 * signal emissions into C language callback invocations.
1585 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1586 * without a return value.
1587 * @n_params: the number of parameter types in @args.
1588 * @args: va_list of #GType, one for each parameter.
1590 * Creates a new signal. (This is usually done in the class initializer.)
1592 * See g_signal_new() for details on allowed signal names.
1594 * Returns: the signal id
1597 g_signal_new_valist (const gchar *signal_name,
1599 GSignalFlags signal_flags,
1600 GClosure *class_closure,
1601 GSignalAccumulator accumulator,
1603 GSignalCMarshaller c_marshaller,
1614 param_types = g_new (GType, n_params);
1616 for (i = 0; i < n_params; i++)
1617 param_types[i] = va_arg (args, GType);
1622 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1623 class_closure, accumulator, accu_data, c_marshaller,
1624 return_type, n_params, param_types);
1625 g_free (param_types);
1631 signal_destroy_R (SignalNode *signal_node)
1633 SignalNode node = *signal_node;
1635 signal_node->destroyed = TRUE;
1637 /* reentrancy caution, zero out real contents first */
1638 signal_node->test_class_offset = 0;
1639 signal_node->n_params = 0;
1640 signal_node->param_types = NULL;
1641 signal_node->return_type = 0;
1642 signal_node->class_closure_bsa = NULL;
1643 signal_node->accumulator = NULL;
1644 signal_node->c_marshaller = NULL;
1645 signal_node->emission_hooks = NULL;
1647 #ifdef G_ENABLE_DEBUG
1648 /* check current emissions */
1652 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1653 emission; emission = emission->next)
1654 if (emission->ihint.signal_id == node.signal_id)
1655 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1656 node.name, emission->instance);
1660 /* free contents that need to
1663 g_free (node.param_types);
1664 if (node.class_closure_bsa)
1668 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1670 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1672 g_closure_unref (cc->closure);
1674 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1676 g_free (node.accumulator);
1677 if (node.emission_hooks)
1679 g_hook_list_clear (node.emission_hooks);
1680 g_free (node.emission_hooks);
1686 * g_signal_override_class_closure:
1687 * @signal_id: the signal id
1688 * @instance_type: the instance type on which to override the class closure
1690 * @class_closure: the closure.
1692 * Overrides the class closure (i.e. the default handler) for the given signal
1693 * for emissions on instances of @instance_type. @instance_type must be derived
1694 * from the type to which the signal belongs.
1697 g_signal_override_class_closure (guint signal_id,
1698 GType instance_type,
1699 GClosure *class_closure)
1703 g_return_if_fail (signal_id > 0);
1704 g_return_if_fail (class_closure != NULL);
1707 node = LOOKUP_SIGNAL_NODE (signal_id);
1708 if (!g_type_is_a (instance_type, node->itype))
1709 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1712 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1714 if (cc && cc->instance_type == instance_type)
1715 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1717 signal_add_class_closure (node, instance_type, class_closure);
1723 * g_signal_chain_from_overridden:
1724 * @instance_and_params: the argument list of the signal emission. The first
1725 * element in the array is a #GValue for the instance the signal is being
1726 * emitted on. The rest are any arguments to be passed to the signal.
1727 * @return_value: Location for the return value.
1729 * Calls the original class closure of a signal. This function should only
1730 * be called from an overridden class closure; see
1731 * g_signal_override_class_closure().
1734 g_signal_chain_from_overridden (const GValue *instance_and_params,
1735 GValue *return_value)
1737 GType chain_type = 0, restore_type = 0;
1738 Emission *emission = NULL;
1739 GClosure *closure = NULL;
1743 g_return_if_fail (instance_and_params != NULL);
1744 instance = g_value_peek_pointer (instance_and_params);
1745 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1748 emission = emission_find_innermost (instance);
1751 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1753 g_assert (node != NULL); /* paranoid */
1755 /* we should probably do the same parameter checks as g_signal_emit() here.
1757 if (emission->chain_type != G_TYPE_NONE)
1759 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1761 g_assert (cc != NULL); /* closure currently in call stack */
1763 n_params = node->n_params;
1764 restore_type = cc->instance_type;
1765 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1766 if (cc && cc->instance_type != restore_type)
1768 closure = cc->closure;
1769 chain_type = cc->instance_type;
1773 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1776 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1779 emission->chain_type = chain_type;
1781 g_closure_invoke (closure,
1784 instance_and_params,
1787 emission->chain_type = restore_type;
1793 * g_signal_get_invocation_hint:
1794 * @instance: the instance to query
1796 * Returns the invocation hint of the innermost signal emission of instance.
1798 * Returns: the invocation hint of the innermost signal emission.
1800 GSignalInvocationHint*
1801 g_signal_get_invocation_hint (gpointer instance)
1803 Emission *emission = NULL;
1805 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1808 emission = emission_find_innermost (instance);
1811 return emission ? &emission->ihint : NULL;
1815 * g_signal_connect_closure_by_id:
1816 * @instance: the instance to connect to.
1817 * @signal_id: the id of the signal.
1818 * @detail: the detail.
1819 * @closure: the closure to connect.
1820 * @after: whether the handler should be called before or after the
1821 * default handler of the signal.
1823 * Connects a closure to a signal for a particular object.
1825 * Returns: the handler id
1828 g_signal_connect_closure_by_id (gpointer instance,
1835 gulong handler_seq_no = 0;
1837 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1838 g_return_val_if_fail (signal_id > 0, 0);
1839 g_return_val_if_fail (closure != NULL, 0);
1842 node = LOOKUP_SIGNAL_NODE (signal_id);
1845 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1846 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1847 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1848 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1851 Handler *handler = handler_new (after);
1853 handler_seq_no = handler->sequential_number;
1854 handler->detail = detail;
1855 handler->closure = g_closure_ref (closure);
1856 g_closure_sink (closure);
1857 handler_insert (signal_id, instance, handler);
1858 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1859 g_closure_set_marshal (closure, node->c_marshaller);
1863 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1866 return handler_seq_no;
1870 * g_signal_connect_closure:
1871 * @instance: the instance to connect to.
1872 * @detailed_signal: a string of the form "signal-name::detail".
1873 * @closure: the closure to connect.
1874 * @after: whether the handler should be called before or after the
1875 * default handler of the signal.
1877 * Connects a closure to a signal for a particular object.
1879 * Returns: the handler id
1882 g_signal_connect_closure (gpointer instance,
1883 const gchar *detailed_signal,
1888 gulong handler_seq_no = 0;
1892 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1893 g_return_val_if_fail (detailed_signal != NULL, 0);
1894 g_return_val_if_fail (closure != NULL, 0);
1897 itype = G_TYPE_FROM_INSTANCE (instance);
1898 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1901 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1903 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1904 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1905 else if (!g_type_is_a (itype, node->itype))
1906 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1909 Handler *handler = handler_new (after);
1911 handler_seq_no = handler->sequential_number;
1912 handler->detail = detail;
1913 handler->closure = g_closure_ref (closure);
1914 g_closure_sink (closure);
1915 handler_insert (signal_id, instance, handler);
1916 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1917 g_closure_set_marshal (handler->closure, node->c_marshaller);
1921 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1924 return handler_seq_no;
1928 * g_signal_connect_data:
1929 * @instance: the instance to connect to.
1930 * @detailed_signal: a string of the form "signal-name::detail".
1931 * @c_handler: the #GCallback to connect.
1932 * @data: data to pass to @c_handler calls.
1933 * @destroy_data: a #GClosureNotify for @data.
1934 * @connect_flags: a combination of #GConnectFlags.
1936 * Connects a #GCallback function to a signal for a particular object. Similar
1937 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
1938 * which will be called when the signal handler is disconnected and no longer
1939 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
1940 * <literal>..._swapped()</literal> variants of this function.
1942 * Returns: the handler id
1945 g_signal_connect_data (gpointer instance,
1946 const gchar *detailed_signal,
1947 GCallback c_handler,
1949 GClosureNotify destroy_data,
1950 GConnectFlags connect_flags)
1953 gulong handler_seq_no = 0;
1956 gboolean swapped, after;
1958 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1959 g_return_val_if_fail (detailed_signal != NULL, 0);
1960 g_return_val_if_fail (c_handler != NULL, 0);
1962 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1963 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1966 itype = G_TYPE_FROM_INSTANCE (instance);
1967 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1970 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1972 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1973 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1974 else if (!g_type_is_a (itype, node->itype))
1975 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1978 Handler *handler = handler_new (after);
1980 handler_seq_no = handler->sequential_number;
1981 handler->detail = detail;
1982 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1983 g_closure_sink (handler->closure);
1984 handler_insert (signal_id, instance, handler);
1985 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1986 g_closure_set_marshal (handler->closure, node->c_marshaller);
1990 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1993 return handler_seq_no;
1997 * g_signal_handler_block:
1998 * @instance: The instance to block the signal handler of.
1999 * @handler_id: Handler id of the handler to be blocked.
2001 * Blocks a handler of an instance so it will not be called during
2002 * any signal emissions unless it is unblocked again. Thus "blocking"
2003 * a signal handler means to temporarily deactive it, a signal handler
2004 * has to be unblocked exactly the same amount of times it has been
2005 * blocked before to become active again.
2007 * The @handler_id has to be a valid signal handler id, connected to a
2008 * signal of @instance.
2011 g_signal_handler_block (gpointer instance,
2016 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2017 g_return_if_fail (handler_id > 0);
2020 handler = handler_lookup (instance, handler_id, NULL);
2023 #ifndef G_DISABLE_CHECKS
2024 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2025 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2027 handler->block_count += 1;
2030 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2035 * g_signal_handler_unblock:
2036 * @instance: The instance to unblock the signal handler of.
2037 * @handler_id: Handler id of the handler to be unblocked.
2039 * Undoes the effect of a previous g_signal_handler_block() call.
2040 * A blocked handler is skipped during signal emissions and will not be
2041 * invoked, unblocking it (for exactly the amount of times it has been
2042 * blocked before) reverts its "blocked" state, so the handler will be
2043 * recognized by the signal system and is called upon future or currently
2044 * ongoing signal emissions (since the order in which handlers are
2045 * called during signal emissions is deterministic, whether the
2046 * unblocked handler in question is called as part of a currently
2047 * ongoing emission depends on how far that emission has proceeded
2050 * The @handler_id has to be a valid id of a signal handler that is
2051 * connected to a signal of @instance and is currently blocked.
2054 g_signal_handler_unblock (gpointer instance,
2059 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2060 g_return_if_fail (handler_id > 0);
2063 handler = handler_lookup (instance, handler_id, NULL);
2066 if (handler->block_count)
2067 handler->block_count -= 1;
2069 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2072 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2077 * g_signal_handler_disconnect:
2078 * @instance: The instance to remove the signal handler from.
2079 * @handler_id: Handler id of the handler to be disconnected.
2081 * Disconnects a handler from an instance so it will not be called during
2082 * any future or currently ongoing emissions of the signal it has been
2083 * connected to. The @handler_id becomes invalid and may be reused.
2085 * The @handler_id has to be a valid signal handler id, connected to a
2086 * signal of @instance.
2089 g_signal_handler_disconnect (gpointer instance,
2095 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2096 g_return_if_fail (handler_id > 0);
2099 handler = handler_lookup (instance, handler_id, &signal_id);
2102 handler->sequential_number = 0;
2103 handler->block_count = 1;
2104 handler_unref_R (signal_id, instance, handler);
2107 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2112 * g_signal_handler_is_connected:
2113 * @instance: The instance where a signal handler is sought.
2114 * @handler_id: the handler id.
2116 * Returns whether @handler_id is the id of a handler connected to @instance.
2118 * Returns: whether @handler_id identifies a handler connected to @instance.
2121 g_signal_handler_is_connected (gpointer instance,
2127 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2130 handler = handler_lookup (instance, handler_id, NULL);
2131 connected = handler != NULL;
2138 g_signal_handlers_destroy (gpointer instance)
2140 GBSearchArray *hlbsa;
2142 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2145 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2150 /* reentrancy caution, delete instance trace first */
2151 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2153 for (i = 0; i < hlbsa->n_nodes; i++)
2155 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2156 Handler *handler = hlist->handlers;
2160 Handler *tmp = handler;
2162 handler = tmp->next;
2163 tmp->block_count = 1;
2164 /* cruel unlink, this works because _all_ handlers vanish */
2167 if (tmp->sequential_number)
2169 tmp->sequential_number = 0;
2170 handler_unref_R (0, NULL, tmp);
2174 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2180 * g_signal_handler_find:
2181 * @instance: The instance owning the signal handler to be found.
2182 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2183 * and/or @data the handler has to match.
2184 * @signal_id: Signal the handler has to be connected to.
2185 * @detail: Signal detail the handler has to be connected to.
2186 * @closure: The closure the handler will invoke.
2187 * @func: The C closure callback of the handler (useless for non-C closures).
2188 * @data: The closure data of the handler's closure.
2190 * Finds the first signal handler that matches certain selection criteria.
2191 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2192 * flags, and the criteria values are passed as arguments.
2193 * The match @mask has to be non-0 for successful matches.
2194 * If no handler was found, 0 is returned.
2196 * Returns: A valid non-0 signal handler id for a successful match.
2199 g_signal_handler_find (gpointer instance,
2200 GSignalMatchType mask,
2207 gulong handler_seq_no = 0;
2209 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2210 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2212 if (mask & G_SIGNAL_MATCH_MASK)
2214 HandlerMatch *mlist;
2217 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2220 handler_seq_no = mlist->handler->sequential_number;
2221 handler_match_free1_R (mlist, instance);
2226 return handler_seq_no;
2230 signal_handlers_foreach_matched_R (gpointer instance,
2231 GSignalMatchType mask,
2237 void (*callback) (gpointer instance,
2238 gulong handler_seq_no))
2240 HandlerMatch *mlist;
2241 guint n_handlers = 0;
2243 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2247 if (mlist->handler->sequential_number)
2250 callback (instance, mlist->handler->sequential_number);
2253 mlist = handler_match_free1_R (mlist, instance);
2260 * g_signal_handlers_block_matched:
2261 * @instance: The instance to block handlers from.
2262 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2263 * and/or @data the handlers have to match.
2264 * @signal_id: Signal the handlers have to be connected to.
2265 * @detail: Signal detail the handlers have to be connected to.
2266 * @closure: The closure the handlers will invoke.
2267 * @func: The C closure callback of the handlers (useless for non-C closures).
2268 * @data: The closure data of the handlers' closures.
2270 * Blocks all handlers on an instance that match a certain selection criteria.
2271 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2272 * flags, and the criteria values are passed as arguments.
2273 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2274 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2275 * If no handlers were found, 0 is returned, the number of blocked handlers
2278 * Returns: The number of handlers that matched.
2281 g_signal_handlers_block_matched (gpointer instance,
2282 GSignalMatchType mask,
2289 guint n_handlers = 0;
2291 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2292 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2294 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2297 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2298 closure, func, data,
2299 g_signal_handler_block);
2307 * g_signal_handlers_unblock_matched:
2308 * @instance: The instance to unblock handlers from.
2309 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2310 * and/or @data the handlers have to match.
2311 * @signal_id: Signal the handlers have to be connected to.
2312 * @detail: Signal detail the handlers have to be connected to.
2313 * @closure: The closure the handlers will invoke.
2314 * @func: The C closure callback of the handlers (useless for non-C closures).
2315 * @data: The closure data of the handlers' closures.
2317 * Unblocks all handlers on an instance that match a certain selection
2318 * criteria. The criteria mask is passed as an OR-ed combination of
2319 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2320 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2321 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2322 * If no handlers were found, 0 is returned, the number of unblocked handlers
2323 * otherwise. The match criteria should not apply to any handlers that are
2324 * not currently blocked.
2326 * Returns: The number of handlers that matched.
2329 g_signal_handlers_unblock_matched (gpointer instance,
2330 GSignalMatchType mask,
2337 guint n_handlers = 0;
2339 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2340 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2342 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2345 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2346 closure, func, data,
2347 g_signal_handler_unblock);
2355 * g_signal_handlers_disconnect_matched:
2356 * @instance: The instance to remove handlers from.
2357 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2358 * and/or @data the handlers have to match.
2359 * @signal_id: Signal the handlers have to be connected to.
2360 * @detail: Signal detail the handlers have to be connected to.
2361 * @closure: The closure the handlers will invoke.
2362 * @func: The C closure callback of the handlers (useless for non-C closures).
2363 * @data: The closure data of the handlers' closures.
2365 * Disconnects all handlers on an instance that match a certain selection
2366 * criteria. The criteria mask is passed as an OR-ed combination of
2367 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2368 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2369 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2370 * If no handlers were found, 0 is returned, the number of disconnected
2371 * handlers otherwise.
2373 * Returns: The number of handlers that matched.
2376 g_signal_handlers_disconnect_matched (gpointer instance,
2377 GSignalMatchType mask,
2384 guint n_handlers = 0;
2386 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2387 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2389 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2392 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2393 closure, func, data,
2394 g_signal_handler_disconnect);
2402 * g_signal_has_handler_pending:
2403 * @instance: the object whose signal handlers are sought.
2404 * @signal_id: the signal id.
2405 * @detail: the detail.
2406 * @may_be_blocked: whether blocked handlers should count as match.
2408 * Returns whether there are any handlers connected to @instance for the
2409 * given signal id and detail.
2411 * One example of when you might use this is when the arguments to the
2412 * signal are difficult to compute. A class implementor may opt to not emit
2413 * the signal if no one is attached anyway, thus saving the cost of building
2416 * Returns: %TRUE if a handler is connected to the signal,
2420 g_signal_has_handler_pending (gpointer instance,
2423 gboolean may_be_blocked)
2425 HandlerMatch *mlist;
2426 gboolean has_pending;
2428 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2429 g_return_val_if_fail (signal_id > 0, FALSE);
2434 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2436 if (!(node->flags & G_SIGNAL_DETAILED))
2438 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2443 mlist = handlers_find (instance,
2444 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2445 signal_id, detail, NULL, NULL, NULL, TRUE);
2449 handler_match_free1_R (mlist, instance);
2452 has_pending = FALSE;
2458 static inline gboolean
2459 signal_check_skip_emission (SignalNode *node,
2465 /* are we able to check for NULL class handlers? */
2466 if (!node->test_class_offset)
2469 /* are there emission hooks pending? */
2470 if (node->emission_hooks && node->emission_hooks->hooks)
2473 /* is there a non-NULL class handler? */
2474 if (node->test_class_offset != TEST_CLASS_MAGIC)
2476 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2478 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2482 /* are signals being debugged? */
2483 #ifdef G_ENABLE_DEBUG
2484 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2486 #endif /* G_ENABLE_DEBUG */
2488 /* is this a no-recurse signal already in emission? */
2489 if (node->flags & G_SIGNAL_NO_RECURSE &&
2490 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2493 /* do we have pending handlers? */
2494 hlist = handler_list_lookup (node->signal_id, instance);
2495 if (hlist && hlist->handlers)
2498 /* none of the above, no emission required */
2504 * @instance_and_params: argument list for the signal emission. The first
2505 * element in the array is a #GValue for the instance the signal is
2506 * being emitted on. The rest are any arguments to be passed to the
2508 * @signal_id: the signal id
2509 * @detail: the detail
2510 * @return_value: Location to store the return value of the signal emission.
2514 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2515 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2518 g_signal_emitv (const GValue *instance_and_params,
2521 GValue *return_value)
2525 #ifdef G_ENABLE_DEBUG
2526 const GValue *param_values;
2530 g_return_if_fail (instance_and_params != NULL);
2531 instance = g_value_peek_pointer (instance_and_params);
2532 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2533 g_return_if_fail (signal_id > 0);
2535 #ifdef G_ENABLE_DEBUG
2536 param_values = instance_and_params + 1;
2540 node = LOOKUP_SIGNAL_NODE (signal_id);
2541 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2543 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2547 #ifdef G_ENABLE_DEBUG
2548 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2550 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2554 for (i = 0; i < node->n_params; i++)
2555 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2557 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2559 type_debug_name (node->param_types[i]),
2562 G_VALUE_TYPE_NAME (param_values + i));
2566 if (node->return_type != G_TYPE_NONE)
2570 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2572 type_debug_name (node->return_type),
2577 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2579 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2581 type_debug_name (node->return_type),
2583 G_VALUE_TYPE_NAME (return_value));
2589 return_value = NULL;
2590 #endif /* G_ENABLE_DEBUG */
2592 /* optimize NOP emissions */
2593 if (signal_check_skip_emission (node, instance, detail))
2595 /* nothing to do to emit this signal */
2597 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2602 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2606 * g_signal_emit_valist:
2607 * @instance: the instance the signal is being emitted on.
2608 * @signal_id: the signal id
2609 * @detail: the detail
2610 * @var_args: a list of parameters to be passed to the signal, followed by a
2611 * location for the return value. If the return type of the signal
2612 * is #G_TYPE_NONE, the return value location can be omitted.
2616 * Note that g_signal_emit_valist() resets the return value to the default
2617 * if no handlers are connected, in contrast to g_signal_emitv().
2620 g_signal_emit_valist (gpointer instance,
2625 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2626 GType signal_return_type;
2627 GValue *param_values;
2631 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2632 g_return_if_fail (signal_id > 0);
2635 node = LOOKUP_SIGNAL_NODE (signal_id);
2636 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2638 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2642 #ifndef G_DISABLE_CHECKS
2643 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2645 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2649 #endif /* !G_DISABLE_CHECKS */
2651 /* optimize NOP emissions */
2652 if (signal_check_skip_emission (node, instance, detail))
2654 /* nothing to do to emit this signal */
2656 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2660 n_params = node->n_params;
2661 signal_return_type = node->return_type;
2662 if (node->n_params < MAX_STACK_VALUES)
2663 instance_and_params = stack_values;
2666 free_me = g_new (GValue, node->n_params + 1);
2667 instance_and_params = free_me;
2669 param_values = instance_and_params + 1;
2670 for (i = 0; i < node->n_params; i++)
2673 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2674 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2676 param_values[i].g_type = 0;
2678 g_value_init (param_values + i, ptype);
2679 G_VALUE_COLLECT (param_values + i,
2681 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2685 g_warning ("%s: %s", G_STRLOC, error);
2688 /* we purposely leak the value here, it might not be
2689 * in a sane state if an error condition occoured
2692 g_value_unset (param_values + i);
2700 instance_and_params->g_type = 0;
2701 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2702 g_value_set_instance (instance_and_params, instance);
2703 if (signal_return_type == G_TYPE_NONE)
2704 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2707 GValue return_value = { 0, };
2708 gchar *error = NULL;
2709 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2710 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2712 g_value_init (&return_value, rtype);
2714 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2716 G_VALUE_LCOPY (&return_value,
2718 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2721 g_value_unset (&return_value);
2724 g_warning ("%s: %s", G_STRLOC, error);
2727 /* we purposely leak the value here, it might not be
2728 * in a sane state if an error condition occured
2732 for (i = 0; i < n_params; i++)
2733 g_value_unset (param_values + i);
2734 g_value_unset (instance_and_params);
2741 * @instance: the instance the signal is being emitted on.
2742 * @signal_id: the signal id
2743 * @detail: the detail
2744 * @...: parameters to be passed to the signal, followed by a
2745 * location for the return value. If the return type of the signal
2746 * is #G_TYPE_NONE, the return value location can be omitted.
2750 * Note that g_signal_emit() resets the return value to the default
2751 * if no handlers are connected, in contrast to g_signal_emitv().
2754 g_signal_emit (gpointer instance,
2761 va_start (var_args, detail);
2762 g_signal_emit_valist (instance, signal_id, detail, var_args);
2767 * g_signal_emit_by_name:
2768 * @instance: the instance the signal is being emitted on.
2769 * @detailed_signal: a string of the form "signal-name::detail".
2770 * @...: parameters to be passed to the signal, followed by a
2771 * location for the return value. If the return type of the signal
2772 * is #G_TYPE_NONE, the return value location can be omitted.
2776 * Note that g_signal_emit_by_name() resets the return value to the default
2777 * if no handlers are connected, in contrast to g_signal_emitv().
2780 g_signal_emit_by_name (gpointer instance,
2781 const gchar *detailed_signal,
2787 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2788 g_return_if_fail (detailed_signal != NULL);
2791 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2798 va_start (var_args, detailed_signal);
2799 g_signal_emit_valist (instance, signal_id, detail, var_args);
2803 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2806 static inline gboolean
2807 accumulate (GSignalInvocationHint *ihint,
2808 GValue *return_accu,
2809 GValue *handler_return,
2810 SignalAccumulator *accumulator)
2812 gboolean continue_emission;
2817 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2818 g_value_reset (handler_return);
2820 return continue_emission;
2824 signal_emit_unlocked_R (SignalNode *node,
2827 GValue *emission_return,
2828 const GValue *instance_and_params)
2830 SignalAccumulator *accumulator;
2832 GClosure *class_closure;
2834 Handler *handler_list = NULL;
2835 GValue *return_accu, accu = { 0, };
2837 gulong max_sequential_handler_number;
2838 gboolean return_value_altered = FALSE;
2840 #ifdef G_ENABLE_DEBUG
2841 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2843 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2844 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2847 if (g_trap_instance_signals == instance)
2850 #endif /* G_ENABLE_DEBUG */
2853 signal_id = node->signal_id;
2854 if (node->flags & G_SIGNAL_NO_RECURSE)
2856 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2860 node->state = EMISSION_RESTART;
2862 return return_value_altered;
2865 accumulator = node->accumulator;
2869 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2870 return_accu = &accu;
2874 return_accu = emission_return;
2875 emission.instance = instance;
2876 emission.ihint.signal_id = node->signal_id;
2877 emission.ihint.detail = detail;
2878 emission.ihint.run_type = 0;
2880 emission.chain_type = G_TYPE_NONE;
2881 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2882 class_closure = signal_lookup_closure (node, instance);
2887 handler_unref_R (signal_id, instance, handler_list);
2888 max_sequential_handler_number = g_handler_sequential_number;
2889 hlist = handler_list_lookup (signal_id, instance);
2890 handler_list = hlist ? hlist->handlers : NULL;
2892 handler_ref (handler_list);
2894 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2896 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2898 emission.state = EMISSION_RUN;
2900 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2902 g_closure_invoke (class_closure,
2905 instance_and_params,
2907 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2908 emission.state == EMISSION_RUN)
2909 emission.state = EMISSION_STOP;
2911 emission.chain_type = G_TYPE_NONE;
2912 return_value_altered = TRUE;
2914 if (emission.state == EMISSION_STOP)
2916 else if (emission.state == EMISSION_RESTART)
2920 if (node->emission_hooks)
2922 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2925 emission.state = EMISSION_HOOK;
2926 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2929 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2931 if (!signal_hook->detail || signal_hook->detail == detail)
2933 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2935 was_in_call = G_HOOK_IN_CALL (hook);
2936 hook->flags |= G_HOOK_FLAG_IN_CALL;
2938 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2941 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2943 g_hook_destroy_link (node->emission_hooks, hook);
2945 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2948 if (emission.state == EMISSION_RESTART)
2954 Handler *handler = handler_list;
2956 emission.state = EMISSION_RUN;
2957 handler_ref (handler);
2964 handler_unref_R (signal_id, instance, handler_list);
2965 handler_list = handler;
2968 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2969 handler->sequential_number < max_sequential_handler_number)
2972 g_closure_invoke (handler->closure,
2975 instance_and_params,
2977 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2978 emission.state == EMISSION_RUN)
2979 emission.state = EMISSION_STOP;
2981 return_value_altered = TRUE;
2983 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2986 tmp = handler->next;
2990 handler_unref_R (signal_id, instance, handler_list);
2991 handler_list = handler;
2996 if (emission.state == EMISSION_STOP)
2998 else if (emission.state == EMISSION_RESTART)
3002 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3004 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3006 emission.state = EMISSION_RUN;
3008 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3010 g_closure_invoke (class_closure,
3013 instance_and_params,
3015 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3016 emission.state == EMISSION_RUN)
3017 emission.state = EMISSION_STOP;
3019 emission.chain_type = G_TYPE_NONE;
3020 return_value_altered = TRUE;
3022 if (emission.state == EMISSION_STOP)
3024 else if (emission.state == EMISSION_RESTART)
3030 Handler *handler = handler_list;
3032 emission.state = EMISSION_RUN;
3033 handler_ref (handler);
3038 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3039 handler->sequential_number < max_sequential_handler_number)
3042 g_closure_invoke (handler->closure,
3045 instance_and_params,
3047 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3048 emission.state == EMISSION_RUN)
3049 emission.state = EMISSION_STOP;
3051 return_value_altered = TRUE;
3053 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3056 tmp = handler->next;
3060 handler_unref_R (signal_id, instance, handler);
3065 if (emission.state == EMISSION_STOP)
3067 else if (emission.state == EMISSION_RESTART)
3073 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3075 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3077 gboolean need_unset = FALSE;
3079 emission.state = EMISSION_STOP;
3081 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3083 if (node->return_type != G_TYPE_NONE && !accumulator)
3085 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3088 g_closure_invoke (class_closure,
3089 node->return_type != G_TYPE_NONE ? &accu : NULL,
3091 instance_and_params,
3094 g_value_unset (&accu);
3096 emission.chain_type = G_TYPE_NONE;
3098 if (emission.state == EMISSION_RESTART)
3103 handler_unref_R (signal_id, instance, handler_list);
3105 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3108 g_value_unset (&accu);
3110 return return_value_altered;
3114 type_debug_name (GType type)
3118 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3119 return name ? name : "<unknown>";
3126 * g_signal_accumulator_true_handled:
3127 * @ihint: standard #GSignalAccumulator parameter
3128 * @return_accu: standard #GSignalAccumulator parameter
3129 * @handler_return: standard #GSignalAccumulator parameter
3130 * @dummy: standard #GSignalAccumulator parameter
3132 * A predefined #GSignalAccumulator for signals that return a
3133 * boolean values. The behavior that this accumulator gives is
3134 * that a return of %TRUE stops the signal emission: no further
3135 * callbacks will be invoked, while a return of %FALSE allows
3136 * the emission to coninue. The idea here is that a %TRUE return
3137 * indicates that the callback <emphasis>handled</emphasis> the signal,
3138 * and no further handling is needed.
3141 * Returns: standard #GSignalAccumulator result
3144 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3145 GValue *return_accu,
3146 const GValue *handler_return,
3149 gboolean continue_emission;
3150 gboolean signal_handled;
3152 signal_handled = g_value_get_boolean (handler_return);
3153 g_value_set_boolean (return_accu, signal_handled);
3154 continue_emission = !signal_handled;
3156 return continue_emission;
3159 /* --- compile standard marshallers --- */
3160 #include "gmarshal.c"
3162 #define __G_SIGNAL_C__
3163 #include "gobjectaliasdef.c"