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>
24 * @Short_description: A means for customization of object behaviour and a general purpose notification mechanism
27 * The basic concept of the signal system is that of the <emphasis>emission</emphasis>
29 * Signals are introduced per-type and are identified through strings.
30 * Signals introduced for a parent type are available in derived types as well,
31 * so basically they are a per-type facility that is inherited.
32 * A signal emission mainly involves invocation of a certain set of callbacks in
33 * precisely defined manner. There are two main categories of such callbacks,
35 * i'm referring to those types as "object types" in the following, simply
36 * because that is the context most users will encounter signals in.
38 * ones and user provided ones.
39 * The per-object callbacks are most often referred to as "object method
40 * handler" or "default (signal) handler", while user provided callbacks are
41 * usually just called "signal handler".
42 * The object method handler is provided at signal creation time (this most
43 * frequently happens at the end of an object class' creation), while user
44 * provided handlers are frequently connected and disconnected to/from a certain
45 * signal on certain object instances.
47 * A signal emission consists of five stages, unless prematurely stopped:
49 * <varlistentry><term></term><listitem><para>
50 * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
51 * </para></listitem></varlistentry>
52 * <varlistentry><term></term><listitem><para>
53 * 2 - Invocation of normal user-provided signal handlers (<emphasis>after</emphasis> flag %FALSE)
54 * </para></listitem></varlistentry>
55 * <varlistentry><term></term><listitem><para>
56 * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
57 * </para></listitem></varlistentry>
58 * <varlistentry><term></term><listitem><para>
59 * 4 - Invocation of user provided signal handlers, connected with an <emphasis>after</emphasis> flag of %TRUE
60 * </para></listitem></varlistentry>
61 * <varlistentry><term></term><listitem><para>
62 * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
63 * </para></listitem></varlistentry>
65 * The user-provided signal handlers are called in the order they were
67 * All handlers may prematurely stop a signal emission, and any number of
68 * handlers may be connected, disconnected, blocked or unblocked during
70 * There are certain criteria for skipping user handlers in stages 2 and 4
71 * of a signal emission.
72 * First, user handlers may be <emphasis>blocked</emphasis>, blocked handlers are omitted
73 * during callback invocation, to return from the "blocked" state, a
74 * handler has to get unblocked exactly the same amount of times
75 * it has been blocked before.
76 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
77 * "detail" argument passed in to g_signal_emit() has to match the detail
78 * argument of the signal handler currently subject to invocation.
79 * Specification of no detail argument for signal handlers (omission of the
80 * detail part of the signal specification upon connection) serves as a
81 * wildcard and matches any detail argument passed in to emission.
91 #include "gbsearcharray.h"
92 #include "gvaluecollector.h"
93 #include "gvaluetypes.h"
98 #include "gobjectalias.h"
104 /* pre allocation configurations
106 #define MAX_STACK_VALUES (16)
108 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
109 #ifdef G_ENABLE_DEBUG
110 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
111 static volatile gpointer g_trace_instance_signals = NULL;
112 static volatile gpointer g_trap_instance_signals = NULL;
113 #endif /* G_ENABLE_DEBUG */
116 /* --- typedefs --- */
117 typedef struct _SignalNode SignalNode;
118 typedef struct _SignalKey SignalKey;
119 typedef struct _Emission Emission;
120 typedef struct _Handler Handler;
121 typedef struct _HandlerList HandlerList;
122 typedef struct _HandlerMatch HandlerMatch;
132 /* --- prototypes --- */
133 static inline guint signal_id_lookup (GQuark quark,
135 static void signal_destroy_R (SignalNode *signal_node);
136 static inline HandlerList* handler_list_ensure (guint signal_id,
138 static inline HandlerList* handler_list_lookup (guint signal_id,
140 static inline Handler* handler_new (gboolean after);
141 static void handler_insert (guint signal_id,
144 static Handler* handler_lookup (gpointer instance,
147 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
150 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
152 static HandlerMatch* handlers_find (gpointer instance,
153 GSignalMatchType mask,
159 gboolean one_and_only);
160 static inline void handler_ref (Handler *handler);
161 static inline void handler_unref_R (guint signal_id,
164 static gint handler_lists_cmp (gconstpointer node1,
165 gconstpointer node2);
166 static inline void emission_push (Emission **emission_list_p,
168 static inline void emission_pop (Emission **emission_list_p,
170 static inline Emission* emission_find (Emission *emission_list,
174 static gint class_closures_cmp (gconstpointer node1,
175 gconstpointer node2);
176 static gint signal_key_cmp (gconstpointer node1,
177 gconstpointer node2);
178 static gboolean signal_emit_unlocked_R (SignalNode *node,
181 GValue *return_value,
182 const GValue *instance_and_params);
183 static const gchar * type_debug_name (GType type);
186 /* --- structures --- */
189 GSignalAccumulator func;
197 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
201 /* permanent portion */
207 /* reinitializable portion */
208 guint test_class_offset : 12;
211 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
212 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
213 GBSearchArray *class_closure_bsa;
214 SignalAccumulator *accumulator;
215 GSignalCMarshaller c_marshaller;
216 GHookList *emission_hooks;
218 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
219 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
232 GSignalInvocationHint ihint;
241 Handler *tail_before; /* normal signal handlers are appended here */
242 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
247 gulong sequential_number;
252 guint block_count : 16;
253 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
266 GType instance_type; /* 0 for default closure */
271 /* --- variables --- */
272 static GBSearchArray *g_signal_key_bsa = NULL;
273 static const GBSearchConfig g_signal_key_bconfig = {
276 G_BSEARCH_ARRAY_ALIGN_POWER2,
278 static GBSearchConfig g_signal_hlbsa_bconfig = {
279 sizeof (HandlerList),
283 static GBSearchConfig g_class_closure_bconfig = {
284 sizeof (ClassClosure),
288 static GHashTable *g_handler_list_bsa_ht = NULL;
289 static Emission *g_recursive_emissions = NULL;
290 static Emission *g_restart_emissions = NULL;
291 static gulong g_handler_sequential_number = 1;
292 G_LOCK_DEFINE_STATIC (g_signal_mutex);
293 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
294 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
297 /* --- signal nodes --- */
298 static guint g_n_signal_nodes = 0;
299 static SignalNode **g_signal_nodes = NULL;
301 static inline SignalNode*
302 LOOKUP_SIGNAL_NODE (register guint signal_id)
304 if (signal_id < g_n_signal_nodes)
305 return g_signal_nodes[signal_id];
311 /* --- functions --- */
313 signal_id_lookup (GQuark quark,
316 GType *ifaces, type = itype;
322 /* try looking up signals for this type and its ancestors */
325 SignalKey *signal_key;
328 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
331 return signal_key->signal_id;
333 type = g_type_parent (type);
337 /* no luck, try interfaces it exports */
338 ifaces = g_type_interfaces (itype, &n_ifaces);
341 SignalKey *signal_key;
343 key.itype = ifaces[n_ifaces];
344 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
349 return signal_key->signal_id;
358 class_closures_cmp (gconstpointer node1,
361 const ClassClosure *c1 = node1, *c2 = node2;
363 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
367 handler_lists_cmp (gconstpointer node1,
370 const HandlerList *hlist1 = node1, *hlist2 = node2;
372 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
375 static inline HandlerList*
376 handler_list_ensure (guint signal_id,
379 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
382 key.signal_id = signal_id;
384 key.tail_before = NULL;
385 key.tail_after = NULL;
388 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
389 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
390 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
394 GBSearchArray *o = hlbsa;
396 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
398 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
400 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
403 static inline HandlerList*
404 handler_list_lookup (guint signal_id,
407 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
410 key.signal_id = signal_id;
412 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
416 handler_lookup (gpointer instance,
420 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
426 for (i = 0; i < hlbsa->n_nodes; i++)
428 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
431 for (handler = hlist->handlers; handler; handler = handler->next)
432 if (handler->sequential_number == handler_id)
435 *signal_id_p = hlist->signal_id;
445 static inline HandlerMatch*
446 handler_match_prepend (HandlerMatch *list,
452 node = g_slice_new (HandlerMatch);
453 node->handler = handler;
455 node->signal_id = signal_id;
456 handler_ref (handler);
460 static inline HandlerMatch*
461 handler_match_free1_R (HandlerMatch *node,
464 HandlerMatch *next = node->next;
466 handler_unref_R (node->signal_id, instance, node->handler);
467 g_slice_free (HandlerMatch, node);
473 handlers_find (gpointer instance,
474 GSignalMatchType mask,
480 gboolean one_and_only)
482 HandlerMatch *mlist = NULL;
484 if (mask & G_SIGNAL_MATCH_ID)
486 HandlerList *hlist = handler_list_lookup (signal_id, instance);
488 SignalNode *node = NULL;
490 if (mask & G_SIGNAL_MATCH_FUNC)
492 node = LOOKUP_SIGNAL_NODE (signal_id);
493 if (!node || !node->c_marshaller)
498 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
499 if (handler->sequential_number &&
500 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
501 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
502 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
503 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
504 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
505 handler->closure->meta_marshal == 0 &&
506 ((GCClosure*) handler->closure)->callback == func)))
508 mlist = handler_match_prepend (mlist, handler, signal_id);
515 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
522 for (i = 0; i < hlbsa->n_nodes; i++)
524 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
525 SignalNode *node = NULL;
528 if (!(mask & G_SIGNAL_MATCH_FUNC))
530 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
531 if (!node->c_marshaller)
535 for (handler = hlist->handlers; handler; handler = handler->next)
536 if (handler->sequential_number &&
537 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
538 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
539 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
540 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
541 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
542 handler->closure->meta_marshal == 0 &&
543 ((GCClosure*) handler->closure)->callback == func)))
545 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
556 static inline Handler*
557 handler_new (gboolean after)
559 Handler *handler = g_slice_new (Handler);
560 #ifndef G_DISABLE_CHECKS
561 if (g_handler_sequential_number < 1)
562 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
565 handler->sequential_number = g_handler_sequential_number++;
566 handler->prev = NULL;
567 handler->next = NULL;
569 handler->ref_count = 1;
570 handler->block_count = 0;
571 handler->after = after != FALSE;
572 handler->closure = NULL;
578 handler_ref (Handler *handler)
580 g_return_if_fail (handler->ref_count > 0);
582 g_atomic_int_inc (&handler->ref_count);
586 handler_unref_R (guint signal_id,
592 g_return_if_fail (handler->ref_count > 0);
594 is_zero = g_atomic_int_dec_and_test (&handler->ref_count);
596 if (G_UNLIKELY (is_zero))
598 HandlerList *hlist = NULL;
601 handler->next->prev = handler->prev;
602 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
603 handler->prev->next = handler->next;
606 hlist = handler_list_lookup (signal_id, instance);
607 hlist->handlers = handler->next;
612 /* check if we are removing the handler pointed to by tail_before */
613 if (!handler->after && (!handler->next || handler->next->after))
616 hlist = handler_list_lookup (signal_id, instance);
619 g_assert (hlist->tail_before == handler); /* paranoid */
620 hlist->tail_before = handler->prev;
624 /* check if we are removing the handler pointed to by tail_after */
628 hlist = handler_list_lookup (signal_id, instance);
631 g_assert (hlist->tail_after == handler); /* paranoid */
632 hlist->tail_after = handler->prev;
638 g_closure_unref (handler->closure);
640 g_slice_free (Handler, handler);
645 handler_insert (guint signal_id,
651 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
653 hlist = handler_list_ensure (signal_id, instance);
654 if (!hlist->handlers)
656 hlist->handlers = handler;
658 hlist->tail_before = handler;
660 else if (handler->after)
662 handler->prev = hlist->tail_after;
663 hlist->tail_after->next = handler;
667 if (hlist->tail_before)
669 handler->next = hlist->tail_before->next;
671 handler->next->prev = handler;
672 handler->prev = hlist->tail_before;
673 hlist->tail_before->next = handler;
675 else /* insert !after handler into a list of only after handlers */
677 handler->next = hlist->handlers;
679 handler->next->prev = handler;
680 hlist->handlers = handler;
682 hlist->tail_before = handler;
686 hlist->tail_after = handler;
690 emission_push (Emission **emission_list_p,
693 emission->next = *emission_list_p;
694 *emission_list_p = emission;
698 emission_pop (Emission **emission_list_p,
701 Emission *node, *last = NULL;
703 for (node = *emission_list_p; node; last = node, node = last->next)
704 if (node == emission)
707 last->next = node->next;
709 *emission_list_p = node->next;
712 g_assert_not_reached ();
715 static inline Emission*
716 emission_find (Emission *emission_list,
723 for (emission = emission_list; emission; emission = emission->next)
724 if (emission->instance == instance &&
725 emission->ihint.signal_id == signal_id &&
726 emission->ihint.detail == detail)
731 static inline Emission*
732 emission_find_innermost (gpointer instance)
734 Emission *emission, *s = NULL, *c = NULL;
736 for (emission = g_restart_emissions; emission; emission = emission->next)
737 if (emission->instance == instance)
742 for (emission = g_recursive_emissions; emission; emission = emission->next)
743 if (emission->instance == instance)
753 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
757 signal_key_cmp (gconstpointer node1,
760 const SignalKey *key1 = node1, *key2 = node2;
762 if (key1->itype == key2->itype)
763 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
765 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
772 if (!g_n_signal_nodes)
774 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
775 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
776 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
778 /* invalid (0) signal_id */
779 g_n_signal_nodes = 1;
780 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
781 g_signal_nodes[0] = NULL;
787 _g_signals_destroy (GType itype)
792 for (i = 1; i < g_n_signal_nodes; i++)
794 SignalNode *node = g_signal_nodes[i];
796 if (node->itype == itype)
799 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
801 type_debug_name (node->itype));
803 signal_destroy_R (node);
810 * g_signal_stop_emission:
811 * @instance: the object whose signal handlers you wish to stop.
812 * @signal_id: the signal identifier, as returned by g_signal_lookup().
813 * @detail: the detail which the signal was emitted with.
815 * Stops a signal's current emission.
817 * This will prevent the default method from running, if the signal was
818 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
821 * Prints a warning if used on a signal which isn't being emitted.
824 g_signal_stop_emission (gpointer instance,
830 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
831 g_return_if_fail (signal_id > 0);
834 node = LOOKUP_SIGNAL_NODE (signal_id);
835 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
837 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
841 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
843 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
844 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
848 if (emission->state == EMISSION_HOOK)
849 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
850 node->name, instance);
851 else if (emission->state == EMISSION_RUN)
852 emission->state = EMISSION_STOP;
855 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
856 node->name, instance);
859 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
864 signal_finalize_hook (GHookList *hook_list,
867 GDestroyNotify destroy = hook->destroy;
871 hook->destroy = NULL;
873 destroy (hook->data);
879 * g_signal_add_emission_hook:
880 * @signal_id: the signal identifier, as returned by g_signal_lookup().
881 * @detail: the detail on which to call the hook.
882 * @hook_func: a #GSignalEmissionHook function.
883 * @hook_data: user data for @hook_func.
884 * @data_destroy: a #GDestroyNotify for @hook_data.
886 * Adds an emission hook for a signal, which will get called for any emission
887 * of that signal, independent of the instance. This is possible only
888 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
890 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
893 g_signal_add_emission_hook (guint signal_id,
895 GSignalEmissionHook hook_func,
897 GDestroyNotify data_destroy)
899 static gulong seq_hook_id = 1;
902 SignalHook *signal_hook;
904 g_return_val_if_fail (signal_id > 0, 0);
905 g_return_val_if_fail (hook_func != NULL, 0);
908 node = LOOKUP_SIGNAL_NODE (signal_id);
909 if (!node || node->destroyed)
911 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
915 if (node->flags & G_SIGNAL_NO_HOOKS)
917 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
921 if (detail && !(node->flags & G_SIGNAL_DETAILED))
923 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
927 if (!node->emission_hooks)
929 node->emission_hooks = g_new (GHookList, 1);
930 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
931 node->emission_hooks->finalize_hook = signal_finalize_hook;
933 hook = g_hook_alloc (node->emission_hooks);
934 hook->data = hook_data;
935 hook->func = (gpointer) hook_func;
936 hook->destroy = data_destroy;
937 signal_hook = SIGNAL_HOOK (hook);
938 signal_hook->detail = detail;
939 node->emission_hooks->seq_id = seq_hook_id;
940 g_hook_append (node->emission_hooks, hook);
941 seq_hook_id = node->emission_hooks->seq_id;
944 return hook->hook_id;
948 * g_signal_remove_emission_hook:
949 * @signal_id: the id of the signal
950 * @hook_id: the id of the emission hook, as returned by
951 * g_signal_add_emission_hook()
953 * Deletes an emission hook.
956 g_signal_remove_emission_hook (guint signal_id,
961 g_return_if_fail (signal_id > 0);
962 g_return_if_fail (hook_id > 0);
965 node = LOOKUP_SIGNAL_NODE (signal_id);
966 if (!node || node->destroyed)
967 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
968 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
969 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
974 signal_parse_name (const gchar *name,
977 gboolean force_quark)
979 const gchar *colon = strchr (name, ':');
984 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
985 if (signal_id && detail_p)
988 else if (colon[1] == ':')
991 guint l = colon - name;
995 memcpy (buffer, name, l);
997 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1001 gchar *signal = g_new (gchar, l + 1);
1003 memcpy (signal, name, l);
1005 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1009 if (signal_id && detail_p)
1010 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1018 * g_signal_parse_name:
1019 * @detailed_signal: a string of the form "signal-name::detail".
1020 * @itype: The interface/instance type that introduced "signal-name".
1021 * @signal_id_p: Location to store the signal id.
1022 * @detail_p: Location to store the detail quark.
1023 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1025 * Internal function to parse a signal name into its @signal_id
1026 * and @detail quark.
1028 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1031 g_signal_parse_name (const gchar *detailed_signal,
1035 gboolean force_detail_quark)
1041 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1042 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1045 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1048 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1049 if (!node || node->destroyed ||
1050 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1054 *signal_id_p = signal_id;
1062 * g_signal_stop_emission_by_name:
1063 * @instance: the object whose signal handlers you wish to stop.
1064 * @detailed_signal: a string of the form "signal-name::detail".
1066 * Stops a signal's current emission.
1068 * This is just like g_signal_stop_emission() except it will look up the
1069 * signal id for you.
1072 g_signal_stop_emission_by_name (gpointer instance,
1073 const gchar *detailed_signal)
1079 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1080 g_return_if_fail (detailed_signal != NULL);
1083 itype = G_TYPE_FROM_INSTANCE (instance);
1084 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1087 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1089 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1090 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1091 else if (!g_type_is_a (itype, node->itype))
1092 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1095 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1096 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1100 if (emission->state == EMISSION_HOOK)
1101 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1102 node->name, instance);
1103 else if (emission->state == EMISSION_RUN)
1104 emission->state = EMISSION_STOP;
1107 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1108 node->name, instance);
1112 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1118 * @name: the signal's name.
1119 * @itype: the type that the signal operates on.
1121 * Given the name of the signal and the type of object it connects to, gets
1122 * the signal's identifying integer. Emitting the signal by number is
1123 * somewhat faster than using the name each time.
1125 * Also tries the ancestors of the given type.
1127 * See g_signal_new() for details on allowed signal names.
1129 * Returns: the signal's identifying number, or 0 if no signal was found.
1132 g_signal_lookup (const gchar *name,
1136 g_return_val_if_fail (name != NULL, 0);
1137 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1140 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1144 /* give elaborate warnings */
1145 if (!g_type_name (itype))
1146 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1148 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1149 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1150 name, g_type_name (itype));
1151 else if (!g_type_class_peek (itype))
1152 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1153 name, g_type_name (itype));
1160 * g_signal_list_ids:
1161 * @itype: Instance or interface type.
1162 * @n_ids: Location to store the number of signal ids for @itype.
1164 * Lists the signals by id that a certain instance or interface type
1165 * created. Further information about the signals can be acquired through
1168 * Returns: Newly allocated array of signal IDs.
1171 g_signal_list_ids (GType itype,
1179 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1180 g_return_val_if_fail (n_ids != NULL, NULL);
1183 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1184 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1185 result = g_array_new (FALSE, FALSE, sizeof (guint));
1187 for (i = 0; i < n_nodes; i++)
1188 if (keys[i].itype == itype)
1190 const gchar *name = g_quark_to_string (keys[i].quark);
1192 /* Signal names with "_" in them are aliases to the same
1193 * name with "-" instead of "_".
1195 if (!strchr (name, '_'))
1196 g_array_append_val (result, keys[i].signal_id);
1198 *n_ids = result->len;
1202 /* give elaborate warnings */
1203 if (!g_type_name (itype))
1204 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1206 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1207 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1208 g_type_name (itype));
1209 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1210 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1211 g_type_name (itype));
1214 return (guint*) g_array_free (result, FALSE);
1219 * @signal_id: the signal's identifying number.
1221 * Given the signal's identifier, finds its name.
1223 * Two different signals may have the same name, if they have differing types.
1225 * Returns: the signal name, or %NULL if the signal number was invalid.
1227 G_CONST_RETURN gchar*
1228 g_signal_name (guint signal_id)
1234 node = LOOKUP_SIGNAL_NODE (signal_id);
1235 name = node ? node->name : NULL;
1238 return (char*) name;
1243 * @signal_id: The signal id of the signal to query information for.
1244 * @query: A user provided structure that is filled in with constant
1245 * values upon success.
1247 * Queries the signal system for in-depth information about a
1248 * specific signal. This function will fill in a user-provided
1249 * structure to hold signal-specific information. If an invalid
1250 * signal id is passed in, the @signal_id member of the #GSignalQuery
1251 * is 0. All members filled into the #GSignalQuery structure should
1252 * be considered constant and have to be left untouched.
1255 g_signal_query (guint signal_id,
1256 GSignalQuery *query)
1260 g_return_if_fail (query != NULL);
1263 node = LOOKUP_SIGNAL_NODE (signal_id);
1264 if (!node || node->destroyed)
1265 query->signal_id = 0;
1268 query->signal_id = node->signal_id;
1269 query->signal_name = node->name;
1270 query->itype = node->itype;
1271 query->signal_flags = node->flags;
1272 query->return_type = node->return_type;
1273 query->n_params = node->n_params;
1274 query->param_types = node->param_types;
1281 * @signal_name: the name for the signal
1282 * @itype: the type this signal pertains to. It will also pertain to
1283 * types which are derived from this type.
1284 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1285 * the default handler is to be invoked. You should at least specify
1286 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1287 * @class_offset: The offset of the function pointer in the class structure
1288 * for this type. Used to invoke a class method generically. Pass 0 to
1289 * not associate a class method with this signal.
1290 * @accumulator: the accumulator for this signal; may be %NULL.
1291 * @accu_data: user data for the @accumulator.
1292 * @c_marshaller: the function to translate arrays of parameter values to
1293 * signal emissions into C language callback invocations.
1294 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1295 * without a return value.
1296 * @n_params: the number of parameter types to follow.
1297 * @...: a list of types, one for each parameter.
1299 * Creates a new signal. (This is usually done in the class initializer.)
1301 * A signal name consists of segments consisting of ASCII letters and
1302 * digits, separated by either the '-' or '_' character. The first
1303 * character of a signal name must be a letter. Names which violate these
1304 * rules lead to undefined behaviour of the GSignal system.
1306 * When registering a signal and looking up a signal, either separator can
1307 * be used, but they cannot be mixed.
1309 * Returns: the signal id
1312 g_signal_new (const gchar *signal_name,
1314 GSignalFlags signal_flags,
1316 GSignalAccumulator accumulator,
1318 GSignalCMarshaller c_marshaller,
1326 g_return_val_if_fail (signal_name != NULL, 0);
1328 va_start (args, n_params);
1330 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1331 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1332 accumulator, accu_data, c_marshaller,
1333 return_type, n_params, args);
1337 /* optimize NOP emissions with NULL class handlers */
1338 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1339 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1344 node = LOOKUP_SIGNAL_NODE (signal_id);
1345 node->test_class_offset = class_offset;
1352 static inline ClassClosure*
1353 signal_find_class_closure (SignalNode *node,
1356 GBSearchArray *bsa = node->class_closure_bsa;
1363 /* cc->instance_type is 0 for default closure */
1365 key.instance_type = itype;
1366 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1367 while (!cc && key.instance_type)
1369 key.instance_type = g_type_parent (key.instance_type);
1370 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1378 static inline GClosure*
1379 signal_lookup_closure (SignalNode *node,
1380 GTypeInstance *instance)
1384 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1385 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1387 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1388 return cc ? cc->closure : NULL;
1392 signal_add_class_closure (SignalNode *node,
1398 /* can't optimize NOP emissions with overridden class closures */
1399 node->test_class_offset = 0;
1401 if (!node->class_closure_bsa)
1402 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1403 key.instance_type = itype;
1404 key.closure = g_closure_ref (closure);
1405 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1406 &g_class_closure_bconfig,
1408 g_closure_sink (closure);
1409 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1410 g_closure_set_marshal (closure, node->c_marshaller);
1415 * @signal_name: the name for the signal
1416 * @itype: the type this signal pertains to. It will also pertain to
1417 * types which are derived from this type.
1418 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1419 * the default handler is to be invoked. You should at least specify
1420 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1421 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1422 * @accumulator: the accumulator for this signal; may be %NULL.
1423 * @accu_data: user data for the @accumulator.
1424 * @c_marshaller: the function to translate arrays of parameter values to
1425 * signal emissions into C language callback invocations.
1426 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1427 * without a return value.
1428 * @n_params: the length of @param_types.
1429 * @param_types: an array types, one for each parameter.
1431 * Creates a new signal. (This is usually done in the class initializer.)
1433 * See g_signal_new() for details on allowed signal names.
1435 * Returns: the signal id
1438 g_signal_newv (const gchar *signal_name,
1440 GSignalFlags signal_flags,
1441 GClosure *class_closure,
1442 GSignalAccumulator accumulator,
1444 GSignalCMarshaller c_marshaller,
1453 g_return_val_if_fail (signal_name != NULL, 0);
1454 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1456 g_return_val_if_fail (param_types != NULL, 0);
1457 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1458 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1459 g_return_val_if_fail (accumulator == NULL, 0);
1461 g_return_val_if_fail (accu_data == NULL, 0);
1463 name = g_strdup (signal_name);
1464 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1468 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1469 node = LOOKUP_SIGNAL_NODE (signal_id);
1470 if (node && !node->destroyed)
1472 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1474 type_debug_name (node->itype),
1475 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1480 if (node && node->itype != itype)
1482 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1484 type_debug_name (itype),
1485 type_debug_name (node->itype));
1490 for (i = 0; i < n_params; i++)
1491 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1493 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1494 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1499 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1501 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1502 type_debug_name (return_type), type_debug_name (itype), name);
1507 if (return_type != G_TYPE_NONE &&
1508 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1510 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1511 type_debug_name (itype), name, type_debug_name (return_type));
1517 /* setup permanent portion of signal node */
1522 signal_id = g_n_signal_nodes++;
1523 node = g_new (SignalNode, 1);
1524 node->signal_id = signal_id;
1525 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1526 g_signal_nodes[signal_id] = node;
1527 node->itype = itype;
1530 key.quark = g_quark_from_string (node->name);
1531 key.signal_id = signal_id;
1532 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1533 g_strdelimit (name, "_", '-');
1534 node->name = g_intern_string (name);
1535 key.quark = g_quark_from_string (name);
1536 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1538 node->destroyed = FALSE;
1539 node->test_class_offset = 0;
1541 /* setup reinitializable portion */
1542 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1543 node->n_params = n_params;
1544 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1545 node->return_type = return_type;
1546 node->class_closure_bsa = NULL;
1549 node->accumulator = g_new (SignalAccumulator, 1);
1550 node->accumulator->func = accumulator;
1551 node->accumulator->data = accu_data;
1554 node->accumulator = NULL;
1555 node->c_marshaller = c_marshaller;
1556 node->emission_hooks = NULL;
1558 signal_add_class_closure (node, 0, class_closure);
1559 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1561 /* optimize NOP emissions */
1562 node->test_class_offset = TEST_CLASS_MAGIC;
1572 * g_signal_new_valist:
1573 * @signal_name: the name for the signal
1574 * @itype: the type this signal pertains to. It will also pertain to
1575 * types which are derived from this type.
1576 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1577 * the default handler is to be invoked. You should at least specify
1578 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1579 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1580 * @accumulator: the accumulator for this signal; may be %NULL.
1581 * @accu_data: user data for the @accumulator.
1582 * @c_marshaller: the function to translate arrays of parameter values to
1583 * signal emissions into C language callback invocations.
1584 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1585 * without a return value.
1586 * @n_params: the number of parameter types in @args.
1587 * @args: va_list of #GType, one for each parameter.
1589 * Creates a new signal. (This is usually done in the class initializer.)
1591 * See g_signal_new() for details on allowed signal names.
1593 * Returns: the signal id
1596 g_signal_new_valist (const gchar *signal_name,
1598 GSignalFlags signal_flags,
1599 GClosure *class_closure,
1600 GSignalAccumulator accumulator,
1602 GSignalCMarshaller c_marshaller,
1613 param_types = g_new (GType, n_params);
1615 for (i = 0; i < n_params; i++)
1616 param_types[i] = va_arg (args, GType);
1621 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1622 class_closure, accumulator, accu_data, c_marshaller,
1623 return_type, n_params, param_types);
1624 g_free (param_types);
1630 signal_destroy_R (SignalNode *signal_node)
1632 SignalNode node = *signal_node;
1634 signal_node->destroyed = TRUE;
1636 /* reentrancy caution, zero out real contents first */
1637 signal_node->test_class_offset = 0;
1638 signal_node->n_params = 0;
1639 signal_node->param_types = NULL;
1640 signal_node->return_type = 0;
1641 signal_node->class_closure_bsa = NULL;
1642 signal_node->accumulator = NULL;
1643 signal_node->c_marshaller = NULL;
1644 signal_node->emission_hooks = NULL;
1646 #ifdef G_ENABLE_DEBUG
1647 /* check current emissions */
1651 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1652 emission; emission = emission->next)
1653 if (emission->ihint.signal_id == node.signal_id)
1654 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1655 node.name, emission->instance);
1659 /* free contents that need to
1662 g_free (node.param_types);
1663 if (node.class_closure_bsa)
1667 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1669 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1671 g_closure_unref (cc->closure);
1673 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1675 g_free (node.accumulator);
1676 if (node.emission_hooks)
1678 g_hook_list_clear (node.emission_hooks);
1679 g_free (node.emission_hooks);
1685 * g_signal_override_class_closure:
1686 * @signal_id: the signal id
1687 * @instance_type: the instance type on which to override the class closure
1689 * @class_closure: the closure.
1691 * Overrides the class closure (i.e. the default handler) for the given signal
1692 * for emissions on instances of @instance_type. @instance_type must be derived
1693 * from the type to which the signal belongs.
1696 g_signal_override_class_closure (guint signal_id,
1697 GType instance_type,
1698 GClosure *class_closure)
1702 g_return_if_fail (signal_id > 0);
1703 g_return_if_fail (class_closure != NULL);
1706 node = LOOKUP_SIGNAL_NODE (signal_id);
1707 if (!g_type_is_a (instance_type, node->itype))
1708 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1711 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1713 if (cc && cc->instance_type == instance_type)
1714 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1716 signal_add_class_closure (node, instance_type, class_closure);
1722 * g_signal_chain_from_overridden:
1723 * @instance_and_params: the argument list of the signal emission. The first
1724 * element in the array is a #GValue for the instance the signal is being
1725 * emitted on. The rest are any arguments to be passed to the signal.
1726 * @return_value: Location for the return value.
1728 * Calls the original class closure of a signal. This function should only
1729 * be called from an overridden class closure; see
1730 * g_signal_override_class_closure().
1733 g_signal_chain_from_overridden (const GValue *instance_and_params,
1734 GValue *return_value)
1736 GType chain_type = 0, restore_type = 0;
1737 Emission *emission = NULL;
1738 GClosure *closure = NULL;
1742 g_return_if_fail (instance_and_params != NULL);
1743 instance = g_value_peek_pointer (instance_and_params);
1744 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1747 emission = emission_find_innermost (instance);
1750 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1752 g_assert (node != NULL); /* paranoid */
1754 /* we should probably do the same parameter checks as g_signal_emit() here.
1756 if (emission->chain_type != G_TYPE_NONE)
1758 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1760 g_assert (cc != NULL); /* closure currently in call stack */
1762 n_params = node->n_params;
1763 restore_type = cc->instance_type;
1764 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1765 if (cc && cc->instance_type != restore_type)
1767 closure = cc->closure;
1768 chain_type = cc->instance_type;
1772 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1775 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1778 emission->chain_type = chain_type;
1780 g_closure_invoke (closure,
1783 instance_and_params,
1786 emission->chain_type = restore_type;
1792 * g_signal_get_invocation_hint:
1793 * @instance: the instance to query
1795 * Returns the invocation hint of the innermost signal emission of instance.
1797 * Returns: the invocation hint of the innermost signal emission.
1799 GSignalInvocationHint*
1800 g_signal_get_invocation_hint (gpointer instance)
1802 Emission *emission = NULL;
1804 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1807 emission = emission_find_innermost (instance);
1810 return emission ? &emission->ihint : NULL;
1814 * g_signal_connect_closure_by_id:
1815 * @instance: the instance to connect to.
1816 * @signal_id: the id of the signal.
1817 * @detail: the detail.
1818 * @closure: the closure to connect.
1819 * @after: whether the handler should be called before or after the
1820 * default handler of the signal.
1822 * Connects a closure to a signal for a particular object.
1824 * Returns: the handler id
1827 g_signal_connect_closure_by_id (gpointer instance,
1834 gulong handler_seq_no = 0;
1836 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1837 g_return_val_if_fail (signal_id > 0, 0);
1838 g_return_val_if_fail (closure != NULL, 0);
1841 node = LOOKUP_SIGNAL_NODE (signal_id);
1844 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1845 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1846 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1847 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1850 Handler *handler = handler_new (after);
1852 handler_seq_no = handler->sequential_number;
1853 handler->detail = detail;
1854 handler->closure = g_closure_ref (closure);
1855 g_closure_sink (closure);
1856 handler_insert (signal_id, instance, handler);
1857 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1858 g_closure_set_marshal (closure, node->c_marshaller);
1862 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1865 return handler_seq_no;
1869 * g_signal_connect_closure:
1870 * @instance: the instance to connect to.
1871 * @detailed_signal: a string of the form "signal-name::detail".
1872 * @closure: the closure to connect.
1873 * @after: whether the handler should be called before or after the
1874 * default handler of the signal.
1876 * Connects a closure to a signal for a particular object.
1878 * Returns: the handler id
1881 g_signal_connect_closure (gpointer instance,
1882 const gchar *detailed_signal,
1887 gulong handler_seq_no = 0;
1891 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1892 g_return_val_if_fail (detailed_signal != NULL, 0);
1893 g_return_val_if_fail (closure != NULL, 0);
1896 itype = G_TYPE_FROM_INSTANCE (instance);
1897 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1900 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1902 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1903 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1904 else if (!g_type_is_a (itype, node->itype))
1905 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1908 Handler *handler = handler_new (after);
1910 handler_seq_no = handler->sequential_number;
1911 handler->detail = detail;
1912 handler->closure = g_closure_ref (closure);
1913 g_closure_sink (closure);
1914 handler_insert (signal_id, instance, handler);
1915 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1916 g_closure_set_marshal (handler->closure, node->c_marshaller);
1920 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1923 return handler_seq_no;
1927 * g_signal_connect_data:
1928 * @instance: the instance to connect to.
1929 * @detailed_signal: a string of the form "signal-name::detail".
1930 * @c_handler: the #GCallback to connect.
1931 * @data: data to pass to @c_handler calls.
1932 * @destroy_data: a #GClosureNotify for @data.
1933 * @connect_flags: a combination of #GConnectFlags.
1935 * Connects a #GCallback function to a signal for a particular object. Similar
1936 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
1937 * which will be called when the signal handler is disconnected and no longer
1938 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
1939 * <literal>..._swapped()</literal> variants of this function.
1941 * Returns: the handler id
1944 g_signal_connect_data (gpointer instance,
1945 const gchar *detailed_signal,
1946 GCallback c_handler,
1948 GClosureNotify destroy_data,
1949 GConnectFlags connect_flags)
1952 gulong handler_seq_no = 0;
1955 gboolean swapped, after;
1957 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1958 g_return_val_if_fail (detailed_signal != NULL, 0);
1959 g_return_val_if_fail (c_handler != NULL, 0);
1961 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1962 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1965 itype = G_TYPE_FROM_INSTANCE (instance);
1966 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1969 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1971 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1972 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1973 else if (!g_type_is_a (itype, node->itype))
1974 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1977 Handler *handler = handler_new (after);
1979 handler_seq_no = handler->sequential_number;
1980 handler->detail = detail;
1981 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1982 g_closure_sink (handler->closure);
1983 handler_insert (signal_id, instance, handler);
1984 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1985 g_closure_set_marshal (handler->closure, node->c_marshaller);
1989 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1992 return handler_seq_no;
1996 * g_signal_handler_block:
1997 * @instance: The instance to block the signal handler of.
1998 * @handler_id: Handler id of the handler to be blocked.
2000 * Blocks a handler of an instance so it will not be called during
2001 * any signal emissions unless it is unblocked again. Thus "blocking"
2002 * a signal handler means to temporarily deactive it, a signal handler
2003 * has to be unblocked exactly the same amount of times it has been
2004 * blocked before to become active again.
2006 * The @handler_id has to be a valid signal handler id, connected to a
2007 * signal of @instance.
2010 g_signal_handler_block (gpointer instance,
2015 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2016 g_return_if_fail (handler_id > 0);
2019 handler = handler_lookup (instance, handler_id, NULL);
2022 #ifndef G_DISABLE_CHECKS
2023 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2024 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2026 handler->block_count += 1;
2029 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2034 * g_signal_handler_unblock:
2035 * @instance: The instance to unblock the signal handler of.
2036 * @handler_id: Handler id of the handler to be unblocked.
2038 * Undoes the effect of a previous g_signal_handler_block() call.
2039 * A blocked handler is skipped during signal emissions and will not be
2040 * invoked, unblocking it (for exactly the amount of times it has been
2041 * blocked before) reverts its "blocked" state, so the handler will be
2042 * recognized by the signal system and is called upon future or currently
2043 * ongoing signal emissions (since the order in which handlers are
2044 * called during signal emissions is deterministic, whether the
2045 * unblocked handler in question is called as part of a currently
2046 * ongoing emission depends on how far that emission has proceeded
2049 * The @handler_id has to be a valid id of a signal handler that is
2050 * connected to a signal of @instance and is currently blocked.
2053 g_signal_handler_unblock (gpointer instance,
2058 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2059 g_return_if_fail (handler_id > 0);
2062 handler = handler_lookup (instance, handler_id, NULL);
2065 if (handler->block_count)
2066 handler->block_count -= 1;
2068 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2071 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2076 * g_signal_handler_disconnect:
2077 * @instance: The instance to remove the signal handler from.
2078 * @handler_id: Handler id of the handler to be disconnected.
2080 * Disconnects a handler from an instance so it will not be called during
2081 * any future or currently ongoing emissions of the signal it has been
2082 * connected to. The @handler_id becomes invalid and may be reused.
2084 * The @handler_id has to be a valid signal handler id, connected to a
2085 * signal of @instance.
2088 g_signal_handler_disconnect (gpointer instance,
2094 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2095 g_return_if_fail (handler_id > 0);
2098 handler = handler_lookup (instance, handler_id, &signal_id);
2101 handler->sequential_number = 0;
2102 handler->block_count = 1;
2103 handler_unref_R (signal_id, instance, handler);
2106 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2111 * g_signal_handler_is_connected:
2112 * @instance: The instance where a signal handler is sought.
2113 * @handler_id: the handler id.
2115 * Returns whether @handler_id is the id of a handler connected to @instance.
2117 * Returns: whether @handler_id identifies a handler connected to @instance.
2120 g_signal_handler_is_connected (gpointer instance,
2126 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2129 handler = handler_lookup (instance, handler_id, NULL);
2130 connected = handler != NULL;
2137 g_signal_handlers_destroy (gpointer instance)
2139 GBSearchArray *hlbsa;
2141 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2144 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2149 /* reentrancy caution, delete instance trace first */
2150 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2152 for (i = 0; i < hlbsa->n_nodes; i++)
2154 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2155 Handler *handler = hlist->handlers;
2159 Handler *tmp = handler;
2161 handler = tmp->next;
2162 tmp->block_count = 1;
2163 /* cruel unlink, this works because _all_ handlers vanish */
2166 if (tmp->sequential_number)
2168 tmp->sequential_number = 0;
2169 handler_unref_R (0, NULL, tmp);
2173 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2179 * g_signal_handler_find:
2180 * @instance: The instance owning the signal handler to be found.
2181 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2182 * and/or @data the handler has to match.
2183 * @signal_id: Signal the handler has to be connected to.
2184 * @detail: Signal detail the handler has to be connected to.
2185 * @closure: The closure the handler will invoke.
2186 * @func: The C closure callback of the handler (useless for non-C closures).
2187 * @data: The closure data of the handler's closure.
2189 * Finds the first signal handler that matches certain selection criteria.
2190 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2191 * flags, and the criteria values are passed as arguments.
2192 * The match @mask has to be non-0 for successful matches.
2193 * If no handler was found, 0 is returned.
2195 * Returns: A valid non-0 signal handler id for a successful match.
2198 g_signal_handler_find (gpointer instance,
2199 GSignalMatchType mask,
2206 gulong handler_seq_no = 0;
2208 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2209 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2211 if (mask & G_SIGNAL_MATCH_MASK)
2213 HandlerMatch *mlist;
2216 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2219 handler_seq_no = mlist->handler->sequential_number;
2220 handler_match_free1_R (mlist, instance);
2225 return handler_seq_no;
2229 signal_handlers_foreach_matched_R (gpointer instance,
2230 GSignalMatchType mask,
2236 void (*callback) (gpointer instance,
2237 gulong handler_seq_no))
2239 HandlerMatch *mlist;
2240 guint n_handlers = 0;
2242 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2246 if (mlist->handler->sequential_number)
2249 callback (instance, mlist->handler->sequential_number);
2252 mlist = handler_match_free1_R (mlist, instance);
2259 * g_signal_handlers_block_matched:
2260 * @instance: The instance to block handlers from.
2261 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2262 * and/or @data the handlers have to match.
2263 * @signal_id: Signal the handlers have to be connected to.
2264 * @detail: Signal detail the handlers have to be connected to.
2265 * @closure: The closure the handlers will invoke.
2266 * @func: The C closure callback of the handlers (useless for non-C closures).
2267 * @data: The closure data of the handlers' closures.
2269 * Blocks all handlers on an instance that match a certain selection criteria.
2270 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2271 * flags, and the criteria values are passed as arguments.
2272 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2273 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2274 * If no handlers were found, 0 is returned, the number of blocked handlers
2277 * Returns: The number of handlers that matched.
2280 g_signal_handlers_block_matched (gpointer instance,
2281 GSignalMatchType mask,
2288 guint n_handlers = 0;
2290 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2291 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2293 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2296 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2297 closure, func, data,
2298 g_signal_handler_block);
2306 * g_signal_handlers_unblock_matched:
2307 * @instance: The instance to unblock handlers from.
2308 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2309 * and/or @data the handlers have to match.
2310 * @signal_id: Signal the handlers have to be connected to.
2311 * @detail: Signal detail the handlers have to be connected to.
2312 * @closure: The closure the handlers will invoke.
2313 * @func: The C closure callback of the handlers (useless for non-C closures).
2314 * @data: The closure data of the handlers' closures.
2316 * Unblocks all handlers on an instance that match a certain selection
2317 * criteria. The criteria mask is passed as an OR-ed combination of
2318 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2319 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2320 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2321 * If no handlers were found, 0 is returned, the number of unblocked handlers
2322 * otherwise. The match criteria should not apply to any handlers that are
2323 * not currently blocked.
2325 * Returns: The number of handlers that matched.
2328 g_signal_handlers_unblock_matched (gpointer instance,
2329 GSignalMatchType mask,
2336 guint n_handlers = 0;
2338 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2339 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2341 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2344 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2345 closure, func, data,
2346 g_signal_handler_unblock);
2354 * g_signal_handlers_disconnect_matched:
2355 * @instance: The instance to remove handlers from.
2356 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2357 * and/or @data the handlers have to match.
2358 * @signal_id: Signal the handlers have to be connected to.
2359 * @detail: Signal detail the handlers have to be connected to.
2360 * @closure: The closure the handlers will invoke.
2361 * @func: The C closure callback of the handlers (useless for non-C closures).
2362 * @data: The closure data of the handlers' closures.
2364 * Disconnects all handlers on an instance that match a certain selection
2365 * criteria. The criteria mask is passed as an OR-ed combination of
2366 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2367 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2368 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2369 * If no handlers were found, 0 is returned, the number of disconnected
2370 * handlers otherwise.
2372 * Returns: The number of handlers that matched.
2375 g_signal_handlers_disconnect_matched (gpointer instance,
2376 GSignalMatchType mask,
2383 guint n_handlers = 0;
2385 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2386 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2388 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2391 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2392 closure, func, data,
2393 g_signal_handler_disconnect);
2401 * g_signal_has_handler_pending:
2402 * @instance: the object whose signal handlers are sought.
2403 * @signal_id: the signal id.
2404 * @detail: the detail.
2405 * @may_be_blocked: whether blocked handlers should count as match.
2407 * Returns whether there are any handlers connected to @instance for the
2408 * given signal id and detail.
2410 * One example of when you might use this is when the arguments to the
2411 * signal are difficult to compute. A class implementor may opt to not emit
2412 * the signal if no one is attached anyway, thus saving the cost of building
2415 * Returns: %TRUE if a handler is connected to the signal,
2419 g_signal_has_handler_pending (gpointer instance,
2422 gboolean may_be_blocked)
2424 HandlerMatch *mlist;
2425 gboolean has_pending;
2427 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2428 g_return_val_if_fail (signal_id > 0, FALSE);
2433 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2435 if (!(node->flags & G_SIGNAL_DETAILED))
2437 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2442 mlist = handlers_find (instance,
2443 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2444 signal_id, detail, NULL, NULL, NULL, TRUE);
2448 handler_match_free1_R (mlist, instance);
2451 has_pending = FALSE;
2457 static inline gboolean
2458 signal_check_skip_emission (SignalNode *node,
2464 /* are we able to check for NULL class handlers? */
2465 if (!node->test_class_offset)
2468 /* are there emission hooks pending? */
2469 if (node->emission_hooks && node->emission_hooks->hooks)
2472 /* is there a non-NULL class handler? */
2473 if (node->test_class_offset != TEST_CLASS_MAGIC)
2475 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2477 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2481 /* are signals being debugged? */
2482 #ifdef G_ENABLE_DEBUG
2483 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2485 #endif /* G_ENABLE_DEBUG */
2487 /* is this a no-recurse signal already in emission? */
2488 if (node->flags & G_SIGNAL_NO_RECURSE &&
2489 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2492 /* do we have pending handlers? */
2493 hlist = handler_list_lookup (node->signal_id, instance);
2494 if (hlist && hlist->handlers)
2497 /* none of the above, no emission required */
2503 * @instance_and_params: argument list for the signal emission. The first
2504 * element in the array is a #GValue for the instance the signal is
2505 * being emitted on. The rest are any arguments to be passed to the
2507 * @signal_id: the signal id
2508 * @detail: the detail
2509 * @return_value: Location to store the return value of the signal emission.
2513 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2514 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2517 g_signal_emitv (const GValue *instance_and_params,
2520 GValue *return_value)
2524 #ifdef G_ENABLE_DEBUG
2525 const GValue *param_values;
2529 g_return_if_fail (instance_and_params != NULL);
2530 instance = g_value_peek_pointer (instance_and_params);
2531 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2532 g_return_if_fail (signal_id > 0);
2534 #ifdef G_ENABLE_DEBUG
2535 param_values = instance_and_params + 1;
2539 node = LOOKUP_SIGNAL_NODE (signal_id);
2540 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2542 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2546 #ifdef G_ENABLE_DEBUG
2547 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2549 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2553 for (i = 0; i < node->n_params; i++)
2554 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2556 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2558 type_debug_name (node->param_types[i]),
2561 G_VALUE_TYPE_NAME (param_values + i));
2565 if (node->return_type != G_TYPE_NONE)
2569 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2571 type_debug_name (node->return_type),
2576 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2578 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2580 type_debug_name (node->return_type),
2582 G_VALUE_TYPE_NAME (return_value));
2588 return_value = NULL;
2589 #endif /* G_ENABLE_DEBUG */
2591 /* optimize NOP emissions */
2592 if (signal_check_skip_emission (node, instance, detail))
2594 /* nothing to do to emit this signal */
2596 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2601 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2605 * g_signal_emit_valist:
2606 * @instance: the instance the signal is being emitted on.
2607 * @signal_id: the signal id
2608 * @detail: the detail
2609 * @var_args: a list of parameters to be passed to the signal, followed by a
2610 * location for the return value. If the return type of the signal
2611 * is #G_TYPE_NONE, the return value location can be omitted.
2615 * Note that g_signal_emit_valist() resets the return value to the default
2616 * if no handlers are connected, in contrast to g_signal_emitv().
2619 g_signal_emit_valist (gpointer instance,
2624 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2625 GType signal_return_type;
2626 GValue *param_values;
2630 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2631 g_return_if_fail (signal_id > 0);
2634 node = LOOKUP_SIGNAL_NODE (signal_id);
2635 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2637 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2641 #ifndef G_DISABLE_CHECKS
2642 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2644 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2648 #endif /* !G_DISABLE_CHECKS */
2650 /* optimize NOP emissions */
2651 if (signal_check_skip_emission (node, instance, detail))
2653 /* nothing to do to emit this signal */
2655 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2659 n_params = node->n_params;
2660 signal_return_type = node->return_type;
2661 if (node->n_params < MAX_STACK_VALUES)
2662 instance_and_params = stack_values;
2665 free_me = g_new (GValue, node->n_params + 1);
2666 instance_and_params = free_me;
2668 param_values = instance_and_params + 1;
2669 for (i = 0; i < node->n_params; i++)
2672 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2673 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2675 param_values[i].g_type = 0;
2677 g_value_init (param_values + i, ptype);
2678 G_VALUE_COLLECT (param_values + i,
2680 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2684 g_warning ("%s: %s", G_STRLOC, error);
2687 /* we purposely leak the value here, it might not be
2688 * in a sane state if an error condition occoured
2691 g_value_unset (param_values + i);
2699 instance_and_params->g_type = 0;
2700 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2701 g_value_set_instance (instance_and_params, instance);
2702 if (signal_return_type == G_TYPE_NONE)
2703 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2706 GValue return_value = { 0, };
2707 gchar *error = NULL;
2708 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2709 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2711 g_value_init (&return_value, rtype);
2713 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2715 G_VALUE_LCOPY (&return_value,
2717 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2720 g_value_unset (&return_value);
2723 g_warning ("%s: %s", G_STRLOC, error);
2726 /* we purposely leak the value here, it might not be
2727 * in a sane state if an error condition occured
2731 for (i = 0; i < n_params; i++)
2732 g_value_unset (param_values + i);
2733 g_value_unset (instance_and_params);
2740 * @instance: the instance the signal is being emitted on.
2741 * @signal_id: the signal id
2742 * @detail: the detail
2743 * @...: parameters to be passed to the signal, followed by a
2744 * location for the return value. If the return type of the signal
2745 * is #G_TYPE_NONE, the return value location can be omitted.
2749 * Note that g_signal_emit() resets the return value to the default
2750 * if no handlers are connected, in contrast to g_signal_emitv().
2753 g_signal_emit (gpointer instance,
2760 va_start (var_args, detail);
2761 g_signal_emit_valist (instance, signal_id, detail, var_args);
2766 * g_signal_emit_by_name:
2767 * @instance: the instance the signal is being emitted on.
2768 * @detailed_signal: a string of the form "signal-name::detail".
2769 * @...: parameters to be passed to the signal, followed by a
2770 * location for the return value. If the return type of the signal
2771 * is #G_TYPE_NONE, the return value location can be omitted.
2775 * Note that g_signal_emit_by_name() resets the return value to the default
2776 * if no handlers are connected, in contrast to g_signal_emitv().
2779 g_signal_emit_by_name (gpointer instance,
2780 const gchar *detailed_signal,
2786 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2787 g_return_if_fail (detailed_signal != NULL);
2790 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2797 va_start (var_args, detailed_signal);
2798 g_signal_emit_valist (instance, signal_id, detail, var_args);
2802 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2805 static inline gboolean
2806 accumulate (GSignalInvocationHint *ihint,
2807 GValue *return_accu,
2808 GValue *handler_return,
2809 SignalAccumulator *accumulator)
2811 gboolean continue_emission;
2816 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2817 g_value_reset (handler_return);
2819 return continue_emission;
2823 signal_emit_unlocked_R (SignalNode *node,
2826 GValue *emission_return,
2827 const GValue *instance_and_params)
2829 SignalAccumulator *accumulator;
2831 GClosure *class_closure;
2833 Handler *handler_list = NULL;
2834 GValue *return_accu, accu = { 0, };
2836 gulong max_sequential_handler_number;
2837 gboolean return_value_altered = FALSE;
2839 #ifdef G_ENABLE_DEBUG
2840 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2842 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2843 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2846 if (g_trap_instance_signals == instance)
2849 #endif /* G_ENABLE_DEBUG */
2852 signal_id = node->signal_id;
2853 if (node->flags & G_SIGNAL_NO_RECURSE)
2855 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2859 node->state = EMISSION_RESTART;
2861 return return_value_altered;
2864 accumulator = node->accumulator;
2868 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2869 return_accu = &accu;
2873 return_accu = emission_return;
2874 emission.instance = instance;
2875 emission.ihint.signal_id = node->signal_id;
2876 emission.ihint.detail = detail;
2877 emission.ihint.run_type = 0;
2879 emission.chain_type = G_TYPE_NONE;
2880 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2881 class_closure = signal_lookup_closure (node, instance);
2886 handler_unref_R (signal_id, instance, handler_list);
2887 max_sequential_handler_number = g_handler_sequential_number;
2888 hlist = handler_list_lookup (signal_id, instance);
2889 handler_list = hlist ? hlist->handlers : NULL;
2891 handler_ref (handler_list);
2893 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2895 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2897 emission.state = EMISSION_RUN;
2899 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2901 g_closure_invoke (class_closure,
2904 instance_and_params,
2906 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2907 emission.state == EMISSION_RUN)
2908 emission.state = EMISSION_STOP;
2910 emission.chain_type = G_TYPE_NONE;
2911 return_value_altered = TRUE;
2913 if (emission.state == EMISSION_STOP)
2915 else if (emission.state == EMISSION_RESTART)
2919 if (node->emission_hooks)
2921 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2924 emission.state = EMISSION_HOOK;
2925 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2928 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2930 if (!signal_hook->detail || signal_hook->detail == detail)
2932 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2934 was_in_call = G_HOOK_IN_CALL (hook);
2935 hook->flags |= G_HOOK_FLAG_IN_CALL;
2937 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2940 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2942 g_hook_destroy_link (node->emission_hooks, hook);
2944 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2947 if (emission.state == EMISSION_RESTART)
2953 Handler *handler = handler_list;
2955 emission.state = EMISSION_RUN;
2956 handler_ref (handler);
2963 handler_unref_R (signal_id, instance, handler_list);
2964 handler_list = handler;
2967 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2968 handler->sequential_number < max_sequential_handler_number)
2971 g_closure_invoke (handler->closure,
2974 instance_and_params,
2976 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2977 emission.state == EMISSION_RUN)
2978 emission.state = EMISSION_STOP;
2980 return_value_altered = TRUE;
2982 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2985 tmp = handler->next;
2989 handler_unref_R (signal_id, instance, handler_list);
2990 handler_list = handler;
2995 if (emission.state == EMISSION_STOP)
2997 else if (emission.state == EMISSION_RESTART)
3001 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3003 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3005 emission.state = EMISSION_RUN;
3007 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3009 g_closure_invoke (class_closure,
3012 instance_and_params,
3014 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3015 emission.state == EMISSION_RUN)
3016 emission.state = EMISSION_STOP;
3018 emission.chain_type = G_TYPE_NONE;
3019 return_value_altered = TRUE;
3021 if (emission.state == EMISSION_STOP)
3023 else if (emission.state == EMISSION_RESTART)
3029 Handler *handler = handler_list;
3031 emission.state = EMISSION_RUN;
3032 handler_ref (handler);
3037 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3038 handler->sequential_number < max_sequential_handler_number)
3041 g_closure_invoke (handler->closure,
3044 instance_and_params,
3046 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3047 emission.state == EMISSION_RUN)
3048 emission.state = EMISSION_STOP;
3050 return_value_altered = TRUE;
3052 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3055 tmp = handler->next;
3059 handler_unref_R (signal_id, instance, handler);
3064 if (emission.state == EMISSION_STOP)
3066 else if (emission.state == EMISSION_RESTART)
3072 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3074 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3076 gboolean need_unset = FALSE;
3078 emission.state = EMISSION_STOP;
3080 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3082 if (node->return_type != G_TYPE_NONE && !accumulator)
3084 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3087 g_closure_invoke (class_closure,
3088 node->return_type != G_TYPE_NONE ? &accu : NULL,
3090 instance_and_params,
3093 g_value_unset (&accu);
3095 emission.chain_type = G_TYPE_NONE;
3097 if (emission.state == EMISSION_RESTART)
3102 handler_unref_R (signal_id, instance, handler_list);
3104 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3107 g_value_unset (&accu);
3109 return return_value_altered;
3113 type_debug_name (GType type)
3117 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3118 return name ? name : "<unknown>";
3125 * g_signal_accumulator_true_handled:
3126 * @ihint: standard #GSignalAccumulator parameter
3127 * @return_accu: standard #GSignalAccumulator parameter
3128 * @handler_return: standard #GSignalAccumulator parameter
3129 * @dummy: standard #GSignalAccumulator parameter
3131 * A predefined #GSignalAccumulator for signals that return a
3132 * boolean values. The behavior that this accumulator gives is
3133 * that a return of %TRUE stops the signal emission: no further
3134 * callbacks will be invoked, while a return of %FALSE allows
3135 * the emission to coninue. The idea here is that a %TRUE return
3136 * indicates that the callback <emphasis>handled</emphasis> the signal,
3137 * and no further handling is needed.
3140 * Returns: standard #GSignalAccumulator result
3143 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3144 GValue *return_accu,
3145 const GValue *handler_return,
3148 gboolean continue_emission;
3149 gboolean signal_handled;
3151 signal_handled = g_value_get_boolean (handler_return);
3152 g_value_set_boolean (return_accu, signal_handled);
3153 continue_emission = !signal_handled;
3155 return continue_emission;
3158 /* --- compile standard marshallers --- */
3159 #include "gmarshal.c"
3161 #define __G_SIGNAL_C__
3162 #include "gobjectaliasdef.c"