1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
33 #include "gbsearcharray.h"
34 #include "gvaluecollector.h"
35 #include "gvaluetypes.h"
39 #include "gobjectalias.h"
44 * @short_description: A means for customization of object behaviour
45 * and a general purpose notification mechanism
48 * The basic concept of the signal system is that of the
49 * <emphasis>emission</emphasis> of a signal. Signals are introduced
50 * per-type and are identified through strings. Signals introduced
51 * for a parent type are available in derived types as well, so
52 * basically they are a per-type facility that is inherited. A signal
53 * emission mainly involves invocation of a certain set of callbacks
54 * in precisely defined manner. There are two main categories of such
55 * callbacks, per-object
56 * <footnote><para>Although signals can deal with any kind of instantiatable
57 * type, i'm referring to those types as "object types" in the following,
58 * simply because that is the context most users will encounter signals in.
60 * ones and user provided ones.
61 * The per-object callbacks are most often referred to as "object method
62 * handler" or "default (signal) handler", while user provided callbacks are
63 * usually just called "signal handler".
64 * The object method handler is provided at signal creation time (this most
65 * frequently happens at the end of an object class' creation), while user
66 * provided handlers are frequently connected and disconnected to/from a certain
67 * signal on certain object instances.
69 * A signal emission consists of five stages, unless prematurely stopped:
71 * <varlistentry><term></term><listitem><para>
72 * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
73 * </para></listitem></varlistentry>
74 * <varlistentry><term></term><listitem><para>
75 * 2 - Invocation of normal user-provided signal handlers (<emphasis>after</emphasis> flag %FALSE)
76 * </para></listitem></varlistentry>
77 * <varlistentry><term></term><listitem><para>
78 * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
79 * </para></listitem></varlistentry>
80 * <varlistentry><term></term><listitem><para>
81 * 4 - Invocation of user provided signal handlers, connected with an <emphasis>after</emphasis> flag of %TRUE
82 * </para></listitem></varlistentry>
83 * <varlistentry><term></term><listitem><para>
84 * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
85 * </para></listitem></varlistentry>
87 * The user-provided signal handlers are called in the order they were
89 * All handlers may prematurely stop a signal emission, and any number of
90 * handlers may be connected, disconnected, blocked or unblocked during
92 * There are certain criteria for skipping user handlers in stages 2 and 4
93 * of a signal emission.
94 * First, user handlers may be <emphasis>blocked</emphasis>, blocked handlers are omitted
95 * during callback invocation, to return from the "blocked" state, a
96 * handler has to get unblocked exactly the same amount of times
97 * it has been blocked before.
98 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
99 * "detail" argument passed in to g_signal_emit() has to match the detail
100 * argument of the signal handler currently subject to invocation.
101 * Specification of no detail argument for signal handlers (omission of the
102 * detail part of the signal specification upon connection) serves as a
103 * wildcard and matches any detail argument passed in to emission.
107 /* pre allocation configurations
109 #define MAX_STACK_VALUES (16)
111 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
112 #ifdef G_ENABLE_DEBUG
113 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
114 static volatile gpointer g_trace_instance_signals = NULL;
115 static volatile gpointer g_trap_instance_signals = NULL;
116 #endif /* G_ENABLE_DEBUG */
119 /* --- typedefs --- */
120 typedef struct _SignalNode SignalNode;
121 typedef struct _SignalKey SignalKey;
122 typedef struct _Emission Emission;
123 typedef struct _Handler Handler;
124 typedef struct _HandlerList HandlerList;
125 typedef struct _HandlerMatch HandlerMatch;
135 /* --- prototypes --- */
136 static inline guint signal_id_lookup (GQuark quark,
138 static void signal_destroy_R (SignalNode *signal_node);
139 static inline HandlerList* handler_list_ensure (guint signal_id,
141 static inline HandlerList* handler_list_lookup (guint signal_id,
143 static inline Handler* handler_new (gboolean after);
144 static void handler_insert (guint signal_id,
147 static Handler* handler_lookup (gpointer instance,
150 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
153 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
155 static HandlerMatch* handlers_find (gpointer instance,
156 GSignalMatchType mask,
162 gboolean one_and_only);
163 static inline void handler_ref (Handler *handler);
164 static inline void handler_unref_R (guint signal_id,
167 static gint handler_lists_cmp (gconstpointer node1,
168 gconstpointer node2);
169 static inline void emission_push (Emission **emission_list_p,
171 static inline void emission_pop (Emission **emission_list_p,
173 static inline Emission* emission_find (Emission *emission_list,
177 static gint class_closures_cmp (gconstpointer node1,
178 gconstpointer node2);
179 static gint signal_key_cmp (gconstpointer node1,
180 gconstpointer node2);
181 static gboolean signal_emit_unlocked_R (SignalNode *node,
184 GValue *return_value,
185 const GValue *instance_and_params);
186 static const gchar * type_debug_name (GType type);
189 /* --- structures --- */
192 GSignalAccumulator func;
200 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
204 /* permanent portion */
210 /* reinitializable portion */
211 guint test_class_offset : 12;
214 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
215 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
216 GBSearchArray *class_closure_bsa;
217 SignalAccumulator *accumulator;
218 GSignalCMarshaller c_marshaller;
219 GHookList *emission_hooks;
221 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
222 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
235 GSignalInvocationHint ihint;
244 Handler *tail_before; /* normal signal handlers are appended here */
245 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
250 gulong sequential_number;
255 guint block_count : 16;
256 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
269 GType instance_type; /* 0 for default closure */
274 /* --- variables --- */
275 static GBSearchArray *g_signal_key_bsa = NULL;
276 static const GBSearchConfig g_signal_key_bconfig = {
279 G_BSEARCH_ARRAY_ALIGN_POWER2,
281 static GBSearchConfig g_signal_hlbsa_bconfig = {
282 sizeof (HandlerList),
286 static GBSearchConfig g_class_closure_bconfig = {
287 sizeof (ClassClosure),
291 static GHashTable *g_handler_list_bsa_ht = NULL;
292 static Emission *g_recursive_emissions = NULL;
293 static Emission *g_restart_emissions = NULL;
294 static gulong g_handler_sequential_number = 1;
295 G_LOCK_DEFINE_STATIC (g_signal_mutex);
296 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
297 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
300 /* --- signal nodes --- */
301 static guint g_n_signal_nodes = 0;
302 static SignalNode **g_signal_nodes = NULL;
304 static inline SignalNode*
305 LOOKUP_SIGNAL_NODE (register guint signal_id)
307 if (signal_id < g_n_signal_nodes)
308 return g_signal_nodes[signal_id];
314 /* --- functions --- */
316 signal_id_lookup (GQuark quark,
319 GType *ifaces, type = itype;
325 /* try looking up signals for this type and its ancestors */
328 SignalKey *signal_key;
331 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
334 return signal_key->signal_id;
336 type = g_type_parent (type);
340 /* no luck, try interfaces it exports */
341 ifaces = g_type_interfaces (itype, &n_ifaces);
344 SignalKey *signal_key;
346 key.itype = ifaces[n_ifaces];
347 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
352 return signal_key->signal_id;
361 class_closures_cmp (gconstpointer node1,
364 const ClassClosure *c1 = node1, *c2 = node2;
366 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
370 handler_lists_cmp (gconstpointer node1,
373 const HandlerList *hlist1 = node1, *hlist2 = node2;
375 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
378 static inline HandlerList*
379 handler_list_ensure (guint signal_id,
382 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
385 key.signal_id = signal_id;
387 key.tail_before = NULL;
388 key.tail_after = NULL;
391 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
392 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
393 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
397 GBSearchArray *o = hlbsa;
399 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
401 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
403 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
406 static inline HandlerList*
407 handler_list_lookup (guint signal_id,
410 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
413 key.signal_id = signal_id;
415 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
419 handler_lookup (gpointer instance,
423 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
429 for (i = 0; i < hlbsa->n_nodes; i++)
431 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
434 for (handler = hlist->handlers; handler; handler = handler->next)
435 if (handler->sequential_number == handler_id)
438 *signal_id_p = hlist->signal_id;
448 static inline HandlerMatch*
449 handler_match_prepend (HandlerMatch *list,
455 node = g_slice_new (HandlerMatch);
456 node->handler = handler;
458 node->signal_id = signal_id;
459 handler_ref (handler);
463 static inline HandlerMatch*
464 handler_match_free1_R (HandlerMatch *node,
467 HandlerMatch *next = node->next;
469 handler_unref_R (node->signal_id, instance, node->handler);
470 g_slice_free (HandlerMatch, node);
476 handlers_find (gpointer instance,
477 GSignalMatchType mask,
483 gboolean one_and_only)
485 HandlerMatch *mlist = NULL;
487 if (mask & G_SIGNAL_MATCH_ID)
489 HandlerList *hlist = handler_list_lookup (signal_id, instance);
491 SignalNode *node = NULL;
493 if (mask & G_SIGNAL_MATCH_FUNC)
495 node = LOOKUP_SIGNAL_NODE (signal_id);
496 if (!node || !node->c_marshaller)
501 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
502 if (handler->sequential_number &&
503 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
504 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
505 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
506 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
507 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
508 handler->closure->meta_marshal == 0 &&
509 ((GCClosure*) handler->closure)->callback == func)))
511 mlist = handler_match_prepend (mlist, handler, signal_id);
518 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
525 for (i = 0; i < hlbsa->n_nodes; i++)
527 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
528 SignalNode *node = NULL;
531 if (!(mask & G_SIGNAL_MATCH_FUNC))
533 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
534 if (!node->c_marshaller)
538 for (handler = hlist->handlers; handler; handler = handler->next)
539 if (handler->sequential_number &&
540 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
541 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
542 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
543 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
544 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
545 handler->closure->meta_marshal == 0 &&
546 ((GCClosure*) handler->closure)->callback == func)))
548 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
559 static inline Handler*
560 handler_new (gboolean after)
562 Handler *handler = g_slice_new (Handler);
563 #ifndef G_DISABLE_CHECKS
564 if (g_handler_sequential_number < 1)
565 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
568 handler->sequential_number = g_handler_sequential_number++;
569 handler->prev = NULL;
570 handler->next = NULL;
572 handler->ref_count = 1;
573 handler->block_count = 0;
574 handler->after = after != FALSE;
575 handler->closure = NULL;
581 handler_ref (Handler *handler)
583 g_return_if_fail (handler->ref_count > 0);
585 g_atomic_int_inc (&handler->ref_count);
589 handler_unref_R (guint signal_id,
595 g_return_if_fail (handler->ref_count > 0);
597 is_zero = g_atomic_int_dec_and_test (&handler->ref_count);
599 if (G_UNLIKELY (is_zero))
601 HandlerList *hlist = NULL;
604 handler->next->prev = handler->prev;
605 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
606 handler->prev->next = handler->next;
609 hlist = handler_list_lookup (signal_id, instance);
610 hlist->handlers = handler->next;
615 /* check if we are removing the handler pointed to by tail_before */
616 if (!handler->after && (!handler->next || handler->next->after))
619 hlist = handler_list_lookup (signal_id, instance);
622 g_assert (hlist->tail_before == handler); /* paranoid */
623 hlist->tail_before = handler->prev;
627 /* check if we are removing the handler pointed to by tail_after */
631 hlist = handler_list_lookup (signal_id, instance);
634 g_assert (hlist->tail_after == handler); /* paranoid */
635 hlist->tail_after = handler->prev;
641 g_closure_unref (handler->closure);
643 g_slice_free (Handler, handler);
648 handler_insert (guint signal_id,
654 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
656 hlist = handler_list_ensure (signal_id, instance);
657 if (!hlist->handlers)
659 hlist->handlers = handler;
661 hlist->tail_before = handler;
663 else if (handler->after)
665 handler->prev = hlist->tail_after;
666 hlist->tail_after->next = handler;
670 if (hlist->tail_before)
672 handler->next = hlist->tail_before->next;
674 handler->next->prev = handler;
675 handler->prev = hlist->tail_before;
676 hlist->tail_before->next = handler;
678 else /* insert !after handler into a list of only after handlers */
680 handler->next = hlist->handlers;
682 handler->next->prev = handler;
683 hlist->handlers = handler;
685 hlist->tail_before = handler;
689 hlist->tail_after = handler;
693 emission_push (Emission **emission_list_p,
696 emission->next = *emission_list_p;
697 *emission_list_p = emission;
701 emission_pop (Emission **emission_list_p,
704 Emission *node, *last = NULL;
706 for (node = *emission_list_p; node; last = node, node = last->next)
707 if (node == emission)
710 last->next = node->next;
712 *emission_list_p = node->next;
715 g_assert_not_reached ();
718 static inline Emission*
719 emission_find (Emission *emission_list,
726 for (emission = emission_list; emission; emission = emission->next)
727 if (emission->instance == instance &&
728 emission->ihint.signal_id == signal_id &&
729 emission->ihint.detail == detail)
734 static inline Emission*
735 emission_find_innermost (gpointer instance)
737 Emission *emission, *s = NULL, *c = NULL;
739 for (emission = g_restart_emissions; emission; emission = emission->next)
740 if (emission->instance == instance)
745 for (emission = g_recursive_emissions; emission; emission = emission->next)
746 if (emission->instance == instance)
756 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
760 signal_key_cmp (gconstpointer node1,
763 const SignalKey *key1 = node1, *key2 = node2;
765 if (key1->itype == key2->itype)
766 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
768 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
775 if (!g_n_signal_nodes)
777 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
778 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
779 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
781 /* invalid (0) signal_id */
782 g_n_signal_nodes = 1;
783 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
784 g_signal_nodes[0] = NULL;
790 _g_signals_destroy (GType itype)
795 for (i = 1; i < g_n_signal_nodes; i++)
797 SignalNode *node = g_signal_nodes[i];
799 if (node->itype == itype)
802 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
804 type_debug_name (node->itype));
806 signal_destroy_R (node);
813 * g_signal_stop_emission:
814 * @instance: the object whose signal handlers you wish to stop.
815 * @signal_id: the signal identifier, as returned by g_signal_lookup().
816 * @detail: the detail which the signal was emitted with.
818 * Stops a signal's current emission.
820 * This will prevent the default method from running, if the signal was
821 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
824 * Prints a warning if used on a signal which isn't being emitted.
827 g_signal_stop_emission (gpointer instance,
833 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
834 g_return_if_fail (signal_id > 0);
837 node = LOOKUP_SIGNAL_NODE (signal_id);
838 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
840 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
844 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
846 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
847 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
851 if (emission->state == EMISSION_HOOK)
852 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
853 node->name, instance);
854 else if (emission->state == EMISSION_RUN)
855 emission->state = EMISSION_STOP;
858 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
859 node->name, instance);
862 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
867 signal_finalize_hook (GHookList *hook_list,
870 GDestroyNotify destroy = hook->destroy;
874 hook->destroy = NULL;
876 destroy (hook->data);
882 * g_signal_add_emission_hook:
883 * @signal_id: the signal identifier, as returned by g_signal_lookup().
884 * @detail: the detail on which to call the hook.
885 * @hook_func: a #GSignalEmissionHook function.
886 * @hook_data: user data for @hook_func.
887 * @data_destroy: a #GDestroyNotify for @hook_data.
889 * Adds an emission hook for a signal, which will get called for any emission
890 * of that signal, independent of the instance. This is possible only
891 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
893 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
896 g_signal_add_emission_hook (guint signal_id,
898 GSignalEmissionHook hook_func,
900 GDestroyNotify data_destroy)
902 static gulong seq_hook_id = 1;
905 SignalHook *signal_hook;
907 g_return_val_if_fail (signal_id > 0, 0);
908 g_return_val_if_fail (hook_func != NULL, 0);
911 node = LOOKUP_SIGNAL_NODE (signal_id);
912 if (!node || node->destroyed)
914 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
918 if (node->flags & G_SIGNAL_NO_HOOKS)
920 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
924 if (detail && !(node->flags & G_SIGNAL_DETAILED))
926 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
930 if (!node->emission_hooks)
932 node->emission_hooks = g_new (GHookList, 1);
933 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
934 node->emission_hooks->finalize_hook = signal_finalize_hook;
936 hook = g_hook_alloc (node->emission_hooks);
937 hook->data = hook_data;
938 hook->func = (gpointer) hook_func;
939 hook->destroy = data_destroy;
940 signal_hook = SIGNAL_HOOK (hook);
941 signal_hook->detail = detail;
942 node->emission_hooks->seq_id = seq_hook_id;
943 g_hook_append (node->emission_hooks, hook);
944 seq_hook_id = node->emission_hooks->seq_id;
947 return hook->hook_id;
951 * g_signal_remove_emission_hook:
952 * @signal_id: the id of the signal
953 * @hook_id: the id of the emission hook, as returned by
954 * g_signal_add_emission_hook()
956 * Deletes an emission hook.
959 g_signal_remove_emission_hook (guint signal_id,
964 g_return_if_fail (signal_id > 0);
965 g_return_if_fail (hook_id > 0);
968 node = LOOKUP_SIGNAL_NODE (signal_id);
969 if (!node || node->destroyed)
970 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
971 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
972 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
977 signal_parse_name (const gchar *name,
980 gboolean force_quark)
982 const gchar *colon = strchr (name, ':');
987 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
988 if (signal_id && detail_p)
991 else if (colon[1] == ':')
994 guint l = colon - name;
998 memcpy (buffer, name, l);
1000 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1004 gchar *signal = g_new (gchar, l + 1);
1006 memcpy (signal, name, l);
1008 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1012 if (signal_id && detail_p)
1013 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1021 * g_signal_parse_name:
1022 * @detailed_signal: a string of the form "signal-name::detail".
1023 * @itype: The interface/instance type that introduced "signal-name".
1024 * @signal_id_p: Location to store the signal id.
1025 * @detail_p: Location to store the detail quark.
1026 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1028 * Internal function to parse a signal name into its @signal_id
1029 * and @detail quark.
1031 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1034 g_signal_parse_name (const gchar *detailed_signal,
1038 gboolean force_detail_quark)
1044 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1045 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1048 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1051 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1052 if (!node || node->destroyed ||
1053 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1057 *signal_id_p = signal_id;
1065 * g_signal_stop_emission_by_name:
1066 * @instance: the object whose signal handlers you wish to stop.
1067 * @detailed_signal: a string of the form "signal-name::detail".
1069 * Stops a signal's current emission.
1071 * This is just like g_signal_stop_emission() except it will look up the
1072 * signal id for you.
1075 g_signal_stop_emission_by_name (gpointer instance,
1076 const gchar *detailed_signal)
1082 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1083 g_return_if_fail (detailed_signal != NULL);
1086 itype = G_TYPE_FROM_INSTANCE (instance);
1087 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1090 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1092 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1093 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1094 else if (!g_type_is_a (itype, node->itype))
1095 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1098 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1099 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1103 if (emission->state == EMISSION_HOOK)
1104 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1105 node->name, instance);
1106 else if (emission->state == EMISSION_RUN)
1107 emission->state = EMISSION_STOP;
1110 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1111 node->name, instance);
1115 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1121 * @name: the signal's name.
1122 * @itype: the type that the signal operates on.
1124 * Given the name of the signal and the type of object it connects to, gets
1125 * the signal's identifying integer. Emitting the signal by number is
1126 * somewhat faster than using the name each time.
1128 * Also tries the ancestors of the given type.
1130 * See g_signal_new() for details on allowed signal names.
1132 * Returns: the signal's identifying number, or 0 if no signal was found.
1135 g_signal_lookup (const gchar *name,
1139 g_return_val_if_fail (name != NULL, 0);
1140 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1143 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1147 /* give elaborate warnings */
1148 if (!g_type_name (itype))
1149 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'",
1151 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1152 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1153 name, g_type_name (itype));
1154 else if (!g_type_class_peek (itype))
1155 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1156 name, g_type_name (itype));
1163 * g_signal_list_ids:
1164 * @itype: Instance or interface type.
1165 * @n_ids: Location to store the number of signal ids for @itype.
1167 * Lists the signals by id that a certain instance or interface type
1168 * created. Further information about the signals can be acquired through
1171 * Returns: Newly allocated array of signal IDs.
1174 g_signal_list_ids (GType itype,
1182 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1183 g_return_val_if_fail (n_ids != NULL, NULL);
1186 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1187 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1188 result = g_array_new (FALSE, FALSE, sizeof (guint));
1190 for (i = 0; i < n_nodes; i++)
1191 if (keys[i].itype == itype)
1193 const gchar *name = g_quark_to_string (keys[i].quark);
1195 /* Signal names with "_" in them are aliases to the same
1196 * name with "-" instead of "_".
1198 if (!strchr (name, '_'))
1199 g_array_append_val (result, keys[i].signal_id);
1201 *n_ids = result->len;
1205 /* give elaborate warnings */
1206 if (!g_type_name (itype))
1207 g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'",
1209 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1210 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1211 g_type_name (itype));
1212 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1213 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1214 g_type_name (itype));
1217 return (guint*) g_array_free (result, FALSE);
1222 * @signal_id: the signal's identifying number.
1224 * Given the signal's identifier, finds its name.
1226 * Two different signals may have the same name, if they have differing types.
1228 * Returns: the signal name, or %NULL if the signal number was invalid.
1230 G_CONST_RETURN gchar*
1231 g_signal_name (guint signal_id)
1237 node = LOOKUP_SIGNAL_NODE (signal_id);
1238 name = node ? node->name : NULL;
1241 return (char*) name;
1246 * @signal_id: The signal id of the signal to query information for.
1247 * @query: A user provided structure that is filled in with constant
1248 * values upon success.
1250 * Queries the signal system for in-depth information about a
1251 * specific signal. This function will fill in a user-provided
1252 * structure to hold signal-specific information. If an invalid
1253 * signal id is passed in, the @signal_id member of the #GSignalQuery
1254 * is 0. All members filled into the #GSignalQuery structure should
1255 * be considered constant and have to be left untouched.
1258 g_signal_query (guint signal_id,
1259 GSignalQuery *query)
1263 g_return_if_fail (query != NULL);
1266 node = LOOKUP_SIGNAL_NODE (signal_id);
1267 if (!node || node->destroyed)
1268 query->signal_id = 0;
1271 query->signal_id = node->signal_id;
1272 query->signal_name = node->name;
1273 query->itype = node->itype;
1274 query->signal_flags = node->flags;
1275 query->return_type = node->return_type;
1276 query->n_params = node->n_params;
1277 query->param_types = node->param_types;
1284 * @signal_name: the name for the signal
1285 * @itype: the type this signal pertains to. It will also pertain to
1286 * types which are derived from this type.
1287 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1288 * the default handler is to be invoked. You should at least specify
1289 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1290 * @class_offset: The offset of the function pointer in the class structure
1291 * for this type. Used to invoke a class method generically. Pass 0 to
1292 * not associate a class method with this signal.
1293 * @accumulator: the accumulator for this signal; may be %NULL.
1294 * @accu_data: user data for the @accumulator.
1295 * @c_marshaller: the function to translate arrays of parameter values to
1296 * signal emissions into C language callback invocations.
1297 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1298 * without a return value.
1299 * @n_params: the number of parameter types to follow.
1300 * @...: a list of types, one for each parameter.
1302 * Creates a new signal. (This is usually done in the class initializer.)
1304 * A signal name consists of segments consisting of ASCII letters and
1305 * digits, separated by either the '-' or '_' character. The first
1306 * character of a signal name must be a letter. Names which violate these
1307 * rules lead to undefined behaviour of the GSignal system.
1309 * When registering a signal and looking up a signal, either separator can
1310 * be used, but they cannot be mixed.
1312 * Returns: the signal id
1315 g_signal_new (const gchar *signal_name,
1317 GSignalFlags signal_flags,
1319 GSignalAccumulator accumulator,
1321 GSignalCMarshaller c_marshaller,
1329 g_return_val_if_fail (signal_name != NULL, 0);
1331 va_start (args, n_params);
1333 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1334 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1335 accumulator, accu_data, c_marshaller,
1336 return_type, n_params, args);
1340 /* optimize NOP emissions with NULL class handlers */
1341 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1342 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1347 node = LOOKUP_SIGNAL_NODE (signal_id);
1348 node->test_class_offset = class_offset;
1356 * g_signal_new_class_handler:
1357 * @signal_name: the name for the signal
1358 * @itype: the type this signal pertains to. It will also pertain to
1359 * types which are derived from this type.
1360 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1361 * the default handler is to be invoked. You should at least specify
1362 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1363 * @class_handler: a #GCallback which acts as class implementation of
1364 * this signal. Used to invoke a class method generically. Pass %NULL to
1365 * not associate a class method with this signal.
1366 * @accumulator: the accumulator for this signal; may be %NULL.
1367 * @accu_data: user data for the @accumulator.
1368 * @c_marshaller: the function to translate arrays of parameter values to
1369 * signal emissions into C language callback invocations.
1370 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1371 * without a return value.
1372 * @n_params: the number of parameter types to follow.
1373 * @...: a list of types, one for each parameter.
1375 * Creates a new signal. (This is usually done in the class initializer.)
1377 * This is a variant of g_signal_new() that takes a C callback instead
1378 * off a class offset for the signal's class handler. This function
1379 * doesn't need a function pointer exposed in the class structure of
1380 * an object definition, instead the function pointer is passed
1381 * directly and can be overriden by derived classes with
1382 * g_signal_override_class_closure() or
1383 * g_signal_override_class_handler()and chained to with
1384 * g_signal_chain_from_overridden() or
1385 * g_signal_chain_from_overridden().
1387 * See g_signal_new() for information about signal names.
1389 * Returns: the signal id
1394 g_signal_new_class_handler (const gchar *signal_name,
1396 GSignalFlags signal_flags,
1397 GCallback class_handler,
1398 GSignalAccumulator accumulator,
1400 GSignalCMarshaller c_marshaller,
1408 g_return_val_if_fail (signal_name != NULL, 0);
1410 va_start (args, n_params);
1412 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1413 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1414 accumulator, accu_data, c_marshaller,
1415 return_type, n_params, args);
1422 static inline ClassClosure*
1423 signal_find_class_closure (SignalNode *node,
1426 GBSearchArray *bsa = node->class_closure_bsa;
1433 /* cc->instance_type is 0 for default closure */
1435 key.instance_type = itype;
1436 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1437 while (!cc && key.instance_type)
1439 key.instance_type = g_type_parent (key.instance_type);
1440 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1448 static inline GClosure*
1449 signal_lookup_closure (SignalNode *node,
1450 GTypeInstance *instance)
1454 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1455 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1457 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1458 return cc ? cc->closure : NULL;
1462 signal_add_class_closure (SignalNode *node,
1468 /* can't optimize NOP emissions with overridden class closures */
1469 node->test_class_offset = 0;
1471 if (!node->class_closure_bsa)
1472 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1473 key.instance_type = itype;
1474 key.closure = g_closure_ref (closure);
1475 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1476 &g_class_closure_bconfig,
1478 g_closure_sink (closure);
1479 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1480 g_closure_set_marshal (closure, node->c_marshaller);
1485 * @signal_name: the name for the signal
1486 * @itype: the type this signal pertains to. It will also pertain to
1487 * types which are derived from this type.
1488 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1489 * the default handler is to be invoked. You should at least specify
1490 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1491 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1492 * @accumulator: the accumulator for this signal; may be %NULL.
1493 * @accu_data: user data for the @accumulator.
1494 * @c_marshaller: the function to translate arrays of parameter values to
1495 * signal emissions into C language callback invocations.
1496 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1497 * without a return value.
1498 * @n_params: the length of @param_types.
1499 * @param_types: an array types, one for each parameter.
1501 * Creates a new signal. (This is usually done in the class initializer.)
1503 * See g_signal_new() for details on allowed signal names.
1505 * Returns: the signal id
1508 g_signal_newv (const gchar *signal_name,
1510 GSignalFlags signal_flags,
1511 GClosure *class_closure,
1512 GSignalAccumulator accumulator,
1514 GSignalCMarshaller c_marshaller,
1523 g_return_val_if_fail (signal_name != NULL, 0);
1524 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1526 g_return_val_if_fail (param_types != NULL, 0);
1527 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1528 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1529 g_return_val_if_fail (accumulator == NULL, 0);
1531 g_return_val_if_fail (accu_data == NULL, 0);
1533 name = g_strdup (signal_name);
1534 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1538 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1539 node = LOOKUP_SIGNAL_NODE (signal_id);
1540 if (node && !node->destroyed)
1542 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1544 type_debug_name (node->itype),
1545 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1550 if (node && node->itype != itype)
1552 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1554 type_debug_name (itype),
1555 type_debug_name (node->itype));
1560 for (i = 0; i < n_params; i++)
1561 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1563 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1564 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1569 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1571 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1572 type_debug_name (return_type), type_debug_name (itype), name);
1577 if (return_type != G_TYPE_NONE &&
1578 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1580 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1581 type_debug_name (itype), name, type_debug_name (return_type));
1587 /* setup permanent portion of signal node */
1592 signal_id = g_n_signal_nodes++;
1593 node = g_new (SignalNode, 1);
1594 node->signal_id = signal_id;
1595 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1596 g_signal_nodes[signal_id] = node;
1597 node->itype = itype;
1600 key.quark = g_quark_from_string (node->name);
1601 key.signal_id = signal_id;
1602 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1603 g_strdelimit (name, "_", '-');
1604 node->name = g_intern_string (name);
1605 key.quark = g_quark_from_string (name);
1606 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1608 node->destroyed = FALSE;
1609 node->test_class_offset = 0;
1611 /* setup reinitializable portion */
1612 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1613 node->n_params = n_params;
1614 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1615 node->return_type = return_type;
1616 node->class_closure_bsa = NULL;
1619 node->accumulator = g_new (SignalAccumulator, 1);
1620 node->accumulator->func = accumulator;
1621 node->accumulator->data = accu_data;
1624 node->accumulator = NULL;
1625 node->c_marshaller = c_marshaller;
1626 node->emission_hooks = NULL;
1628 signal_add_class_closure (node, 0, class_closure);
1629 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1631 /* optimize NOP emissions */
1632 node->test_class_offset = TEST_CLASS_MAGIC;
1642 * g_signal_new_valist:
1643 * @signal_name: the name for the signal
1644 * @itype: the type this signal pertains to. It will also pertain to
1645 * types which are derived from this type.
1646 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1647 * the default handler is to be invoked. You should at least specify
1648 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1649 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1650 * @accumulator: the accumulator for this signal; may be %NULL.
1651 * @accu_data: user data for the @accumulator.
1652 * @c_marshaller: the function to translate arrays of parameter values to
1653 * signal emissions into C language callback invocations.
1654 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1655 * without a return value.
1656 * @n_params: the number of parameter types in @args.
1657 * @args: va_list of #GType, one for each parameter.
1659 * Creates a new signal. (This is usually done in the class initializer.)
1661 * See g_signal_new() for details on allowed signal names.
1663 * Returns: the signal id
1666 g_signal_new_valist (const gchar *signal_name,
1668 GSignalFlags signal_flags,
1669 GClosure *class_closure,
1670 GSignalAccumulator accumulator,
1672 GSignalCMarshaller c_marshaller,
1683 param_types = g_new (GType, n_params);
1685 for (i = 0; i < n_params; i++)
1686 param_types[i] = va_arg (args, GType);
1691 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1692 class_closure, accumulator, accu_data, c_marshaller,
1693 return_type, n_params, param_types);
1694 g_free (param_types);
1700 signal_destroy_R (SignalNode *signal_node)
1702 SignalNode node = *signal_node;
1704 signal_node->destroyed = TRUE;
1706 /* reentrancy caution, zero out real contents first */
1707 signal_node->test_class_offset = 0;
1708 signal_node->n_params = 0;
1709 signal_node->param_types = NULL;
1710 signal_node->return_type = 0;
1711 signal_node->class_closure_bsa = NULL;
1712 signal_node->accumulator = NULL;
1713 signal_node->c_marshaller = NULL;
1714 signal_node->emission_hooks = NULL;
1716 #ifdef G_ENABLE_DEBUG
1717 /* check current emissions */
1721 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1722 emission; emission = emission->next)
1723 if (emission->ihint.signal_id == node.signal_id)
1724 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1725 node.name, emission->instance);
1729 /* free contents that need to
1732 g_free (node.param_types);
1733 if (node.class_closure_bsa)
1737 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1739 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1741 g_closure_unref (cc->closure);
1743 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1745 g_free (node.accumulator);
1746 if (node.emission_hooks)
1748 g_hook_list_clear (node.emission_hooks);
1749 g_free (node.emission_hooks);
1755 * g_signal_override_class_closure:
1756 * @signal_id: the signal id
1757 * @instance_type: the instance type on which to override the class closure
1759 * @class_closure: the closure.
1761 * Overrides the class closure (i.e. the default handler) for the given signal
1762 * for emissions on instances of @instance_type. @instance_type must be derived
1763 * from the type to which the signal belongs.
1765 * See g_signal_chain_from_overridden() and
1766 * g_signal_chain_from_overridden_handler() for how to chain up to the
1767 * parent class closure from inside the overridden one.
1770 g_signal_override_class_closure (guint signal_id,
1771 GType instance_type,
1772 GClosure *class_closure)
1776 g_return_if_fail (signal_id > 0);
1777 g_return_if_fail (class_closure != NULL);
1780 node = LOOKUP_SIGNAL_NODE (signal_id);
1781 if (!g_type_is_a (instance_type, node->itype))
1782 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1785 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1787 if (cc && cc->instance_type == instance_type)
1788 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1790 signal_add_class_closure (node, instance_type, class_closure);
1796 * g_signal_override_class_handler:
1797 * @signal_name: the name for the signal
1798 * @instance_type: the instance type on which to override the class handler
1800 * @class_handler: the handler.
1802 * Overrides the class closure (i.e. the default handler) for the
1803 * given signal for emissions on instances of @instance_type with
1804 * callabck @class_handler. @instance_type must be derived from the
1805 * type to which the signal belongs.
1807 * See g_signal_chain_from_overridden() and
1808 * g_signal_chain_from_overridden_handler() for how to chain up to the
1809 * parent class closure from inside the overridden one.
1814 g_signal_override_class_handler (const gchar *signal_name,
1815 GType instance_type,
1816 GCallback class_handler)
1820 g_return_if_fail (signal_name != NULL);
1821 g_return_if_fail (instance_type != G_TYPE_NONE);
1822 g_return_if_fail (class_handler != NULL);
1824 signal_id = g_signal_lookup (signal_name, instance_type);
1827 g_signal_override_class_closure (signal_id, instance_type,
1828 g_cclosure_new (class_handler, NULL, NULL));
1830 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1831 G_STRLOC, signal_name, instance_type);
1836 * g_signal_chain_from_overridden:
1837 * @instance_and_params: the argument list of the signal emission. The first
1838 * element in the array is a #GValue for the instance the signal is being
1839 * emitted on. The rest are any arguments to be passed to the signal.
1840 * @return_value: Location for the return value.
1842 * Calls the original class closure of a signal. This function should only
1843 * be called from an overridden class closure; see
1844 * g_signal_override_class_closure() and
1845 * g_signal_override_class_handler().
1848 g_signal_chain_from_overridden (const GValue *instance_and_params,
1849 GValue *return_value)
1851 GType chain_type = 0, restore_type = 0;
1852 Emission *emission = NULL;
1853 GClosure *closure = NULL;
1857 g_return_if_fail (instance_and_params != NULL);
1858 instance = g_value_peek_pointer (instance_and_params);
1859 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1862 emission = emission_find_innermost (instance);
1865 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1867 g_assert (node != NULL); /* paranoid */
1869 /* we should probably do the same parameter checks as g_signal_emit() here.
1871 if (emission->chain_type != G_TYPE_NONE)
1873 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1875 g_assert (cc != NULL); /* closure currently in call stack */
1877 n_params = node->n_params;
1878 restore_type = cc->instance_type;
1879 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1880 if (cc && cc->instance_type != restore_type)
1882 closure = cc->closure;
1883 chain_type = cc->instance_type;
1887 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1890 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1894 emission->chain_type = chain_type;
1896 g_closure_invoke (closure,
1899 instance_and_params,
1902 emission->chain_type = restore_type;
1908 * g_signal_chain_from_overridden_handler:
1909 * @instance: the instance the signal is being emitted on.
1910 * @...: parameters to be passed to the parent class closure, followed by a
1911 * location for the return value. If the return type of the signal
1912 * is #G_TYPE_NONE, the return value location can be omitted.
1914 * Calls the original class closure of a signal. This function should
1915 * only be called from an overridden class closure; see
1916 * g_signal_override_class_closure() and
1917 * g_signal_override_class_handler().
1922 g_signal_chain_from_overridden_handler (gpointer instance,
1925 GType chain_type = 0, restore_type = 0;
1926 Emission *emission = NULL;
1927 GClosure *closure = NULL;
1931 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1934 emission = emission_find_innermost (instance);
1937 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1939 g_assert (node != NULL); /* paranoid */
1941 /* we should probably do the same parameter checks as g_signal_emit() here.
1943 if (emission->chain_type != G_TYPE_NONE)
1945 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1947 g_assert (cc != NULL); /* closure currently in call stack */
1949 n_params = node->n_params;
1950 restore_type = cc->instance_type;
1951 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1952 if (cc && cc->instance_type != restore_type)
1954 closure = cc->closure;
1955 chain_type = cc->instance_type;
1959 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1962 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1966 GValue *instance_and_params, *free_me = NULL;
1967 GType signal_return_type;
1968 GValue *param_values;
1972 va_start (var_args, instance);
1974 signal_return_type = node->return_type;
1975 free_me = g_new (GValue, node->n_params + 1);
1976 instance_and_params = free_me;
1977 param_values = instance_and_params + 1;
1979 for (i = 0; i < node->n_params; i++)
1982 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1983 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1985 param_values[i].g_type = 0;
1987 g_value_init (param_values + i, ptype);
1988 G_VALUE_COLLECT (param_values + i,
1990 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1994 g_warning ("%s: %s", G_STRLOC, error);
1997 /* we purposely leak the value here, it might not be
1998 * in a sane state if an error condition occoured
2001 g_value_unset (param_values + i);
2011 instance_and_params->g_type = 0;
2012 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2013 g_value_set_instance (instance_and_params, instance);
2016 emission->chain_type = chain_type;
2019 if (signal_return_type == G_TYPE_NONE)
2021 g_closure_invoke (closure,
2024 instance_and_params,
2029 GValue return_value = { 0, };
2030 gchar *error = NULL;
2031 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2032 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2034 g_value_init (&return_value, rtype);
2036 g_closure_invoke (closure,
2039 instance_and_params,
2042 G_VALUE_LCOPY (&return_value,
2044 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2048 g_value_unset (&return_value);
2052 g_warning ("%s: %s", G_STRLOC, error);
2055 /* we purposely leak the value here, it might not be
2056 * in a sane state if an error condition occured
2061 for (i = 0; i < n_params; i++)
2062 g_value_unset (param_values + i);
2063 g_value_unset (instance_and_params);
2070 emission->chain_type = restore_type;
2076 * g_signal_get_invocation_hint:
2077 * @instance: the instance to query
2079 * Returns the invocation hint of the innermost signal emission of instance.
2081 * Returns: the invocation hint of the innermost signal emission.
2083 GSignalInvocationHint*
2084 g_signal_get_invocation_hint (gpointer instance)
2086 Emission *emission = NULL;
2088 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2091 emission = emission_find_innermost (instance);
2094 return emission ? &emission->ihint : NULL;
2098 * g_signal_connect_closure_by_id:
2099 * @instance: the instance to connect to.
2100 * @signal_id: the id of the signal.
2101 * @detail: the detail.
2102 * @closure: the closure to connect.
2103 * @after: whether the handler should be called before or after the
2104 * default handler of the signal.
2106 * Connects a closure to a signal for a particular object.
2108 * Returns: the handler id
2111 g_signal_connect_closure_by_id (gpointer instance,
2118 gulong handler_seq_no = 0;
2120 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2121 g_return_val_if_fail (signal_id > 0, 0);
2122 g_return_val_if_fail (closure != NULL, 0);
2125 node = LOOKUP_SIGNAL_NODE (signal_id);
2128 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2129 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2130 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2131 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2134 Handler *handler = handler_new (after);
2136 handler_seq_no = handler->sequential_number;
2137 handler->detail = detail;
2138 handler->closure = g_closure_ref (closure);
2139 g_closure_sink (closure);
2140 handler_insert (signal_id, instance, handler);
2141 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2142 g_closure_set_marshal (closure, node->c_marshaller);
2146 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2149 return handler_seq_no;
2153 * g_signal_connect_closure:
2154 * @instance: the instance to connect to.
2155 * @detailed_signal: a string of the form "signal-name::detail".
2156 * @closure: the closure to connect.
2157 * @after: whether the handler should be called before or after the
2158 * default handler of the signal.
2160 * Connects a closure to a signal for a particular object.
2162 * Returns: the handler id
2165 g_signal_connect_closure (gpointer instance,
2166 const gchar *detailed_signal,
2171 gulong handler_seq_no = 0;
2175 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2176 g_return_val_if_fail (detailed_signal != NULL, 0);
2177 g_return_val_if_fail (closure != NULL, 0);
2180 itype = G_TYPE_FROM_INSTANCE (instance);
2181 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2184 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2186 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2187 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2188 else if (!g_type_is_a (itype, node->itype))
2189 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2192 Handler *handler = handler_new (after);
2194 handler_seq_no = handler->sequential_number;
2195 handler->detail = detail;
2196 handler->closure = g_closure_ref (closure);
2197 g_closure_sink (closure);
2198 handler_insert (signal_id, instance, handler);
2199 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2200 g_closure_set_marshal (handler->closure, node->c_marshaller);
2204 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2207 return handler_seq_no;
2211 * g_signal_connect_data:
2212 * @instance: the instance to connect to.
2213 * @detailed_signal: a string of the form "signal-name::detail".
2214 * @c_handler: the #GCallback to connect.
2215 * @data: data to pass to @c_handler calls.
2216 * @destroy_data: a #GClosureNotify for @data.
2217 * @connect_flags: a combination of #GConnectFlags.
2219 * Connects a #GCallback function to a signal for a particular object. Similar
2220 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2221 * which will be called when the signal handler is disconnected and no longer
2222 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
2223 * <literal>..._swapped()</literal> variants of this function.
2225 * Returns: the handler id
2228 g_signal_connect_data (gpointer instance,
2229 const gchar *detailed_signal,
2230 GCallback c_handler,
2232 GClosureNotify destroy_data,
2233 GConnectFlags connect_flags)
2236 gulong handler_seq_no = 0;
2239 gboolean swapped, after;
2241 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2242 g_return_val_if_fail (detailed_signal != NULL, 0);
2243 g_return_val_if_fail (c_handler != NULL, 0);
2245 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2246 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2249 itype = G_TYPE_FROM_INSTANCE (instance);
2250 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2253 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2255 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2256 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2257 else if (!g_type_is_a (itype, node->itype))
2258 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2261 Handler *handler = handler_new (after);
2263 handler_seq_no = handler->sequential_number;
2264 handler->detail = detail;
2265 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2266 g_closure_sink (handler->closure);
2267 handler_insert (signal_id, instance, handler);
2268 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2269 g_closure_set_marshal (handler->closure, node->c_marshaller);
2273 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2276 return handler_seq_no;
2280 * g_signal_handler_block:
2281 * @instance: The instance to block the signal handler of.
2282 * @handler_id: Handler id of the handler to be blocked.
2284 * Blocks a handler of an instance so it will not be called during any
2285 * signal emissions unless it is unblocked again. Thus "blocking" a
2286 * signal handler means to temporarily deactive it, a signal handler
2287 * has to be unblocked exactly the same amount of times it has been
2288 * blocked before to become active again.
2290 * The @handler_id has to be a valid signal handler id, connected to a
2291 * signal of @instance.
2294 g_signal_handler_block (gpointer instance,
2299 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2300 g_return_if_fail (handler_id > 0);
2303 handler = handler_lookup (instance, handler_id, NULL);
2306 #ifndef G_DISABLE_CHECKS
2307 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2308 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2310 handler->block_count += 1;
2313 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2318 * g_signal_handler_unblock:
2319 * @instance: The instance to unblock the signal handler of.
2320 * @handler_id: Handler id of the handler to be unblocked.
2322 * Undoes the effect of a previous g_signal_handler_block() call. A
2323 * blocked handler is skipped during signal emissions and will not be
2324 * invoked, unblocking it (for exactly the amount of times it has been
2325 * blocked before) reverts its "blocked" state, so the handler will be
2326 * recognized by the signal system and is called upon future or
2327 * currently ongoing signal emissions (since the order in which
2328 * handlers are called during signal emissions is deterministic,
2329 * whether the unblocked handler in question is called as part of a
2330 * currently ongoing emission depends on how far that emission has
2333 * The @handler_id has to be a valid id of a signal handler that is
2334 * connected to a signal of @instance and is currently blocked.
2337 g_signal_handler_unblock (gpointer instance,
2342 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2343 g_return_if_fail (handler_id > 0);
2346 handler = handler_lookup (instance, handler_id, NULL);
2349 if (handler->block_count)
2350 handler->block_count -= 1;
2352 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2355 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2360 * g_signal_handler_disconnect:
2361 * @instance: The instance to remove the signal handler from.
2362 * @handler_id: Handler id of the handler to be disconnected.
2364 * Disconnects a handler from an instance so it will not be called during
2365 * any future or currently ongoing emissions of the signal it has been
2366 * connected to. The @handler_id becomes invalid and may be reused.
2368 * The @handler_id has to be a valid signal handler id, connected to a
2369 * signal of @instance.
2372 g_signal_handler_disconnect (gpointer instance,
2378 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2379 g_return_if_fail (handler_id > 0);
2382 handler = handler_lookup (instance, handler_id, &signal_id);
2385 handler->sequential_number = 0;
2386 handler->block_count = 1;
2387 handler_unref_R (signal_id, instance, handler);
2390 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2395 * g_signal_handler_is_connected:
2396 * @instance: The instance where a signal handler is sought.
2397 * @handler_id: the handler id.
2399 * Returns whether @handler_id is the id of a handler connected to @instance.
2401 * Returns: whether @handler_id identifies a handler connected to @instance.
2404 g_signal_handler_is_connected (gpointer instance,
2410 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2413 handler = handler_lookup (instance, handler_id, NULL);
2414 connected = handler != NULL;
2421 g_signal_handlers_destroy (gpointer instance)
2423 GBSearchArray *hlbsa;
2425 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2428 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2433 /* reentrancy caution, delete instance trace first */
2434 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2436 for (i = 0; i < hlbsa->n_nodes; i++)
2438 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2439 Handler *handler = hlist->handlers;
2443 Handler *tmp = handler;
2445 handler = tmp->next;
2446 tmp->block_count = 1;
2447 /* cruel unlink, this works because _all_ handlers vanish */
2450 if (tmp->sequential_number)
2452 tmp->sequential_number = 0;
2453 handler_unref_R (0, NULL, tmp);
2457 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2463 * g_signal_handler_find:
2464 * @instance: The instance owning the signal handler to be found.
2465 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2466 * and/or @data the handler has to match.
2467 * @signal_id: Signal the handler has to be connected to.
2468 * @detail: Signal detail the handler has to be connected to.
2469 * @closure: The closure the handler will invoke.
2470 * @func: The C closure callback of the handler (useless for non-C closures).
2471 * @data: The closure data of the handler's closure.
2473 * Finds the first signal handler that matches certain selection criteria.
2474 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2475 * flags, and the criteria values are passed as arguments.
2476 * The match @mask has to be non-0 for successful matches.
2477 * If no handler was found, 0 is returned.
2479 * Returns: A valid non-0 signal handler id for a successful match.
2482 g_signal_handler_find (gpointer instance,
2483 GSignalMatchType mask,
2490 gulong handler_seq_no = 0;
2492 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2493 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2495 if (mask & G_SIGNAL_MATCH_MASK)
2497 HandlerMatch *mlist;
2500 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2503 handler_seq_no = mlist->handler->sequential_number;
2504 handler_match_free1_R (mlist, instance);
2509 return handler_seq_no;
2513 signal_handlers_foreach_matched_R (gpointer instance,
2514 GSignalMatchType mask,
2520 void (*callback) (gpointer instance,
2521 gulong handler_seq_no))
2523 HandlerMatch *mlist;
2524 guint n_handlers = 0;
2526 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2530 if (mlist->handler->sequential_number)
2533 callback (instance, mlist->handler->sequential_number);
2536 mlist = handler_match_free1_R (mlist, instance);
2543 * g_signal_handlers_block_matched:
2544 * @instance: The instance to block handlers from.
2545 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2546 * and/or @data the handlers have to match.
2547 * @signal_id: Signal the handlers have to be connected to.
2548 * @detail: Signal detail the handlers have to be connected to.
2549 * @closure: The closure the handlers will invoke.
2550 * @func: The C closure callback of the handlers (useless for non-C closures).
2551 * @data: The closure data of the handlers' closures.
2553 * Blocks all handlers on an instance that match a certain selection criteria.
2554 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2555 * flags, and the criteria values are passed as arguments.
2556 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2557 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2558 * If no handlers were found, 0 is returned, the number of blocked handlers
2561 * Returns: The number of handlers that matched.
2564 g_signal_handlers_block_matched (gpointer instance,
2565 GSignalMatchType mask,
2572 guint n_handlers = 0;
2574 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2575 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2577 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2580 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2581 closure, func, data,
2582 g_signal_handler_block);
2590 * g_signal_handlers_unblock_matched:
2591 * @instance: The instance to unblock handlers from.
2592 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2593 * and/or @data the handlers have to match.
2594 * @signal_id: Signal the handlers have to be connected to.
2595 * @detail: Signal detail the handlers have to be connected to.
2596 * @closure: The closure the handlers will invoke.
2597 * @func: The C closure callback of the handlers (useless for non-C closures).
2598 * @data: The closure data of the handlers' closures.
2600 * Unblocks all handlers on an instance that match a certain selection
2601 * criteria. The criteria mask is passed as an OR-ed combination of
2602 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2603 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2604 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2605 * If no handlers were found, 0 is returned, the number of unblocked handlers
2606 * otherwise. The match criteria should not apply to any handlers that are
2607 * not currently blocked.
2609 * Returns: The number of handlers that matched.
2612 g_signal_handlers_unblock_matched (gpointer instance,
2613 GSignalMatchType mask,
2620 guint n_handlers = 0;
2622 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2623 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2625 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2628 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2629 closure, func, data,
2630 g_signal_handler_unblock);
2638 * g_signal_handlers_disconnect_matched:
2639 * @instance: The instance to remove handlers from.
2640 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2641 * and/or @data the handlers have to match.
2642 * @signal_id: Signal the handlers have to be connected to.
2643 * @detail: Signal detail the handlers have to be connected to.
2644 * @closure: The closure the handlers will invoke.
2645 * @func: The C closure callback of the handlers (useless for non-C closures).
2646 * @data: The closure data of the handlers' closures.
2648 * Disconnects all handlers on an instance that match a certain
2649 * selection criteria. The criteria mask is passed as an OR-ed
2650 * combination of #GSignalMatchType flags, and the criteria values are
2651 * passed as arguments. Passing at least one of the
2652 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2653 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2654 * matches. If no handlers were found, 0 is returned, the number of
2655 * disconnected handlers otherwise.
2657 * Returns: The number of handlers that matched.
2660 g_signal_handlers_disconnect_matched (gpointer instance,
2661 GSignalMatchType mask,
2668 guint n_handlers = 0;
2670 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2671 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2673 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2676 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2677 closure, func, data,
2678 g_signal_handler_disconnect);
2686 * g_signal_has_handler_pending:
2687 * @instance: the object whose signal handlers are sought.
2688 * @signal_id: the signal id.
2689 * @detail: the detail.
2690 * @may_be_blocked: whether blocked handlers should count as match.
2692 * Returns whether there are any handlers connected to @instance for the
2693 * given signal id and detail.
2695 * One example of when you might use this is when the arguments to the
2696 * signal are difficult to compute. A class implementor may opt to not
2697 * emit the signal if no one is attached anyway, thus saving the cost
2698 * of building the arguments.
2700 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2704 g_signal_has_handler_pending (gpointer instance,
2707 gboolean may_be_blocked)
2709 HandlerMatch *mlist;
2710 gboolean has_pending;
2712 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2713 g_return_val_if_fail (signal_id > 0, FALSE);
2718 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2720 if (!(node->flags & G_SIGNAL_DETAILED))
2722 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2727 mlist = handlers_find (instance,
2728 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2729 signal_id, detail, NULL, NULL, NULL, TRUE);
2733 handler_match_free1_R (mlist, instance);
2736 has_pending = FALSE;
2742 static inline gboolean
2743 signal_check_skip_emission (SignalNode *node,
2749 /* are we able to check for NULL class handlers? */
2750 if (!node->test_class_offset)
2753 /* are there emission hooks pending? */
2754 if (node->emission_hooks && node->emission_hooks->hooks)
2757 /* is there a non-NULL class handler? */
2758 if (node->test_class_offset != TEST_CLASS_MAGIC)
2760 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2762 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2766 /* are signals being debugged? */
2767 #ifdef G_ENABLE_DEBUG
2768 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2770 #endif /* G_ENABLE_DEBUG */
2772 /* is this a no-recurse signal already in emission? */
2773 if (node->flags & G_SIGNAL_NO_RECURSE &&
2774 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2777 /* do we have pending handlers? */
2778 hlist = handler_list_lookup (node->signal_id, instance);
2779 if (hlist && hlist->handlers)
2782 /* none of the above, no emission required */
2788 * @instance_and_params: argument list for the signal emission. The first
2789 * element in the array is a #GValue for the instance the signal is
2790 * being emitted on. The rest are any arguments to be passed to the
2792 * @signal_id: the signal id
2793 * @detail: the detail
2794 * @return_value: Location to store the return value of the signal emission.
2798 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2799 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2802 g_signal_emitv (const GValue *instance_and_params,
2805 GValue *return_value)
2809 #ifdef G_ENABLE_DEBUG
2810 const GValue *param_values;
2814 g_return_if_fail (instance_and_params != NULL);
2815 instance = g_value_peek_pointer (instance_and_params);
2816 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2817 g_return_if_fail (signal_id > 0);
2819 #ifdef G_ENABLE_DEBUG
2820 param_values = instance_and_params + 1;
2824 node = LOOKUP_SIGNAL_NODE (signal_id);
2825 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2827 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2831 #ifdef G_ENABLE_DEBUG
2832 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2834 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2838 for (i = 0; i < node->n_params; i++)
2839 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2841 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2843 type_debug_name (node->param_types[i]),
2846 G_VALUE_TYPE_NAME (param_values + i));
2850 if (node->return_type != G_TYPE_NONE)
2854 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2856 type_debug_name (node->return_type),
2861 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2863 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2865 type_debug_name (node->return_type),
2867 G_VALUE_TYPE_NAME (return_value));
2873 return_value = NULL;
2874 #endif /* G_ENABLE_DEBUG */
2876 /* optimize NOP emissions */
2877 if (signal_check_skip_emission (node, instance, detail))
2879 /* nothing to do to emit this signal */
2881 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2886 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2890 * g_signal_emit_valist:
2891 * @instance: the instance the signal is being emitted on.
2892 * @signal_id: the signal id
2893 * @detail: the detail
2894 * @var_args: a list of parameters to be passed to the signal, followed by a
2895 * location for the return value. If the return type of the signal
2896 * is #G_TYPE_NONE, the return value location can be omitted.
2900 * Note that g_signal_emit_valist() resets the return value to the default
2901 * if no handlers are connected, in contrast to g_signal_emitv().
2904 g_signal_emit_valist (gpointer instance,
2909 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2910 GType signal_return_type;
2911 GValue *param_values;
2915 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2916 g_return_if_fail (signal_id > 0);
2919 node = LOOKUP_SIGNAL_NODE (signal_id);
2920 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2922 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2926 #ifndef G_DISABLE_CHECKS
2927 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2929 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2933 #endif /* !G_DISABLE_CHECKS */
2935 /* optimize NOP emissions */
2936 if (signal_check_skip_emission (node, instance, detail))
2938 /* nothing to do to emit this signal */
2940 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2944 n_params = node->n_params;
2945 signal_return_type = node->return_type;
2946 if (node->n_params < MAX_STACK_VALUES)
2947 instance_and_params = stack_values;
2950 free_me = g_new (GValue, node->n_params + 1);
2951 instance_and_params = free_me;
2953 param_values = instance_and_params + 1;
2954 for (i = 0; i < node->n_params; i++)
2957 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2958 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2960 param_values[i].g_type = 0;
2962 g_value_init (param_values + i, ptype);
2963 G_VALUE_COLLECT (param_values + i,
2965 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2969 g_warning ("%s: %s", G_STRLOC, error);
2972 /* we purposely leak the value here, it might not be
2973 * in a sane state if an error condition occoured
2976 g_value_unset (param_values + i);
2984 instance_and_params->g_type = 0;
2985 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2986 g_value_set_instance (instance_and_params, instance);
2987 if (signal_return_type == G_TYPE_NONE)
2988 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2991 GValue return_value = { 0, };
2992 gchar *error = NULL;
2993 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2994 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2996 g_value_init (&return_value, rtype);
2998 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3000 G_VALUE_LCOPY (&return_value,
3002 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3005 g_value_unset (&return_value);
3008 g_warning ("%s: %s", G_STRLOC, error);
3011 /* we purposely leak the value here, it might not be
3012 * in a sane state if an error condition occured
3016 for (i = 0; i < n_params; i++)
3017 g_value_unset (param_values + i);
3018 g_value_unset (instance_and_params);
3025 * @instance: the instance the signal is being emitted on.
3026 * @signal_id: the signal id
3027 * @detail: the detail
3028 * @...: parameters to be passed to the signal, followed by a
3029 * location for the return value. If the return type of the signal
3030 * is #G_TYPE_NONE, the return value location can be omitted.
3034 * Note that g_signal_emit() resets the return value to the default
3035 * if no handlers are connected, in contrast to g_signal_emitv().
3038 g_signal_emit (gpointer instance,
3045 va_start (var_args, detail);
3046 g_signal_emit_valist (instance, signal_id, detail, var_args);
3051 * g_signal_emit_by_name:
3052 * @instance: the instance the signal is being emitted on.
3053 * @detailed_signal: a string of the form "signal-name::detail".
3054 * @...: parameters to be passed to the signal, followed by a
3055 * location for the return value. If the return type of the signal
3056 * is #G_TYPE_NONE, the return value location can be omitted.
3060 * Note that g_signal_emit_by_name() resets the return value to the default
3061 * if no handlers are connected, in contrast to g_signal_emitv().
3064 g_signal_emit_by_name (gpointer instance,
3065 const gchar *detailed_signal,
3071 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3072 g_return_if_fail (detailed_signal != NULL);
3075 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
3082 va_start (var_args, detailed_signal);
3083 g_signal_emit_valist (instance, signal_id, detail, var_args);
3087 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
3090 static inline gboolean
3091 accumulate (GSignalInvocationHint *ihint,
3092 GValue *return_accu,
3093 GValue *handler_return,
3094 SignalAccumulator *accumulator)
3096 gboolean continue_emission;
3101 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3102 g_value_reset (handler_return);
3104 return continue_emission;
3108 signal_emit_unlocked_R (SignalNode *node,
3111 GValue *emission_return,
3112 const GValue *instance_and_params)
3114 SignalAccumulator *accumulator;
3116 GClosure *class_closure;
3118 Handler *handler_list = NULL;
3119 GValue *return_accu, accu = { 0, };
3121 gulong max_sequential_handler_number;
3122 gboolean return_value_altered = FALSE;
3124 #ifdef G_ENABLE_DEBUG
3125 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
3127 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
3128 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
3131 if (g_trap_instance_signals == instance)
3134 #endif /* G_ENABLE_DEBUG */
3137 signal_id = node->signal_id;
3138 if (node->flags & G_SIGNAL_NO_RECURSE)
3140 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3144 node->state = EMISSION_RESTART;
3146 return return_value_altered;
3149 accumulator = node->accumulator;
3153 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3154 return_accu = &accu;
3158 return_accu = emission_return;
3159 emission.instance = instance;
3160 emission.ihint.signal_id = node->signal_id;
3161 emission.ihint.detail = detail;
3162 emission.ihint.run_type = 0;
3164 emission.chain_type = G_TYPE_NONE;
3165 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3166 class_closure = signal_lookup_closure (node, instance);
3171 handler_unref_R (signal_id, instance, handler_list);
3172 max_sequential_handler_number = g_handler_sequential_number;
3173 hlist = handler_list_lookup (signal_id, instance);
3174 handler_list = hlist ? hlist->handlers : NULL;
3176 handler_ref (handler_list);
3178 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3180 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3182 emission.state = EMISSION_RUN;
3184 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3186 g_closure_invoke (class_closure,
3189 instance_and_params,
3191 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3192 emission.state == EMISSION_RUN)
3193 emission.state = EMISSION_STOP;
3195 emission.chain_type = G_TYPE_NONE;
3196 return_value_altered = TRUE;
3198 if (emission.state == EMISSION_STOP)
3200 else if (emission.state == EMISSION_RESTART)
3204 if (node->emission_hooks)
3206 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3209 emission.state = EMISSION_HOOK;
3210 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3213 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3215 if (!signal_hook->detail || signal_hook->detail == detail)
3217 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3219 was_in_call = G_HOOK_IN_CALL (hook);
3220 hook->flags |= G_HOOK_FLAG_IN_CALL;
3222 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3225 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3227 g_hook_destroy_link (node->emission_hooks, hook);
3229 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3232 if (emission.state == EMISSION_RESTART)
3238 Handler *handler = handler_list;
3240 emission.state = EMISSION_RUN;
3241 handler_ref (handler);
3248 handler_unref_R (signal_id, instance, handler_list);
3249 handler_list = handler;
3252 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3253 handler->sequential_number < max_sequential_handler_number)
3256 g_closure_invoke (handler->closure,
3259 instance_and_params,
3261 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3262 emission.state == EMISSION_RUN)
3263 emission.state = EMISSION_STOP;
3265 return_value_altered = TRUE;
3267 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3270 tmp = handler->next;
3274 handler_unref_R (signal_id, instance, handler_list);
3275 handler_list = handler;
3280 if (emission.state == EMISSION_STOP)
3282 else if (emission.state == EMISSION_RESTART)
3286 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3288 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3290 emission.state = EMISSION_RUN;
3292 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3294 g_closure_invoke (class_closure,
3297 instance_and_params,
3299 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3300 emission.state == EMISSION_RUN)
3301 emission.state = EMISSION_STOP;
3303 emission.chain_type = G_TYPE_NONE;
3304 return_value_altered = TRUE;
3306 if (emission.state == EMISSION_STOP)
3308 else if (emission.state == EMISSION_RESTART)
3314 Handler *handler = handler_list;
3316 emission.state = EMISSION_RUN;
3317 handler_ref (handler);
3322 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3323 handler->sequential_number < max_sequential_handler_number)
3326 g_closure_invoke (handler->closure,
3329 instance_and_params,
3331 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3332 emission.state == EMISSION_RUN)
3333 emission.state = EMISSION_STOP;
3335 return_value_altered = TRUE;
3337 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3340 tmp = handler->next;
3344 handler_unref_R (signal_id, instance, handler);
3349 if (emission.state == EMISSION_STOP)
3351 else if (emission.state == EMISSION_RESTART)
3357 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3359 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3361 gboolean need_unset = FALSE;
3363 emission.state = EMISSION_STOP;
3365 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3367 if (node->return_type != G_TYPE_NONE && !accumulator)
3369 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3372 g_closure_invoke (class_closure,
3373 node->return_type != G_TYPE_NONE ? &accu : NULL,
3375 instance_and_params,
3378 g_value_unset (&accu);
3380 emission.chain_type = G_TYPE_NONE;
3382 if (emission.state == EMISSION_RESTART)
3387 handler_unref_R (signal_id, instance, handler_list);
3389 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3392 g_value_unset (&accu);
3394 return return_value_altered;
3398 type_debug_name (GType type)
3402 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3403 return name ? name : "<unknown>";
3410 * g_signal_accumulator_true_handled:
3411 * @ihint: standard #GSignalAccumulator parameter
3412 * @return_accu: standard #GSignalAccumulator parameter
3413 * @handler_return: standard #GSignalAccumulator parameter
3414 * @dummy: standard #GSignalAccumulator parameter
3416 * A predefined #GSignalAccumulator for signals that return a
3417 * boolean values. The behavior that this accumulator gives is
3418 * that a return of %TRUE stops the signal emission: no further
3419 * callbacks will be invoked, while a return of %FALSE allows
3420 * the emission to coninue. The idea here is that a %TRUE return
3421 * indicates that the callback <emphasis>handled</emphasis> the signal,
3422 * and no further handling is needed.
3426 * Returns: standard #GSignalAccumulator result
3429 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3430 GValue *return_accu,
3431 const GValue *handler_return,
3434 gboolean continue_emission;
3435 gboolean signal_handled;
3437 signal_handled = g_value_get_boolean (handler_return);
3438 g_value_set_boolean (return_accu, signal_handled);
3439 continue_emission = !signal_handled;
3441 return continue_emission;
3444 /* --- compile standard marshallers --- */
3445 #include "gmarshal.c"
3447 #define __G_SIGNAL_C__
3448 #include "gobjectaliasdef.c"