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 "gtype-private.h"
34 #include "gbsearcharray.h"
35 #include "gvaluecollector.h"
36 #include "gvaluetypes.h"
39 #include "gobject_trace.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 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
108 #ifdef G_ENABLE_DEBUG
109 #define COND_DEBUG(debug_type, cond) ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || (cond))
110 #define IF_DEBUG(debug_type, cond) if (COND_DEBUG(debug_type, cond))
112 static volatile gpointer g_trace_instance_signals = NULL;
113 static volatile gpointer g_trap_instance_signals = NULL;
114 #endif /* G_ENABLE_DEBUG */
117 /* --- typedefs --- */
118 typedef struct _SignalNode SignalNode;
119 typedef struct _SignalKey SignalKey;
120 typedef struct _Emission Emission;
121 typedef struct _Handler Handler;
122 typedef struct _HandlerList HandlerList;
123 typedef struct _HandlerMatch HandlerMatch;
133 /* --- prototypes --- */
134 static inline guint signal_id_lookup (GQuark quark,
136 static void signal_destroy_R (SignalNode *signal_node);
137 static inline HandlerList* handler_list_ensure (guint signal_id,
139 static inline HandlerList* handler_list_lookup (guint signal_id,
141 static inline Handler* handler_new (gboolean after);
142 static void handler_insert (guint signal_id,
145 static Handler* handler_lookup (gpointer instance,
149 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
152 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
154 static HandlerMatch* handlers_find (gpointer instance,
155 GSignalMatchType mask,
161 gboolean one_and_only);
162 static inline void handler_ref (Handler *handler);
163 static inline void handler_unref_R (guint signal_id,
166 static gint handler_lists_cmp (gconstpointer node1,
167 gconstpointer node2);
168 static inline void emission_push (Emission **emission_list_p,
170 static inline void emission_pop (Emission **emission_list_p,
172 static inline Emission* emission_find (Emission *emission_list,
176 static gint class_closures_cmp (gconstpointer node1,
177 gconstpointer node2);
178 static gint signal_key_cmp (gconstpointer node1,
179 gconstpointer node2);
180 static gboolean signal_emit_unlocked_R (SignalNode *node,
183 GValue *return_value,
184 const GValue *instance_and_params);
185 static void add_invalid_closure_notify (Handler *handler,
187 static void remove_invalid_closure_notify (Handler *handler,
189 static void invalid_closure_notify (gpointer data,
191 static const gchar * type_debug_name (GType type);
192 static void node_check_deprecated (const SignalNode *node);
193 static void node_update_single_va_closure (SignalNode *node);
196 /* --- structures --- */
199 GSignalAccumulator func;
207 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
211 /* permanent portion */
217 /* reinitializable portion */
220 guint single_va_closure_is_valid : 1;
221 guint single_va_closure_is_after : 1;
222 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
223 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
224 GBSearchArray *class_closure_bsa;
225 SignalAccumulator *accumulator;
226 GSignalCMarshaller c_marshaller;
227 GSignalCVaMarshaller va_marshaller;
228 GHookList *emission_hooks;
230 GClosure *single_va_closure;
233 #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1) /* indicates single_va_closure is valid but empty */
246 GSignalInvocationHint ihint;
255 Handler *tail_before; /* normal signal handlers are appended here */
256 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
261 gulong sequential_number;
266 guint block_count : 16;
267 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
269 guint has_invalid_closure_notify : 1;
281 GType instance_type; /* 0 for default closure */
286 /* --- variables --- */
287 static GBSearchArray *g_signal_key_bsa = NULL;
288 static const GBSearchConfig g_signal_key_bconfig = {
291 G_BSEARCH_ARRAY_ALIGN_POWER2,
293 static GBSearchConfig g_signal_hlbsa_bconfig = {
294 sizeof (HandlerList),
298 static GBSearchConfig g_class_closure_bconfig = {
299 sizeof (ClassClosure),
303 static GHashTable *g_handler_list_bsa_ht = NULL;
304 static Emission *g_recursive_emissions = NULL;
305 static Emission *g_restart_emissions = NULL;
306 static gulong g_handler_sequential_number = 1;
307 G_LOCK_DEFINE_STATIC (g_signal_mutex);
308 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
309 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
312 /* --- signal nodes --- */
313 static guint g_n_signal_nodes = 0;
314 static SignalNode **g_signal_nodes = NULL;
316 static inline SignalNode*
317 LOOKUP_SIGNAL_NODE (register guint signal_id)
319 if (signal_id < g_n_signal_nodes)
320 return g_signal_nodes[signal_id];
326 /* --- functions --- */
328 signal_id_lookup (GQuark quark,
331 GType *ifaces, type = itype;
337 /* try looking up signals for this type and its ancestors */
340 SignalKey *signal_key;
343 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
346 return signal_key->signal_id;
348 type = g_type_parent (type);
352 /* no luck, try interfaces it exports */
353 ifaces = g_type_interfaces (itype, &n_ifaces);
356 SignalKey *signal_key;
358 key.itype = ifaces[n_ifaces];
359 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
364 return signal_key->signal_id;
373 class_closures_cmp (gconstpointer node1,
376 const ClassClosure *c1 = node1, *c2 = node2;
378 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
382 handler_lists_cmp (gconstpointer node1,
385 const HandlerList *hlist1 = node1, *hlist2 = node2;
387 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
390 static inline HandlerList*
391 handler_list_ensure (guint signal_id,
394 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
397 key.signal_id = signal_id;
399 key.tail_before = NULL;
400 key.tail_after = NULL;
403 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
404 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
405 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
409 GBSearchArray *o = hlbsa;
411 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
413 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
415 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
418 static inline HandlerList*
419 handler_list_lookup (guint signal_id,
422 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
425 key.signal_id = signal_id;
427 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
431 handler_lookup (gpointer instance,
436 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
442 for (i = 0; i < hlbsa->n_nodes; i++)
444 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
447 for (handler = hlist->handlers; handler; handler = handler->next)
448 if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id))
451 *signal_id_p = hlist->signal_id;
461 static inline HandlerMatch*
462 handler_match_prepend (HandlerMatch *list,
468 node = g_slice_new (HandlerMatch);
469 node->handler = handler;
471 node->signal_id = signal_id;
472 handler_ref (handler);
476 static inline HandlerMatch*
477 handler_match_free1_R (HandlerMatch *node,
480 HandlerMatch *next = node->next;
482 handler_unref_R (node->signal_id, instance, node->handler);
483 g_slice_free (HandlerMatch, node);
489 handlers_find (gpointer instance,
490 GSignalMatchType mask,
496 gboolean one_and_only)
498 HandlerMatch *mlist = NULL;
500 if (mask & G_SIGNAL_MATCH_ID)
502 HandlerList *hlist = handler_list_lookup (signal_id, instance);
504 SignalNode *node = NULL;
506 if (mask & G_SIGNAL_MATCH_FUNC)
508 node = LOOKUP_SIGNAL_NODE (signal_id);
509 if (!node || !node->c_marshaller)
514 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
515 if (handler->sequential_number &&
516 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
517 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
518 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
519 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
520 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
521 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
522 ((GCClosure*) handler->closure)->callback == func)))
524 mlist = handler_match_prepend (mlist, handler, signal_id);
531 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
538 for (i = 0; i < hlbsa->n_nodes; i++)
540 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
541 SignalNode *node = NULL;
544 if (!(mask & G_SIGNAL_MATCH_FUNC))
546 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
547 if (!node->c_marshaller)
551 for (handler = hlist->handlers; handler; handler = handler->next)
552 if (handler->sequential_number &&
553 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
554 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
555 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
556 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
557 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
558 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
559 ((GCClosure*) handler->closure)->callback == func)))
561 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
572 static inline Handler*
573 handler_new (gboolean after)
575 Handler *handler = g_slice_new (Handler);
576 #ifndef G_DISABLE_CHECKS
577 if (g_handler_sequential_number < 1)
578 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
581 handler->sequential_number = g_handler_sequential_number++;
582 handler->prev = NULL;
583 handler->next = NULL;
585 handler->ref_count = 1;
586 handler->block_count = 0;
587 handler->after = after != FALSE;
588 handler->closure = NULL;
589 handler->has_invalid_closure_notify = 0;
595 handler_ref (Handler *handler)
597 g_return_if_fail (handler->ref_count > 0);
599 handler->ref_count++;
603 handler_unref_R (guint signal_id,
607 g_return_if_fail (handler->ref_count > 0);
609 handler->ref_count--;
611 if (G_UNLIKELY (handler->ref_count == 0))
613 HandlerList *hlist = NULL;
616 handler->next->prev = handler->prev;
617 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
618 handler->prev->next = handler->next;
621 hlist = handler_list_lookup (signal_id, instance);
622 hlist->handlers = handler->next;
627 /* check if we are removing the handler pointed to by tail_before */
628 if (!handler->after && (!handler->next || handler->next->after))
631 hlist = handler_list_lookup (signal_id, instance);
634 g_assert (hlist->tail_before == handler); /* paranoid */
635 hlist->tail_before = handler->prev;
639 /* check if we are removing the handler pointed to by tail_after */
643 hlist = handler_list_lookup (signal_id, instance);
646 g_assert (hlist->tail_after == handler); /* paranoid */
647 hlist->tail_after = handler->prev;
653 g_closure_unref (handler->closure);
655 g_slice_free (Handler, handler);
660 handler_insert (guint signal_id,
666 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
668 hlist = handler_list_ensure (signal_id, instance);
669 if (!hlist->handlers)
671 hlist->handlers = handler;
673 hlist->tail_before = handler;
675 else if (handler->after)
677 handler->prev = hlist->tail_after;
678 hlist->tail_after->next = handler;
682 if (hlist->tail_before)
684 handler->next = hlist->tail_before->next;
686 handler->next->prev = handler;
687 handler->prev = hlist->tail_before;
688 hlist->tail_before->next = handler;
690 else /* insert !after handler into a list of only after handlers */
692 handler->next = hlist->handlers;
694 handler->next->prev = handler;
695 hlist->handlers = handler;
697 hlist->tail_before = handler;
701 hlist->tail_after = handler;
705 node_update_single_va_closure (SignalNode *node)
707 GClosure *closure = NULL;
708 gboolean is_after = FALSE;
710 /* Fast path single-handler without boxing the arguments in GValues */
711 if (G_TYPE_IS_OBJECT (node->itype) &&
712 (node->flags & (G_SIGNAL_MUST_COLLECT)) == 0 &&
713 (node->emission_hooks == NULL || node->emission_hooks->hooks == NULL))
715 GSignalFlags run_type;
717 GBSearchArray *bsa = node->class_closure_bsa;
719 if (bsa == NULL || bsa->n_nodes == 0)
720 closure = SINGLE_VA_CLOSURE_EMPTY_MAGIC;
721 else if (bsa->n_nodes == 1)
723 /* Look for default class closure (can't support non-default as it
724 chains up using GValues */
725 cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
726 if (cc->instance_type == 0)
728 run_type = node->flags & (G_SIGNAL_RUN_FIRST|G_SIGNAL_RUN_LAST|G_SIGNAL_RUN_CLEANUP);
729 /* Only support *one* of run-first or run-last, not multiple or cleanup */
730 if (run_type == G_SIGNAL_RUN_FIRST ||
731 run_type == G_SIGNAL_RUN_LAST)
733 closure = cc->closure;
734 is_after = (run_type == G_SIGNAL_RUN_LAST);
740 node->single_va_closure_is_valid = TRUE;
741 node->single_va_closure = closure;
742 node->single_va_closure_is_after = is_after;
746 emission_push (Emission **emission_list_p,
749 emission->next = *emission_list_p;
750 *emission_list_p = emission;
754 emission_pop (Emission **emission_list_p,
757 Emission *node, *last = NULL;
759 for (node = *emission_list_p; node; last = node, node = last->next)
760 if (node == emission)
763 last->next = node->next;
765 *emission_list_p = node->next;
768 g_assert_not_reached ();
771 static inline Emission*
772 emission_find (Emission *emission_list,
779 for (emission = emission_list; emission; emission = emission->next)
780 if (emission->instance == instance &&
781 emission->ihint.signal_id == signal_id &&
782 emission->ihint.detail == detail)
787 static inline Emission*
788 emission_find_innermost (gpointer instance)
790 Emission *emission, *s = NULL, *c = NULL;
792 for (emission = g_restart_emissions; emission; emission = emission->next)
793 if (emission->instance == instance)
798 for (emission = g_recursive_emissions; emission; emission = emission->next)
799 if (emission->instance == instance)
809 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
813 signal_key_cmp (gconstpointer node1,
816 const SignalKey *key1 = node1, *key2 = node2;
818 if (key1->itype == key2->itype)
819 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
821 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
825 _g_signal_init (void)
828 if (!g_n_signal_nodes)
830 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
831 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
832 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
834 /* invalid (0) signal_id */
835 g_n_signal_nodes = 1;
836 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
837 g_signal_nodes[0] = NULL;
843 _g_signals_destroy (GType itype)
848 for (i = 1; i < g_n_signal_nodes; i++)
850 SignalNode *node = g_signal_nodes[i];
852 if (node->itype == itype)
855 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
857 type_debug_name (node->itype));
859 signal_destroy_R (node);
866 * g_signal_stop_emission:
867 * @instance: the object whose signal handlers you wish to stop.
868 * @signal_id: the signal identifier, as returned by g_signal_lookup().
869 * @detail: the detail which the signal was emitted with.
871 * Stops a signal's current emission.
873 * This will prevent the default method from running, if the signal was
874 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
877 * Prints a warning if used on a signal which isn't being emitted.
880 g_signal_stop_emission (gpointer instance,
886 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
887 g_return_if_fail (signal_id > 0);
890 node = LOOKUP_SIGNAL_NODE (signal_id);
891 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
893 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
897 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
899 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
900 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
904 if (emission->state == EMISSION_HOOK)
905 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
906 node->name, instance);
907 else if (emission->state == EMISSION_RUN)
908 emission->state = EMISSION_STOP;
911 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
912 node->name, instance);
915 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
920 signal_finalize_hook (GHookList *hook_list,
923 GDestroyNotify destroy = hook->destroy;
927 hook->destroy = NULL;
929 destroy (hook->data);
935 * g_signal_add_emission_hook:
936 * @signal_id: the signal identifier, as returned by g_signal_lookup().
937 * @detail: the detail on which to call the hook.
938 * @hook_func: a #GSignalEmissionHook function.
939 * @hook_data: user data for @hook_func.
940 * @data_destroy: a #GDestroyNotify for @hook_data.
942 * Adds an emission hook for a signal, which will get called for any emission
943 * of that signal, independent of the instance. This is possible only
944 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
946 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
949 g_signal_add_emission_hook (guint signal_id,
951 GSignalEmissionHook hook_func,
953 GDestroyNotify data_destroy)
955 static gulong seq_hook_id = 1;
958 SignalHook *signal_hook;
960 g_return_val_if_fail (signal_id > 0, 0);
961 g_return_val_if_fail (hook_func != NULL, 0);
964 node = LOOKUP_SIGNAL_NODE (signal_id);
965 if (!node || node->destroyed)
967 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
971 if (node->flags & G_SIGNAL_NO_HOOKS)
973 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
977 if (detail && !(node->flags & G_SIGNAL_DETAILED))
979 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
983 node->single_va_closure_is_valid = FALSE;
984 if (!node->emission_hooks)
986 node->emission_hooks = g_new (GHookList, 1);
987 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
988 node->emission_hooks->finalize_hook = signal_finalize_hook;
991 node_check_deprecated (node);
993 hook = g_hook_alloc (node->emission_hooks);
994 hook->data = hook_data;
995 hook->func = (gpointer) hook_func;
996 hook->destroy = data_destroy;
997 signal_hook = SIGNAL_HOOK (hook);
998 signal_hook->detail = detail;
999 node->emission_hooks->seq_id = seq_hook_id;
1000 g_hook_append (node->emission_hooks, hook);
1001 seq_hook_id = node->emission_hooks->seq_id;
1005 return hook->hook_id;
1009 * g_signal_remove_emission_hook:
1010 * @signal_id: the id of the signal
1011 * @hook_id: the id of the emission hook, as returned by
1012 * g_signal_add_emission_hook()
1014 * Deletes an emission hook.
1017 g_signal_remove_emission_hook (guint signal_id,
1022 g_return_if_fail (signal_id > 0);
1023 g_return_if_fail (hook_id > 0);
1026 node = LOOKUP_SIGNAL_NODE (signal_id);
1027 if (!node || node->destroyed)
1029 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
1032 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
1033 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
1035 node->single_va_closure_is_valid = FALSE;
1042 signal_parse_name (const gchar *name,
1045 gboolean force_quark)
1047 const gchar *colon = strchr (name, ':');
1052 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1053 if (signal_id && detail_p)
1056 else if (colon[1] == ':')
1059 guint l = colon - name;
1063 memcpy (buffer, name, l);
1065 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1069 gchar *signal = g_new (gchar, l + 1);
1071 memcpy (signal, name, l);
1073 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1077 if (signal_id && detail_p)
1078 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1086 * g_signal_parse_name:
1087 * @detailed_signal: a string of the form "signal-name::detail".
1088 * @itype: The interface/instance type that introduced "signal-name".
1089 * @signal_id_p: (out): Location to store the signal id.
1090 * @detail_p: (out): Location to store the detail quark.
1091 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1093 * Internal function to parse a signal name into its @signal_id
1094 * and @detail quark.
1096 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1099 g_signal_parse_name (const gchar *detailed_signal,
1103 gboolean force_detail_quark)
1109 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1110 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1113 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1116 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1117 if (!node || node->destroyed ||
1118 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1122 *signal_id_p = signal_id;
1130 * g_signal_stop_emission_by_name:
1131 * @instance: the object whose signal handlers you wish to stop.
1132 * @detailed_signal: a string of the form "signal-name::detail".
1134 * Stops a signal's current emission.
1136 * This is just like g_signal_stop_emission() except it will look up the
1137 * signal id for you.
1140 g_signal_stop_emission_by_name (gpointer instance,
1141 const gchar *detailed_signal)
1147 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1148 g_return_if_fail (detailed_signal != NULL);
1151 itype = G_TYPE_FROM_INSTANCE (instance);
1152 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1155 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1157 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1158 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1159 else if (!g_type_is_a (itype, node->itype))
1160 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1163 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1164 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1168 if (emission->state == EMISSION_HOOK)
1169 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1170 node->name, instance);
1171 else if (emission->state == EMISSION_RUN)
1172 emission->state = EMISSION_STOP;
1175 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1176 node->name, instance);
1180 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1186 * @name: the signal's name.
1187 * @itype: the type that the signal operates on.
1189 * Given the name of the signal and the type of object it connects to, gets
1190 * the signal's identifying integer. Emitting the signal by number is
1191 * somewhat faster than using the name each time.
1193 * Also tries the ancestors of the given type.
1195 * See g_signal_new() for details on allowed signal names.
1197 * Returns: the signal's identifying number, or 0 if no signal was found.
1200 g_signal_lookup (const gchar *name,
1204 g_return_val_if_fail (name != NULL, 0);
1205 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1208 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1212 /* give elaborate warnings */
1213 if (!g_type_name (itype))
1214 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'",
1216 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1217 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1218 name, g_type_name (itype));
1219 else if (!g_type_class_peek (itype))
1220 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1221 name, g_type_name (itype));
1228 * g_signal_list_ids:
1229 * @itype: Instance or interface type.
1230 * @n_ids: Location to store the number of signal ids for @itype.
1232 * Lists the signals by id that a certain instance or interface type
1233 * created. Further information about the signals can be acquired through
1236 * Returns: (array length=n_ids): Newly allocated array of signal IDs.
1239 g_signal_list_ids (GType itype,
1247 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1248 g_return_val_if_fail (n_ids != NULL, NULL);
1251 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1252 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1253 result = g_array_new (FALSE, FALSE, sizeof (guint));
1255 for (i = 0; i < n_nodes; i++)
1256 if (keys[i].itype == itype)
1258 const gchar *name = g_quark_to_string (keys[i].quark);
1260 /* Signal names with "_" in them are aliases to the same
1261 * name with "-" instead of "_".
1263 if (!strchr (name, '_'))
1264 g_array_append_val (result, keys[i].signal_id);
1266 *n_ids = result->len;
1270 /* give elaborate warnings */
1271 if (!g_type_name (itype))
1272 g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'",
1274 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1275 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1276 g_type_name (itype));
1277 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1278 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1279 g_type_name (itype));
1282 return (guint*) g_array_free (result, FALSE);
1287 * @signal_id: the signal's identifying number.
1289 * Given the signal's identifier, finds its name.
1291 * Two different signals may have the same name, if they have differing types.
1293 * Returns: the signal name, or %NULL if the signal number was invalid.
1296 g_signal_name (guint signal_id)
1302 node = LOOKUP_SIGNAL_NODE (signal_id);
1303 name = node ? node->name : NULL;
1306 return (char*) name;
1311 * @signal_id: The signal id of the signal to query information for.
1312 * @query: (out caller-allocates): A user provided structure that is
1313 * filled in with constant values upon success.
1315 * Queries the signal system for in-depth information about a
1316 * specific signal. This function will fill in a user-provided
1317 * structure to hold signal-specific information. If an invalid
1318 * signal id is passed in, the @signal_id member of the #GSignalQuery
1319 * is 0. All members filled into the #GSignalQuery structure should
1320 * be considered constant and have to be left untouched.
1323 g_signal_query (guint signal_id,
1324 GSignalQuery *query)
1328 g_return_if_fail (query != NULL);
1331 node = LOOKUP_SIGNAL_NODE (signal_id);
1332 if (!node || node->destroyed)
1333 query->signal_id = 0;
1336 query->signal_id = node->signal_id;
1337 query->signal_name = node->name;
1338 query->itype = node->itype;
1339 query->signal_flags = node->flags;
1340 query->return_type = node->return_type;
1341 query->n_params = node->n_params;
1342 query->param_types = node->param_types;
1349 * @signal_name: the name for the signal
1350 * @itype: the type this signal pertains to. It will also pertain to
1351 * types which are derived from this type.
1352 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1353 * the default handler is to be invoked. You should at least specify
1354 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1355 * @class_offset: The offset of the function pointer in the class structure
1356 * for this type. Used to invoke a class method generically. Pass 0 to
1357 * not associate a class method slot with this signal.
1358 * @accumulator: the accumulator for this signal; may be %NULL.
1359 * @accu_data: user data for the @accumulator.
1360 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1361 * values to signal emissions into C language callback invocations or %NULL.
1362 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1363 * without a return value.
1364 * @n_params: the number of parameter types to follow.
1365 * @...: a list of types, one for each parameter.
1367 * Creates a new signal. (This is usually done in the class initializer.)
1369 * A signal name consists of segments consisting of ASCII letters and
1370 * digits, separated by either the '-' or '_' character. The first
1371 * character of a signal name must be a letter. Names which violate these
1372 * rules lead to undefined behaviour of the GSignal system.
1374 * When registering a signal and looking up a signal, either separator can
1375 * be used, but they cannot be mixed.
1377 * If 0 is used for @class_offset subclasses cannot override the class handler
1378 * in their <code>class_init</code> method by doing
1379 * <code>super_class->signal_handler = my_signal_handler</code>. Instead they
1380 * will have to use g_signal_override_class_handler().
1382 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1383 * the marshaller for this signal.
1385 * Returns: the signal id
1388 g_signal_new (const gchar *signal_name,
1390 GSignalFlags signal_flags,
1392 GSignalAccumulator accumulator,
1394 GSignalCMarshaller c_marshaller,
1402 g_return_val_if_fail (signal_name != NULL, 0);
1404 va_start (args, n_params);
1406 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1407 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1408 accumulator, accu_data, c_marshaller,
1409 return_type, n_params, args);
1417 * g_signal_new_class_handler:
1418 * @signal_name: the name for the signal
1419 * @itype: the type this signal pertains to. It will also pertain to
1420 * types which are derived from this type.
1421 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1422 * the default handler is to be invoked. You should at least specify
1423 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1424 * @class_handler: a #GCallback which acts as class implementation of
1425 * this signal. Used to invoke a class method generically. Pass %NULL to
1426 * not associate a class method with this signal.
1427 * @accumulator: the accumulator for this signal; may be %NULL.
1428 * @accu_data: user data for the @accumulator.
1429 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1430 * values to signal emissions into C language callback invocations or %NULL.
1431 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1432 * without a return value.
1433 * @n_params: the number of parameter types to follow.
1434 * @...: a list of types, one for each parameter.
1436 * Creates a new signal. (This is usually done in the class initializer.)
1438 * This is a variant of g_signal_new() that takes a C callback instead
1439 * off a class offset for the signal's class handler. This function
1440 * doesn't need a function pointer exposed in the class structure of
1441 * an object definition, instead the function pointer is passed
1442 * directly and can be overriden by derived classes with
1443 * g_signal_override_class_closure() or
1444 * g_signal_override_class_handler()and chained to with
1445 * g_signal_chain_from_overridden() or
1446 * g_signal_chain_from_overridden_handler().
1448 * See g_signal_new() for information about signal names.
1450 * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1451 * the marshaller for this signal.
1453 * Returns: the signal id
1458 g_signal_new_class_handler (const gchar *signal_name,
1460 GSignalFlags signal_flags,
1461 GCallback class_handler,
1462 GSignalAccumulator accumulator,
1464 GSignalCMarshaller c_marshaller,
1472 g_return_val_if_fail (signal_name != NULL, 0);
1474 va_start (args, n_params);
1476 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1477 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1478 accumulator, accu_data, c_marshaller,
1479 return_type, n_params, args);
1486 static inline ClassClosure*
1487 signal_find_class_closure (SignalNode *node,
1490 GBSearchArray *bsa = node->class_closure_bsa;
1497 /* cc->instance_type is 0 for default closure */
1499 key.instance_type = itype;
1500 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1501 while (!cc && key.instance_type)
1503 key.instance_type = g_type_parent (key.instance_type);
1504 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1512 static inline GClosure*
1513 signal_lookup_closure (SignalNode *node,
1514 GTypeInstance *instance)
1518 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1520 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1521 if (cc && cc->instance_type == 0) /* check for default closure */
1524 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1525 return cc ? cc->closure : NULL;
1529 signal_add_class_closure (SignalNode *node,
1535 node->single_va_closure_is_valid = FALSE;
1537 if (!node->class_closure_bsa)
1538 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1539 key.instance_type = itype;
1540 key.closure = g_closure_ref (closure);
1541 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1542 &g_class_closure_bconfig,
1544 g_closure_sink (closure);
1545 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1547 g_closure_set_marshal (closure, node->c_marshaller);
1548 if (node->va_marshaller)
1549 _g_closure_set_va_marshal (closure, node->va_marshaller);
1555 * @signal_name: the name for the signal
1556 * @itype: the type this signal pertains to. It will also pertain to
1557 * types which are derived from this type
1558 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1559 * the default handler is to be invoked. You should at least specify
1560 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1561 * @class_closure: (allow-none): The closure to invoke on signal emission;
1563 * @accumulator: (allow-none): the accumulator for this signal; may be %NULL
1564 * @accu_data: user data for the @accumulator
1565 * @c_marshaller: (allow-none): the function to translate arrays of
1566 * parameter values to signal emissions into C language callback
1567 * invocations or %NULL
1568 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1569 * without a return value
1570 * @n_params: the length of @param_types
1571 * @param_types: (array length=n_params): an array of types, one for
1574 * Creates a new signal. (This is usually done in the class initializer.)
1576 * See g_signal_new() for details on allowed signal names.
1578 * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1579 * the marshaller for this signal.
1581 * Returns: the signal id
1584 g_signal_newv (const gchar *signal_name,
1586 GSignalFlags signal_flags,
1587 GClosure *class_closure,
1588 GSignalAccumulator accumulator,
1590 GSignalCMarshaller c_marshaller,
1598 GSignalCMarshaller builtin_c_marshaller;
1599 GSignalCVaMarshaller va_marshaller;
1601 g_return_val_if_fail (signal_name != NULL, 0);
1602 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1604 g_return_val_if_fail (param_types != NULL, 0);
1605 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1606 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1607 g_return_val_if_fail (accumulator == NULL, 0);
1609 g_return_val_if_fail (accu_data == NULL, 0);
1611 name = g_strdup (signal_name);
1612 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1616 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1617 node = LOOKUP_SIGNAL_NODE (signal_id);
1618 if (node && !node->destroyed)
1620 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1622 type_debug_name (node->itype),
1623 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1628 if (node && node->itype != itype)
1630 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1632 type_debug_name (itype),
1633 type_debug_name (node->itype));
1638 for (i = 0; i < n_params; i++)
1639 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1641 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1642 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1647 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1649 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1650 type_debug_name (return_type), type_debug_name (itype), name);
1655 if (return_type != G_TYPE_NONE &&
1656 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1658 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1659 type_debug_name (itype), name, type_debug_name (return_type));
1665 /* setup permanent portion of signal node */
1670 signal_id = g_n_signal_nodes++;
1671 node = g_new (SignalNode, 1);
1672 node->signal_id = signal_id;
1673 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1674 g_signal_nodes[signal_id] = node;
1675 node->itype = itype;
1678 key.quark = g_quark_from_string (node->name);
1679 key.signal_id = signal_id;
1680 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1681 g_strdelimit (name, "_", '-');
1682 node->name = g_intern_string (name);
1683 key.quark = g_quark_from_string (name);
1684 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1686 TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1688 node->destroyed = FALSE;
1690 /* setup reinitializable portion */
1691 node->single_va_closure_is_valid = FALSE;
1692 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1693 node->n_params = n_params;
1694 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1695 node->return_type = return_type;
1696 node->class_closure_bsa = NULL;
1699 node->accumulator = g_new (SignalAccumulator, 1);
1700 node->accumulator->func = accumulator;
1701 node->accumulator->data = accu_data;
1704 node->accumulator = NULL;
1706 builtin_c_marshaller = NULL;
1707 va_marshaller = NULL;
1709 /* Pick up built-in va marshallers for standard types, and
1710 instead of generic marshaller if no marshaller specified */
1711 if (n_params == 0 && return_type == G_TYPE_NONE)
1713 builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1714 va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1716 else if (n_params == 1 && return_type == G_TYPE_NONE)
1718 #define ADD_CHECK(__type__) \
1719 else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \
1721 builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \
1722 va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \
1745 if (c_marshaller == NULL)
1747 if (builtin_c_marshaller)
1748 c_marshaller = builtin_c_marshaller;
1751 c_marshaller = g_cclosure_marshal_generic;
1752 va_marshaller = g_cclosure_marshal_generic_va;
1756 node->c_marshaller = c_marshaller;
1757 node->va_marshaller = va_marshaller;
1758 node->emission_hooks = NULL;
1760 signal_add_class_closure (node, 0, class_closure);
1770 g_signal_set_va_marshaller (guint signal_id,
1771 GType instance_type,
1772 GSignalCVaMarshaller va_marshaller)
1776 g_return_if_fail (signal_id > 0);
1777 g_return_if_fail (va_marshaller != NULL);
1780 node = LOOKUP_SIGNAL_NODE (signal_id);
1783 node->va_marshaller = va_marshaller;
1784 if (node->class_closure_bsa)
1786 ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1787 if (cc->closure->marshal == node->c_marshaller)
1788 _g_closure_set_va_marshal (cc->closure, va_marshaller);
1791 node->single_va_closure_is_valid = FALSE;
1799 * g_signal_new_valist:
1800 * @signal_name: the name for the signal
1801 * @itype: the type this signal pertains to. It will also pertain to
1802 * types which are derived from this type.
1803 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1804 * the default handler is to be invoked. You should at least specify
1805 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1806 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1807 * @accumulator: the accumulator for this signal; may be %NULL.
1808 * @accu_data: user data for the @accumulator.
1809 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1810 * values to signal emissions into C language callback invocations or %NULL.
1811 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1812 * without a return value.
1813 * @n_params: the number of parameter types in @args.
1814 * @args: va_list of #GType, one for each parameter.
1816 * Creates a new signal. (This is usually done in the class initializer.)
1818 * See g_signal_new() for details on allowed signal names.
1820 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1821 * the marshaller for this signal.
1823 * Returns: the signal id
1826 g_signal_new_valist (const gchar *signal_name,
1828 GSignalFlags signal_flags,
1829 GClosure *class_closure,
1830 GSignalAccumulator accumulator,
1832 GSignalCMarshaller c_marshaller,
1843 param_types = g_new (GType, n_params);
1845 for (i = 0; i < n_params; i++)
1846 param_types[i] = va_arg (args, GType);
1851 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1852 class_closure, accumulator, accu_data, c_marshaller,
1853 return_type, n_params, param_types);
1854 g_free (param_types);
1860 signal_destroy_R (SignalNode *signal_node)
1862 SignalNode node = *signal_node;
1864 signal_node->destroyed = TRUE;
1866 /* reentrancy caution, zero out real contents first */
1867 signal_node->single_va_closure_is_valid = FALSE;
1868 signal_node->n_params = 0;
1869 signal_node->param_types = NULL;
1870 signal_node->return_type = 0;
1871 signal_node->class_closure_bsa = NULL;
1872 signal_node->accumulator = NULL;
1873 signal_node->c_marshaller = NULL;
1874 signal_node->va_marshaller = NULL;
1875 signal_node->emission_hooks = NULL;
1877 #ifdef G_ENABLE_DEBUG
1878 /* check current emissions */
1882 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1883 emission; emission = emission->next)
1884 if (emission->ihint.signal_id == node.signal_id)
1885 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1886 node.name, emission->instance);
1890 /* free contents that need to
1893 g_free (node.param_types);
1894 if (node.class_closure_bsa)
1898 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1900 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1902 g_closure_unref (cc->closure);
1904 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1906 g_free (node.accumulator);
1907 if (node.emission_hooks)
1909 g_hook_list_clear (node.emission_hooks);
1910 g_free (node.emission_hooks);
1916 * g_signal_override_class_closure:
1917 * @signal_id: the signal id
1918 * @instance_type: the instance type on which to override the class closure
1920 * @class_closure: the closure.
1922 * Overrides the class closure (i.e. the default handler) for the given signal
1923 * for emissions on instances of @instance_type. @instance_type must be derived
1924 * from the type to which the signal belongs.
1926 * See g_signal_chain_from_overridden() and
1927 * g_signal_chain_from_overridden_handler() for how to chain up to the
1928 * parent class closure from inside the overridden one.
1931 g_signal_override_class_closure (guint signal_id,
1932 GType instance_type,
1933 GClosure *class_closure)
1937 g_return_if_fail (signal_id > 0);
1938 g_return_if_fail (class_closure != NULL);
1941 node = LOOKUP_SIGNAL_NODE (signal_id);
1942 node_check_deprecated (node);
1943 if (!g_type_is_a (instance_type, node->itype))
1944 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1947 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1949 if (cc && cc->instance_type == instance_type)
1950 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1952 signal_add_class_closure (node, instance_type, class_closure);
1958 * g_signal_override_class_handler:
1959 * @signal_name: the name for the signal
1960 * @instance_type: the instance type on which to override the class handler
1962 * @class_handler: the handler.
1964 * Overrides the class closure (i.e. the default handler) for the
1965 * given signal for emissions on instances of @instance_type with
1966 * callabck @class_handler. @instance_type must be derived from the
1967 * type to which the signal belongs.
1969 * See g_signal_chain_from_overridden() and
1970 * g_signal_chain_from_overridden_handler() for how to chain up to the
1971 * parent class closure from inside the overridden one.
1976 g_signal_override_class_handler (const gchar *signal_name,
1977 GType instance_type,
1978 GCallback class_handler)
1982 g_return_if_fail (signal_name != NULL);
1983 g_return_if_fail (instance_type != G_TYPE_NONE);
1984 g_return_if_fail (class_handler != NULL);
1986 signal_id = g_signal_lookup (signal_name, instance_type);
1989 g_signal_override_class_closure (signal_id, instance_type,
1990 g_cclosure_new (class_handler, NULL, NULL));
1992 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1993 G_STRLOC, signal_name, instance_type);
1998 * g_signal_chain_from_overridden:
1999 * @instance_and_params: (array) the argument list of the signal emission.
2000 * The first element in the array is a #GValue for the instance the signal
2001 * is being emitted on. The rest are any arguments to be passed to the signal.
2002 * @return_value: Location for the return value.
2004 * Calls the original class closure of a signal. This function should only
2005 * be called from an overridden class closure; see
2006 * g_signal_override_class_closure() and
2007 * g_signal_override_class_handler().
2010 g_signal_chain_from_overridden (const GValue *instance_and_params,
2011 GValue *return_value)
2013 GType chain_type = 0, restore_type = 0;
2014 Emission *emission = NULL;
2015 GClosure *closure = NULL;
2019 g_return_if_fail (instance_and_params != NULL);
2020 instance = g_value_peek_pointer (instance_and_params);
2021 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2024 emission = emission_find_innermost (instance);
2027 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2029 g_assert (node != NULL); /* paranoid */
2031 /* we should probably do the same parameter checks as g_signal_emit() here.
2033 if (emission->chain_type != G_TYPE_NONE)
2035 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2037 g_assert (cc != NULL); /* closure currently in call stack */
2039 n_params = node->n_params;
2040 restore_type = cc->instance_type;
2041 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2042 if (cc && cc->instance_type != restore_type)
2044 closure = cc->closure;
2045 chain_type = cc->instance_type;
2049 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
2052 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
2056 emission->chain_type = chain_type;
2058 g_closure_invoke (closure,
2061 instance_and_params,
2064 emission->chain_type = restore_type;
2070 * g_signal_chain_from_overridden_handler:
2071 * @instance: the instance the signal is being emitted on.
2072 * @...: parameters to be passed to the parent class closure, followed by a
2073 * location for the return value. If the return type of the signal
2074 * is #G_TYPE_NONE, the return value location can be omitted.
2076 * Calls the original class closure of a signal. This function should
2077 * only be called from an overridden class closure; see
2078 * g_signal_override_class_closure() and
2079 * g_signal_override_class_handler().
2084 g_signal_chain_from_overridden_handler (gpointer instance,
2087 GType chain_type = 0, restore_type = 0;
2088 Emission *emission = NULL;
2089 GClosure *closure = NULL;
2093 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2096 emission = emission_find_innermost (instance);
2099 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2101 g_assert (node != NULL); /* paranoid */
2103 /* we should probably do the same parameter checks as g_signal_emit() here.
2105 if (emission->chain_type != G_TYPE_NONE)
2107 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2109 g_assert (cc != NULL); /* closure currently in call stack */
2111 n_params = node->n_params;
2112 restore_type = cc->instance_type;
2113 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2114 if (cc && cc->instance_type != restore_type)
2116 closure = cc->closure;
2117 chain_type = cc->instance_type;
2121 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
2124 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
2128 GValue *instance_and_params;
2129 GType signal_return_type;
2130 GValue *param_values;
2134 va_start (var_args, instance);
2136 signal_return_type = node->return_type;
2137 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
2138 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
2139 param_values = instance_and_params + 1;
2141 for (i = 0; i < node->n_params; i++)
2144 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2145 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2148 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2150 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2154 g_warning ("%s: %s", G_STRLOC, error);
2157 /* we purposely leak the value here, it might not be
2158 * in a sane state if an error condition occoured
2161 g_value_unset (param_values + i);
2170 instance_and_params->g_type = 0;
2171 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2172 g_value_set_instance (instance_and_params, instance);
2175 emission->chain_type = chain_type;
2178 if (signal_return_type == G_TYPE_NONE)
2180 g_closure_invoke (closure,
2183 instance_and_params,
2188 GValue return_value = G_VALUE_INIT;
2189 gchar *error = NULL;
2190 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2191 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2193 g_value_init (&return_value, rtype);
2195 g_closure_invoke (closure,
2198 instance_and_params,
2201 G_VALUE_LCOPY (&return_value,
2203 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2207 g_value_unset (&return_value);
2211 g_warning ("%s: %s", G_STRLOC, error);
2214 /* we purposely leak the value here, it might not be
2215 * in a sane state if an error condition occurred
2220 for (i = 0; i < n_params; i++)
2221 g_value_unset (param_values + i);
2222 g_value_unset (instance_and_params);
2227 emission->chain_type = restore_type;
2233 * g_signal_get_invocation_hint:
2234 * @instance: the instance to query
2236 * Returns the invocation hint of the innermost signal emission of instance.
2238 * Returns: (transfer none): the invocation hint of the innermost signal emission.
2240 GSignalInvocationHint*
2241 g_signal_get_invocation_hint (gpointer instance)
2243 Emission *emission = NULL;
2245 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2248 emission = emission_find_innermost (instance);
2251 return emission ? &emission->ihint : NULL;
2255 * g_signal_connect_closure_by_id:
2256 * @instance: the instance to connect to.
2257 * @signal_id: the id of the signal.
2258 * @detail: the detail.
2259 * @closure: the closure to connect.
2260 * @after: whether the handler should be called before or after the
2261 * default handler of the signal.
2263 * Connects a closure to a signal for a particular object.
2265 * Returns: the handler id
2268 g_signal_connect_closure_by_id (gpointer instance,
2275 gulong handler_seq_no = 0;
2277 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2278 g_return_val_if_fail (signal_id > 0, 0);
2279 g_return_val_if_fail (closure != NULL, 0);
2282 node = LOOKUP_SIGNAL_NODE (signal_id);
2285 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2286 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2287 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2288 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2291 Handler *handler = handler_new (after);
2293 handler_seq_no = handler->sequential_number;
2294 handler->detail = detail;
2295 handler->closure = g_closure_ref (closure);
2296 g_closure_sink (closure);
2297 add_invalid_closure_notify (handler, instance);
2298 handler_insert (signal_id, instance, handler);
2299 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2301 g_closure_set_marshal (closure, node->c_marshaller);
2302 if (node->va_marshaller)
2303 _g_closure_set_va_marshal (closure, node->va_marshaller);
2308 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2311 return handler_seq_no;
2315 * g_signal_connect_closure:
2316 * @instance: the instance to connect to.
2317 * @detailed_signal: a string of the form "signal-name::detail".
2318 * @closure: the closure to connect.
2319 * @after: whether the handler should be called before or after the
2320 * default handler of the signal.
2322 * Connects a closure to a signal for a particular object.
2324 * Returns: the handler id
2327 g_signal_connect_closure (gpointer instance,
2328 const gchar *detailed_signal,
2333 gulong handler_seq_no = 0;
2337 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2338 g_return_val_if_fail (detailed_signal != NULL, 0);
2339 g_return_val_if_fail (closure != NULL, 0);
2342 itype = G_TYPE_FROM_INSTANCE (instance);
2343 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2346 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2348 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2349 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2350 else if (!g_type_is_a (itype, node->itype))
2351 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2354 Handler *handler = handler_new (after);
2356 handler_seq_no = handler->sequential_number;
2357 handler->detail = detail;
2358 handler->closure = g_closure_ref (closure);
2359 g_closure_sink (closure);
2360 add_invalid_closure_notify (handler, instance);
2361 handler_insert (signal_id, instance, handler);
2362 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2364 g_closure_set_marshal (handler->closure, node->c_marshaller);
2365 if (node->va_marshaller)
2366 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2371 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2374 return handler_seq_no;
2378 node_check_deprecated (const SignalNode *node)
2380 static const gchar * g_enable_diagnostic = NULL;
2382 if (G_UNLIKELY (!g_enable_diagnostic))
2384 g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2385 if (!g_enable_diagnostic)
2386 g_enable_diagnostic = "0";
2389 if (g_enable_diagnostic[0] == '1')
2391 if (node->flags & G_SIGNAL_DEPRECATED)
2393 g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2394 "anymore. It will be removed in a future version.",
2395 type_debug_name (node->itype), node->name);
2401 * g_signal_connect_data:
2402 * @instance: the instance to connect to.
2403 * @detailed_signal: a string of the form "signal-name::detail".
2404 * @c_handler: the #GCallback to connect.
2405 * @data: data to pass to @c_handler calls.
2406 * @destroy_data: a #GClosureNotify for @data.
2407 * @connect_flags: a combination of #GConnectFlags.
2409 * Connects a #GCallback function to a signal for a particular object. Similar
2410 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2411 * which will be called when the signal handler is disconnected and no longer
2412 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
2413 * <literal>..._swapped()</literal> variants of this function.
2415 * Returns: the handler id
2418 g_signal_connect_data (gpointer instance,
2419 const gchar *detailed_signal,
2420 GCallback c_handler,
2422 GClosureNotify destroy_data,
2423 GConnectFlags connect_flags)
2426 gulong handler_seq_no = 0;
2429 gboolean swapped, after;
2431 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2432 g_return_val_if_fail (detailed_signal != NULL, 0);
2433 g_return_val_if_fail (c_handler != NULL, 0);
2435 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2436 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2439 itype = G_TYPE_FROM_INSTANCE (instance);
2440 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2443 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2445 node_check_deprecated (node);
2447 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2448 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2449 else if (!g_type_is_a (itype, node->itype))
2450 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2453 Handler *handler = handler_new (after);
2455 handler_seq_no = handler->sequential_number;
2456 handler->detail = detail;
2457 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2458 g_closure_sink (handler->closure);
2459 handler_insert (signal_id, instance, handler);
2460 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2462 g_closure_set_marshal (handler->closure, node->c_marshaller);
2463 if (node->va_marshaller)
2464 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2469 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2472 return handler_seq_no;
2476 * g_signal_handler_block:
2477 * @instance: The instance to block the signal handler of.
2478 * @handler_id: Handler id of the handler to be blocked.
2480 * Blocks a handler of an instance so it will not be called during any
2481 * signal emissions unless it is unblocked again. Thus "blocking" a
2482 * signal handler means to temporarily deactive it, a signal handler
2483 * has to be unblocked exactly the same amount of times it has been
2484 * blocked before to become active again.
2486 * The @handler_id has to be a valid signal handler id, connected to a
2487 * signal of @instance.
2490 g_signal_handler_block (gpointer instance,
2495 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2496 g_return_if_fail (handler_id > 0);
2499 handler = handler_lookup (instance, handler_id, NULL, NULL);
2502 #ifndef G_DISABLE_CHECKS
2503 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2504 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2506 handler->block_count += 1;
2509 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2514 * g_signal_handler_unblock:
2515 * @instance: The instance to unblock the signal handler of.
2516 * @handler_id: Handler id of the handler to be unblocked.
2518 * Undoes the effect of a previous g_signal_handler_block() call. A
2519 * blocked handler is skipped during signal emissions and will not be
2520 * invoked, unblocking it (for exactly the amount of times it has been
2521 * blocked before) reverts its "blocked" state, so the handler will be
2522 * recognized by the signal system and is called upon future or
2523 * currently ongoing signal emissions (since the order in which
2524 * handlers are called during signal emissions is deterministic,
2525 * whether the unblocked handler in question is called as part of a
2526 * currently ongoing emission depends on how far that emission has
2529 * The @handler_id has to be a valid id of a signal handler that is
2530 * connected to a signal of @instance and is currently blocked.
2533 g_signal_handler_unblock (gpointer instance,
2538 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2539 g_return_if_fail (handler_id > 0);
2542 handler = handler_lookup (instance, handler_id, NULL, NULL);
2545 if (handler->block_count)
2546 handler->block_count -= 1;
2548 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2551 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2556 * g_signal_handler_disconnect:
2557 * @instance: The instance to remove the signal handler from.
2558 * @handler_id: Handler id of the handler to be disconnected.
2560 * Disconnects a handler from an instance so it will not be called during
2561 * any future or currently ongoing emissions of the signal it has been
2562 * connected to. The @handler_id becomes invalid and may be reused.
2564 * The @handler_id has to be a valid signal handler id, connected to a
2565 * signal of @instance.
2568 g_signal_handler_disconnect (gpointer instance,
2574 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2575 g_return_if_fail (handler_id > 0);
2578 handler = handler_lookup (instance, handler_id, NULL, &signal_id);
2581 handler->sequential_number = 0;
2582 handler->block_count = 1;
2583 remove_invalid_closure_notify (handler, instance);
2584 handler_unref_R (signal_id, instance, handler);
2587 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2592 * g_signal_handler_is_connected:
2593 * @instance: The instance where a signal handler is sought.
2594 * @handler_id: the handler id.
2596 * Returns whether @handler_id is the id of a handler connected to @instance.
2598 * Returns: whether @handler_id identifies a handler connected to @instance.
2601 g_signal_handler_is_connected (gpointer instance,
2607 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2610 handler = handler_lookup (instance, handler_id, NULL, NULL);
2611 connected = handler != NULL;
2618 g_signal_handlers_destroy (gpointer instance)
2620 GBSearchArray *hlbsa;
2622 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2625 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2630 /* reentrancy caution, delete instance trace first */
2631 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2633 for (i = 0; i < hlbsa->n_nodes; i++)
2635 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2636 Handler *handler = hlist->handlers;
2640 Handler *tmp = handler;
2642 handler = tmp->next;
2643 tmp->block_count = 1;
2644 /* cruel unlink, this works because _all_ handlers vanish */
2647 if (tmp->sequential_number)
2649 remove_invalid_closure_notify (tmp, instance);
2650 tmp->sequential_number = 0;
2651 handler_unref_R (0, NULL, tmp);
2655 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2661 * g_signal_handler_find:
2662 * @instance: The instance owning the signal handler to be found.
2663 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2664 * and/or @data the handler has to match.
2665 * @signal_id: Signal the handler has to be connected to.
2666 * @detail: Signal detail the handler has to be connected to.
2667 * @closure: (allow-none): The closure the handler will invoke.
2668 * @func: The C closure callback of the handler (useless for non-C closures).
2669 * @data: The closure data of the handler's closure.
2671 * Finds the first signal handler that matches certain selection criteria.
2672 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2673 * flags, and the criteria values are passed as arguments.
2674 * The match @mask has to be non-0 for successful matches.
2675 * If no handler was found, 0 is returned.
2677 * Returns: A valid non-0 signal handler id for a successful match.
2680 g_signal_handler_find (gpointer instance,
2681 GSignalMatchType mask,
2688 gulong handler_seq_no = 0;
2690 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2691 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2693 if (mask & G_SIGNAL_MATCH_MASK)
2695 HandlerMatch *mlist;
2698 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2701 handler_seq_no = mlist->handler->sequential_number;
2702 handler_match_free1_R (mlist, instance);
2707 return handler_seq_no;
2711 signal_handlers_foreach_matched_R (gpointer instance,
2712 GSignalMatchType mask,
2718 void (*callback) (gpointer instance,
2719 gulong handler_seq_no))
2721 HandlerMatch *mlist;
2722 guint n_handlers = 0;
2724 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2728 if (mlist->handler->sequential_number)
2731 callback (instance, mlist->handler->sequential_number);
2734 mlist = handler_match_free1_R (mlist, instance);
2741 * g_signal_handlers_block_matched:
2742 * @instance: The instance to block handlers from.
2743 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2744 * and/or @data the handlers have to match.
2745 * @signal_id: Signal the handlers have to be connected to.
2746 * @detail: Signal detail the handlers have to be connected to.
2747 * @closure: (allow-none): The closure the handlers will invoke.
2748 * @func: The C closure callback of the handlers (useless for non-C closures).
2749 * @data: The closure data of the handlers' closures.
2751 * Blocks all handlers on an instance that match a certain selection criteria.
2752 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2753 * flags, and the criteria values are passed as arguments.
2754 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2755 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2756 * If no handlers were found, 0 is returned, the number of blocked handlers
2759 * Returns: The number of handlers that matched.
2762 g_signal_handlers_block_matched (gpointer instance,
2763 GSignalMatchType mask,
2770 guint n_handlers = 0;
2772 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2773 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2775 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2778 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2779 closure, func, data,
2780 g_signal_handler_block);
2788 * g_signal_handlers_unblock_matched:
2789 * @instance: The instance to unblock handlers from.
2790 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2791 * and/or @data the handlers have to match.
2792 * @signal_id: Signal the handlers have to be connected to.
2793 * @detail: Signal detail the handlers have to be connected to.
2794 * @closure: (allow-none): The closure the handlers will invoke.
2795 * @func: The C closure callback of the handlers (useless for non-C closures).
2796 * @data: The closure data of the handlers' closures.
2798 * Unblocks all handlers on an instance that match a certain selection
2799 * criteria. The criteria mask is passed as an OR-ed combination of
2800 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2801 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2802 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2803 * If no handlers were found, 0 is returned, the number of unblocked handlers
2804 * otherwise. The match criteria should not apply to any handlers that are
2805 * not currently blocked.
2807 * Returns: The number of handlers that matched.
2810 g_signal_handlers_unblock_matched (gpointer instance,
2811 GSignalMatchType mask,
2818 guint n_handlers = 0;
2820 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2821 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2823 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2826 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2827 closure, func, data,
2828 g_signal_handler_unblock);
2836 * g_signal_handlers_disconnect_matched:
2837 * @instance: The instance to remove handlers from.
2838 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2839 * and/or @data the handlers have to match.
2840 * @signal_id: Signal the handlers have to be connected to.
2841 * @detail: Signal detail the handlers have to be connected to.
2842 * @closure: (allow-none): The closure the handlers will invoke.
2843 * @func: The C closure callback of the handlers (useless for non-C closures).
2844 * @data: The closure data of the handlers' closures.
2846 * Disconnects all handlers on an instance that match a certain
2847 * selection criteria. The criteria mask is passed as an OR-ed
2848 * combination of #GSignalMatchType flags, and the criteria values are
2849 * passed as arguments. Passing at least one of the
2850 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2851 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2852 * matches. If no handlers were found, 0 is returned, the number of
2853 * disconnected handlers otherwise.
2855 * Returns: The number of handlers that matched.
2858 g_signal_handlers_disconnect_matched (gpointer instance,
2859 GSignalMatchType mask,
2866 guint n_handlers = 0;
2868 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2869 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2871 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2874 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2875 closure, func, data,
2876 g_signal_handler_disconnect);
2884 * g_signal_has_handler_pending:
2885 * @instance: the object whose signal handlers are sought.
2886 * @signal_id: the signal id.
2887 * @detail: the detail.
2888 * @may_be_blocked: whether blocked handlers should count as match.
2890 * Returns whether there are any handlers connected to @instance for the
2891 * given signal id and detail.
2893 * One example of when you might use this is when the arguments to the
2894 * signal are difficult to compute. A class implementor may opt to not
2895 * emit the signal if no one is attached anyway, thus saving the cost
2896 * of building the arguments.
2898 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2902 g_signal_has_handler_pending (gpointer instance,
2905 gboolean may_be_blocked)
2907 HandlerMatch *mlist;
2908 gboolean has_pending;
2910 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2911 g_return_val_if_fail (signal_id > 0, FALSE);
2916 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2918 if (!(node->flags & G_SIGNAL_DETAILED))
2920 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2925 mlist = handlers_find (instance,
2926 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2927 signal_id, detail, NULL, NULL, NULL, TRUE);
2931 handler_match_free1_R (mlist, instance);
2934 has_pending = FALSE;
2942 * @instance_and_params: (array): argument list for the signal emission.
2943 * The first element in the array is a #GValue for the instance the signal
2944 * is being emitted on. The rest are any arguments to be passed to the signal.
2945 * @signal_id: the signal id
2946 * @detail: the detail
2947 * @return_value: Location to store the return value of the signal emission.
2951 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2952 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2955 g_signal_emitv (const GValue *instance_and_params,
2958 GValue *return_value)
2962 #ifdef G_ENABLE_DEBUG
2963 const GValue *param_values;
2967 g_return_if_fail (instance_and_params != NULL);
2968 instance = g_value_peek_pointer (instance_and_params);
2969 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2970 g_return_if_fail (signal_id > 0);
2972 #ifdef G_ENABLE_DEBUG
2973 param_values = instance_and_params + 1;
2977 node = LOOKUP_SIGNAL_NODE (signal_id);
2978 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2980 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2984 #ifdef G_ENABLE_DEBUG
2985 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2987 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2991 for (i = 0; i < node->n_params; i++)
2992 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2994 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2996 type_debug_name (node->param_types[i]),
2999 G_VALUE_TYPE_NAME (param_values + i));
3003 if (node->return_type != G_TYPE_NONE)
3007 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
3009 type_debug_name (node->return_type),
3014 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3016 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
3018 type_debug_name (node->return_type),
3020 G_VALUE_TYPE_NAME (return_value));
3026 return_value = NULL;
3027 #endif /* G_ENABLE_DEBUG */
3029 /* optimize NOP emissions */
3030 if (!node->single_va_closure_is_valid)
3031 node_update_single_va_closure (node);
3033 if (node->single_va_closure != NULL &&
3034 (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3035 _g_closure_is_void (node->single_va_closure, instance))
3036 #ifdef G_ENABLE_DEBUG
3037 && !COND_DEBUG (SIGNALS, g_trace_instance_signals != instance &&
3038 g_trap_instance_signals == instance)
3039 #endif /* G_ENABLE_DEBUG */
3042 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3043 if (hlist == NULL || hlist->handlers == NULL)
3045 /* nothing to do to emit this signal */
3047 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3053 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
3056 static inline gboolean
3057 accumulate (GSignalInvocationHint *ihint,
3058 GValue *return_accu,
3059 GValue *handler_return,
3060 SignalAccumulator *accumulator)
3062 gboolean continue_emission;
3067 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3068 g_value_reset (handler_return);
3070 return continue_emission;
3074 * g_signal_emit_valist:
3075 * @instance: the instance the signal is being emitted on.
3076 * @signal_id: the signal id
3077 * @detail: the detail
3078 * @var_args: a list of parameters to be passed to the signal, followed by a
3079 * location for the return value. If the return type of the signal
3080 * is #G_TYPE_NONE, the return value location can be omitted.
3084 * Note that g_signal_emit_valist() resets the return value to the default
3085 * if no handlers are connected, in contrast to g_signal_emitv().
3088 g_signal_emit_valist (gpointer instance,
3093 GValue *instance_and_params;
3094 GType signal_return_type;
3095 GValue *param_values;
3099 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3100 g_return_if_fail (signal_id > 0);
3103 node = LOOKUP_SIGNAL_NODE (signal_id);
3104 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3106 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
3110 #ifndef G_DISABLE_CHECKS
3111 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3113 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3117 #endif /* !G_DISABLE_CHECKS */
3119 if (!node->single_va_closure_is_valid)
3120 node_update_single_va_closure (node);
3122 if (node->single_va_closure != NULL
3123 #ifdef G_ENABLE_DEBUG
3124 && !COND_DEBUG (SIGNALS, g_trace_instance_signals != instance &&
3125 g_trap_instance_signals == instance)
3126 #endif /* G_ENABLE_DEBUG */
3129 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3130 Handler *fastpath_handler = NULL;
3132 GClosure *closure = NULL;
3133 gboolean fastpath = TRUE;
3134 GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3136 if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3137 !_g_closure_is_void (node->single_va_closure, instance))
3139 if (_g_closure_supports_invoke_va (node->single_va_closure))
3141 closure = node->single_va_closure;
3142 if (node->single_va_closure_is_after)
3143 run_type = G_SIGNAL_RUN_LAST;
3145 run_type = G_SIGNAL_RUN_FIRST;
3151 for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3153 if (!l->block_count &&
3154 (!l->detail || l->detail == detail))
3156 if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3163 fastpath_handler = l;
3164 closure = l->closure;
3166 run_type = G_SIGNAL_RUN_LAST;
3168 run_type = G_SIGNAL_RUN_FIRST;
3173 if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3179 /* Don't allow no-recurse emission as we might have to restart, which means
3180 we will run multiple handlers and thus must ref all arguments */
3181 if (closure != NULL && (node->flags & (G_SIGNAL_NO_RECURSE)) != 0)
3186 SignalAccumulator *accumulator;
3188 GValue *return_accu, accu = G_VALUE_INIT;
3190 GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3191 GValue emission_return = G_VALUE_INIT;
3192 GType rtype = node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3193 gboolean static_scope = node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3195 signal_id = node->signal_id;
3196 accumulator = node->accumulator;
3197 if (rtype == G_TYPE_NONE)
3199 else if (accumulator)
3200 return_accu = &accu;
3202 return_accu = &emission_return;
3204 emission.instance = instance;
3205 emission.ihint.signal_id = signal_id;
3206 emission.ihint.detail = detail;
3207 emission.ihint.run_type = run_type;
3208 emission.state = EMISSION_RUN;
3209 emission.chain_type = instance_type;
3210 emission_push (&g_recursive_emissions, &emission);
3212 if (fastpath_handler)
3213 handler_ref (fastpath_handler);
3217 TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3219 if (rtype != G_TYPE_NONE)
3220 g_value_init (&emission_return, rtype);
3223 g_value_init (&accu, rtype);
3225 if (closure != NULL)
3227 g_object_ref (instance);
3228 _g_closure_invoke_va (closure,
3234 accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3239 emission.chain_type = G_TYPE_NONE;
3240 emission_pop (&g_recursive_emissions, &emission);
3242 if (fastpath_handler)
3243 handler_unref_R (signal_id, instance, fastpath_handler);
3248 g_value_unset (&accu);
3250 if (rtype != G_TYPE_NONE)
3252 gchar *error = NULL;
3253 for (i = 0; i < node->n_params; i++)
3255 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3256 G_VALUE_COLLECT_SKIP (ptype, var_args);
3259 G_VALUE_LCOPY (&emission_return,
3261 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3264 g_value_unset (&emission_return);
3267 g_warning ("%s: %s", G_STRLOC, error);
3269 /* we purposely leak the value here, it might not be
3270 * in a sane state if an error condition occurred
3275 TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3277 if (closure != NULL)
3278 g_object_unref (instance);
3284 n_params = node->n_params;
3285 signal_return_type = node->return_type;
3286 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
3287 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
3288 param_values = instance_and_params + 1;
3290 for (i = 0; i < node->n_params; i++)
3293 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3294 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3297 G_VALUE_COLLECT_INIT (param_values + i, ptype,
3299 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3303 g_warning ("%s: %s", G_STRLOC, error);
3306 /* we purposely leak the value here, it might not be
3307 * in a sane state if an error condition occoured
3310 g_value_unset (param_values + i);
3318 instance_and_params->g_type = 0;
3319 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
3320 g_value_set_instance (instance_and_params, instance);
3321 if (signal_return_type == G_TYPE_NONE)
3322 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
3325 GValue return_value = G_VALUE_INIT;
3326 gchar *error = NULL;
3327 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3328 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3330 g_value_init (&return_value, rtype);
3332 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3334 G_VALUE_LCOPY (&return_value,
3336 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3339 g_value_unset (&return_value);
3342 g_warning ("%s: %s", G_STRLOC, error);
3345 /* we purposely leak the value here, it might not be
3346 * in a sane state if an error condition occurred
3350 for (i = 0; i < n_params; i++)
3351 g_value_unset (param_values + i);
3352 g_value_unset (instance_and_params);
3357 * @instance: the instance the signal is being emitted on.
3358 * @signal_id: the signal id
3359 * @detail: the detail
3360 * @...: parameters to be passed to the signal, followed by a
3361 * location for the return value. If the return type of the signal
3362 * is #G_TYPE_NONE, the return value location can be omitted.
3366 * Note that g_signal_emit() resets the return value to the default
3367 * if no handlers are connected, in contrast to g_signal_emitv().
3370 g_signal_emit (gpointer instance,
3377 va_start (var_args, detail);
3378 g_signal_emit_valist (instance, signal_id, detail, var_args);
3383 * g_signal_emit_by_name:
3384 * @instance: the instance the signal is being emitted on.
3385 * @detailed_signal: a string of the form "signal-name::detail".
3386 * @...: parameters to be passed to the signal, followed by a
3387 * location for the return value. If the return type of the signal
3388 * is #G_TYPE_NONE, the return value location can be omitted.
3392 * Note that g_signal_emit_by_name() resets the return value to the default
3393 * if no handlers are connected, in contrast to g_signal_emitv().
3396 g_signal_emit_by_name (gpointer instance,
3397 const gchar *detailed_signal,
3403 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3404 g_return_if_fail (detailed_signal != NULL);
3407 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
3414 va_start (var_args, detailed_signal);
3415 g_signal_emit_valist (instance, signal_id, detail, var_args);
3419 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
3423 signal_emit_unlocked_R (SignalNode *node,
3426 GValue *emission_return,
3427 const GValue *instance_and_params)
3429 SignalAccumulator *accumulator;
3431 GClosure *class_closure;
3433 Handler *handler_list = NULL;
3434 GValue *return_accu, accu = G_VALUE_INIT;
3436 gulong max_sequential_handler_number;
3437 gboolean return_value_altered = FALSE;
3439 #ifdef G_ENABLE_DEBUG
3440 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
3442 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
3443 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
3446 if (g_trap_instance_signals == instance)
3449 #endif /* G_ENABLE_DEBUG */
3451 TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3454 signal_id = node->signal_id;
3456 if (node->flags & G_SIGNAL_NO_RECURSE)
3458 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3462 node->state = EMISSION_RESTART;
3464 return return_value_altered;
3467 accumulator = node->accumulator;
3471 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3472 return_accu = &accu;
3476 return_accu = emission_return;
3477 emission.instance = instance;
3478 emission.ihint.signal_id = node->signal_id;
3479 emission.ihint.detail = detail;
3480 emission.ihint.run_type = 0;
3482 emission.chain_type = G_TYPE_NONE;
3483 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3484 class_closure = signal_lookup_closure (node, instance);
3489 handler_unref_R (signal_id, instance, handler_list);
3490 max_sequential_handler_number = g_handler_sequential_number;
3491 hlist = handler_list_lookup (signal_id, instance);
3492 handler_list = hlist ? hlist->handlers : NULL;
3494 handler_ref (handler_list);
3496 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3498 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3500 emission.state = EMISSION_RUN;
3502 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3504 g_closure_invoke (class_closure,
3507 instance_and_params,
3509 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3510 emission.state == EMISSION_RUN)
3511 emission.state = EMISSION_STOP;
3513 emission.chain_type = G_TYPE_NONE;
3514 return_value_altered = TRUE;
3516 if (emission.state == EMISSION_STOP)
3518 else if (emission.state == EMISSION_RESTART)
3522 if (node->emission_hooks)
3524 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3527 emission.state = EMISSION_HOOK;
3528 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3531 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3533 if (!signal_hook->detail || signal_hook->detail == detail)
3535 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3537 was_in_call = G_HOOK_IN_CALL (hook);
3538 hook->flags |= G_HOOK_FLAG_IN_CALL;
3540 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3543 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3545 g_hook_destroy_link (node->emission_hooks, hook);
3547 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3550 if (emission.state == EMISSION_RESTART)
3556 Handler *handler = handler_list;
3558 emission.state = EMISSION_RUN;
3559 handler_ref (handler);
3566 handler_unref_R (signal_id, instance, handler_list);
3567 handler_list = handler;
3570 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3571 handler->sequential_number < max_sequential_handler_number)
3574 g_closure_invoke (handler->closure,
3577 instance_and_params,
3579 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3580 emission.state == EMISSION_RUN)
3581 emission.state = EMISSION_STOP;
3583 return_value_altered = TRUE;
3585 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3588 tmp = handler->next;
3592 handler_unref_R (signal_id, instance, handler_list);
3593 handler_list = handler;
3598 if (emission.state == EMISSION_STOP)
3600 else if (emission.state == EMISSION_RESTART)
3604 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3606 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3608 emission.state = EMISSION_RUN;
3610 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3612 g_closure_invoke (class_closure,
3615 instance_and_params,
3617 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3618 emission.state == EMISSION_RUN)
3619 emission.state = EMISSION_STOP;
3621 emission.chain_type = G_TYPE_NONE;
3622 return_value_altered = TRUE;
3624 if (emission.state == EMISSION_STOP)
3626 else if (emission.state == EMISSION_RESTART)
3632 Handler *handler = handler_list;
3634 emission.state = EMISSION_RUN;
3635 handler_ref (handler);
3640 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3641 handler->sequential_number < max_sequential_handler_number)
3644 g_closure_invoke (handler->closure,
3647 instance_and_params,
3649 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3650 emission.state == EMISSION_RUN)
3651 emission.state = EMISSION_STOP;
3653 return_value_altered = TRUE;
3655 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3658 tmp = handler->next;
3662 handler_unref_R (signal_id, instance, handler);
3667 if (emission.state == EMISSION_STOP)
3669 else if (emission.state == EMISSION_RESTART)
3675 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3677 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3679 gboolean need_unset = FALSE;
3681 emission.state = EMISSION_STOP;
3683 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3685 if (node->return_type != G_TYPE_NONE && !accumulator)
3687 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3690 g_closure_invoke (class_closure,
3691 node->return_type != G_TYPE_NONE ? &accu : NULL,
3693 instance_and_params,
3696 g_value_unset (&accu);
3698 emission.chain_type = G_TYPE_NONE;
3700 if (emission.state == EMISSION_RESTART)
3705 handler_unref_R (signal_id, instance, handler_list);
3707 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3710 g_value_unset (&accu);
3712 TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3714 return return_value_altered;
3718 add_invalid_closure_notify (Handler *handler,
3721 g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3722 handler->has_invalid_closure_notify = 1;
3726 remove_invalid_closure_notify (Handler *handler,
3729 if (handler->has_invalid_closure_notify)
3731 g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3732 handler->has_invalid_closure_notify = 0;
3737 invalid_closure_notify (gpointer instance,
3745 handler = handler_lookup (instance, 0, closure, &signal_id);
3746 g_assert (handler->closure == closure);
3748 handler->sequential_number = 0;
3749 handler->block_count = 1;
3750 handler_unref_R (signal_id, instance, handler);
3756 type_debug_name (GType type)
3760 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3761 return name ? name : "<unknown>";
3768 * g_signal_accumulator_true_handled:
3769 * @ihint: standard #GSignalAccumulator parameter
3770 * @return_accu: standard #GSignalAccumulator parameter
3771 * @handler_return: standard #GSignalAccumulator parameter
3772 * @dummy: standard #GSignalAccumulator parameter
3774 * A predefined #GSignalAccumulator for signals that return a
3775 * boolean values. The behavior that this accumulator gives is
3776 * that a return of %TRUE stops the signal emission: no further
3777 * callbacks will be invoked, while a return of %FALSE allows
3778 * the emission to continue. The idea here is that a %TRUE return
3779 * indicates that the callback <emphasis>handled</emphasis> the signal,
3780 * and no further handling is needed.
3784 * Returns: standard #GSignalAccumulator result
3787 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3788 GValue *return_accu,
3789 const GValue *handler_return,
3792 gboolean continue_emission;
3793 gboolean signal_handled;
3795 signal_handled = g_value_get_boolean (handler_return);
3796 g_value_set_boolean (return_accu, signal_handled);
3797 continue_emission = !signal_handled;
3799 return continue_emission;
3803 * g_signal_accumulator_first_wins:
3804 * @ihint: standard #GSignalAccumulator parameter
3805 * @return_accu: standard #GSignalAccumulator parameter
3806 * @handler_return: standard #GSignalAccumulator parameter
3807 * @dummy: standard #GSignalAccumulator parameter
3809 * A predefined #GSignalAccumulator for signals intended to be used as a
3810 * hook for application code to provide a particular value. Usually
3811 * only one such value is desired and multiple handlers for the same
3812 * signal don't make much sense (except for the case of the default
3813 * handler defined in the class structure, in which case you will
3814 * usually want the signal connection to override the class handler).
3816 * This accumulator will use the return value from the first signal
3817 * handler that is run as the return value for the signal and not run
3818 * any further handlers (ie: the first handler "wins").
3820 * Returns: standard #GSignalAccumulator result
3825 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
3826 GValue *return_accu,
3827 const GValue *handler_return,
3830 g_value_copy (handler_return, return_accu);