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' of type `%s'",
1161 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1164 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1165 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1169 if (emission->state == EMISSION_HOOK)
1170 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1171 node->name, instance);
1172 else if (emission->state == EMISSION_RUN)
1173 emission->state = EMISSION_STOP;
1176 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1177 node->name, instance);
1181 g_warning ("%s: signal `%s' is invalid for instance `%p' of type `%s'",
1182 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1188 * @name: the signal's name.
1189 * @itype: the type that the signal operates on.
1191 * Given the name of the signal and the type of object it connects to, gets
1192 * the signal's identifying integer. Emitting the signal by number is
1193 * somewhat faster than using the name each time.
1195 * Also tries the ancestors of the given type.
1197 * See g_signal_new() for details on allowed signal names.
1199 * Returns: the signal's identifying number, or 0 if no signal was found.
1202 g_signal_lookup (const gchar *name,
1206 g_return_val_if_fail (name != NULL, 0);
1207 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1210 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1214 /* give elaborate warnings */
1215 if (!g_type_name (itype))
1216 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'",
1218 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1219 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1220 name, g_type_name (itype));
1221 else if (!g_type_class_peek (itype))
1222 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1223 name, g_type_name (itype));
1230 * g_signal_list_ids:
1231 * @itype: Instance or interface type.
1232 * @n_ids: Location to store the number of signal ids for @itype.
1234 * Lists the signals by id that a certain instance or interface type
1235 * created. Further information about the signals can be acquired through
1238 * Returns: (array length=n_ids): Newly allocated array of signal IDs.
1241 g_signal_list_ids (GType itype,
1249 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1250 g_return_val_if_fail (n_ids != NULL, NULL);
1253 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1254 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1255 result = g_array_new (FALSE, FALSE, sizeof (guint));
1257 for (i = 0; i < n_nodes; i++)
1258 if (keys[i].itype == itype)
1260 const gchar *name = g_quark_to_string (keys[i].quark);
1262 /* Signal names with "_" in them are aliases to the same
1263 * name with "-" instead of "_".
1265 if (!strchr (name, '_'))
1266 g_array_append_val (result, keys[i].signal_id);
1268 *n_ids = result->len;
1272 /* give elaborate warnings */
1273 if (!g_type_name (itype))
1274 g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'",
1276 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1277 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1278 g_type_name (itype));
1279 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1280 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1281 g_type_name (itype));
1284 return (guint*) g_array_free (result, FALSE);
1289 * @signal_id: the signal's identifying number.
1291 * Given the signal's identifier, finds its name.
1293 * Two different signals may have the same name, if they have differing types.
1295 * Returns: the signal name, or %NULL if the signal number was invalid.
1298 g_signal_name (guint signal_id)
1304 node = LOOKUP_SIGNAL_NODE (signal_id);
1305 name = node ? node->name : NULL;
1308 return (char*) name;
1313 * @signal_id: The signal id of the signal to query information for.
1314 * @query: (out caller-allocates): A user provided structure that is
1315 * filled in with constant values upon success.
1317 * Queries the signal system for in-depth information about a
1318 * specific signal. This function will fill in a user-provided
1319 * structure to hold signal-specific information. If an invalid
1320 * signal id is passed in, the @signal_id member of the #GSignalQuery
1321 * is 0. All members filled into the #GSignalQuery structure should
1322 * be considered constant and have to be left untouched.
1325 g_signal_query (guint signal_id,
1326 GSignalQuery *query)
1330 g_return_if_fail (query != NULL);
1333 node = LOOKUP_SIGNAL_NODE (signal_id);
1334 if (!node || node->destroyed)
1335 query->signal_id = 0;
1338 query->signal_id = node->signal_id;
1339 query->signal_name = node->name;
1340 query->itype = node->itype;
1341 query->signal_flags = node->flags;
1342 query->return_type = node->return_type;
1343 query->n_params = node->n_params;
1344 query->param_types = node->param_types;
1351 * @signal_name: the name for the signal
1352 * @itype: the type this signal pertains to. It will also pertain to
1353 * types which are derived from this type.
1354 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1355 * the default handler is to be invoked. You should at least specify
1356 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1357 * @class_offset: The offset of the function pointer in the class structure
1358 * for this type. Used to invoke a class method generically. Pass 0 to
1359 * not associate a class method slot with this signal.
1360 * @accumulator: the accumulator for this signal; may be %NULL.
1361 * @accu_data: user data for the @accumulator.
1362 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1363 * values to signal emissions into C language callback invocations or %NULL.
1364 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1365 * without a return value.
1366 * @n_params: the number of parameter types to follow.
1367 * @...: a list of types, one for each parameter.
1369 * Creates a new signal. (This is usually done in the class initializer.)
1371 * A signal name consists of segments consisting of ASCII letters and
1372 * digits, separated by either the '-' or '_' character. The first
1373 * character of a signal name must be a letter. Names which violate these
1374 * rules lead to undefined behaviour of the GSignal system.
1376 * When registering a signal and looking up a signal, either separator can
1377 * be used, but they cannot be mixed.
1379 * If 0 is used for @class_offset subclasses cannot override the class handler
1380 * in their <code>class_init</code> method by doing
1381 * <code>super_class->signal_handler = my_signal_handler</code>. Instead they
1382 * will have to use g_signal_override_class_handler().
1384 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1385 * the marshaller for this signal.
1387 * Returns: the signal id
1390 g_signal_new (const gchar *signal_name,
1392 GSignalFlags signal_flags,
1394 GSignalAccumulator accumulator,
1396 GSignalCMarshaller c_marshaller,
1404 g_return_val_if_fail (signal_name != NULL, 0);
1406 va_start (args, n_params);
1408 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1409 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1410 accumulator, accu_data, c_marshaller,
1411 return_type, n_params, args);
1419 * g_signal_new_class_handler:
1420 * @signal_name: the name for the signal
1421 * @itype: the type this signal pertains to. It will also pertain to
1422 * types which are derived from this type.
1423 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1424 * the default handler is to be invoked. You should at least specify
1425 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1426 * @class_handler: a #GCallback which acts as class implementation of
1427 * this signal. Used to invoke a class method generically. Pass %NULL to
1428 * not associate a class method with this signal.
1429 * @accumulator: the accumulator for this signal; may be %NULL.
1430 * @accu_data: user data for the @accumulator.
1431 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1432 * values to signal emissions into C language callback invocations or %NULL.
1433 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1434 * without a return value.
1435 * @n_params: the number of parameter types to follow.
1436 * @...: a list of types, one for each parameter.
1438 * Creates a new signal. (This is usually done in the class initializer.)
1440 * This is a variant of g_signal_new() that takes a C callback instead
1441 * off a class offset for the signal's class handler. This function
1442 * doesn't need a function pointer exposed in the class structure of
1443 * an object definition, instead the function pointer is passed
1444 * directly and can be overriden by derived classes with
1445 * g_signal_override_class_closure() or
1446 * g_signal_override_class_handler()and chained to with
1447 * g_signal_chain_from_overridden() or
1448 * g_signal_chain_from_overridden_handler().
1450 * See g_signal_new() for information about signal names.
1452 * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1453 * the marshaller for this signal.
1455 * Returns: the signal id
1460 g_signal_new_class_handler (const gchar *signal_name,
1462 GSignalFlags signal_flags,
1463 GCallback class_handler,
1464 GSignalAccumulator accumulator,
1466 GSignalCMarshaller c_marshaller,
1474 g_return_val_if_fail (signal_name != NULL, 0);
1476 va_start (args, n_params);
1478 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1479 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1480 accumulator, accu_data, c_marshaller,
1481 return_type, n_params, args);
1488 static inline ClassClosure*
1489 signal_find_class_closure (SignalNode *node,
1492 GBSearchArray *bsa = node->class_closure_bsa;
1499 /* cc->instance_type is 0 for default closure */
1501 key.instance_type = itype;
1502 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1503 while (!cc && key.instance_type)
1505 key.instance_type = g_type_parent (key.instance_type);
1506 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1514 static inline GClosure*
1515 signal_lookup_closure (SignalNode *node,
1516 GTypeInstance *instance)
1520 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1522 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1523 if (cc && cc->instance_type == 0) /* check for default closure */
1526 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1527 return cc ? cc->closure : NULL;
1531 signal_add_class_closure (SignalNode *node,
1537 node->single_va_closure_is_valid = FALSE;
1539 if (!node->class_closure_bsa)
1540 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1541 key.instance_type = itype;
1542 key.closure = g_closure_ref (closure);
1543 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1544 &g_class_closure_bconfig,
1546 g_closure_sink (closure);
1547 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1549 g_closure_set_marshal (closure, node->c_marshaller);
1550 if (node->va_marshaller)
1551 _g_closure_set_va_marshal (closure, node->va_marshaller);
1557 * @signal_name: the name for the signal
1558 * @itype: the type this signal pertains to. It will also pertain to
1559 * types which are derived from this type
1560 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1561 * the default handler is to be invoked. You should at least specify
1562 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1563 * @class_closure: (allow-none): The closure to invoke on signal emission;
1565 * @accumulator: (allow-none): the accumulator for this signal; may be %NULL
1566 * @accu_data: user data for the @accumulator
1567 * @c_marshaller: (allow-none): the function to translate arrays of
1568 * parameter values to signal emissions into C language callback
1569 * invocations or %NULL
1570 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1571 * without a return value
1572 * @n_params: the length of @param_types
1573 * @param_types: (array length=n_params): an array of types, one for
1576 * Creates a new signal. (This is usually done in the class initializer.)
1578 * See g_signal_new() for details on allowed signal names.
1580 * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1581 * the marshaller for this signal.
1583 * Returns: the signal id
1586 g_signal_newv (const gchar *signal_name,
1588 GSignalFlags signal_flags,
1589 GClosure *class_closure,
1590 GSignalAccumulator accumulator,
1592 GSignalCMarshaller c_marshaller,
1600 GSignalCMarshaller builtin_c_marshaller;
1601 GSignalCVaMarshaller va_marshaller;
1603 g_return_val_if_fail (signal_name != NULL, 0);
1604 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1606 g_return_val_if_fail (param_types != NULL, 0);
1607 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1608 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1609 g_return_val_if_fail (accumulator == NULL, 0);
1611 g_return_val_if_fail (accu_data == NULL, 0);
1613 name = g_strdup (signal_name);
1614 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1618 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1619 node = LOOKUP_SIGNAL_NODE (signal_id);
1620 if (node && !node->destroyed)
1622 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1624 type_debug_name (node->itype),
1625 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1630 if (node && node->itype != itype)
1632 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1634 type_debug_name (itype),
1635 type_debug_name (node->itype));
1640 for (i = 0; i < n_params; i++)
1641 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1643 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1644 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1649 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1651 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1652 type_debug_name (return_type), type_debug_name (itype), name);
1657 if (return_type != G_TYPE_NONE &&
1658 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1660 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1661 type_debug_name (itype), name, type_debug_name (return_type));
1667 /* setup permanent portion of signal node */
1672 signal_id = g_n_signal_nodes++;
1673 node = g_new (SignalNode, 1);
1674 node->signal_id = signal_id;
1675 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1676 g_signal_nodes[signal_id] = node;
1677 node->itype = itype;
1680 key.quark = g_quark_from_string (node->name);
1681 key.signal_id = signal_id;
1682 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1683 g_strdelimit (name, "_", '-');
1684 node->name = g_intern_string (name);
1685 key.quark = g_quark_from_string (name);
1686 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1688 TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1690 node->destroyed = FALSE;
1692 /* setup reinitializable portion */
1693 node->single_va_closure_is_valid = FALSE;
1694 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1695 node->n_params = n_params;
1696 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1697 node->return_type = return_type;
1698 node->class_closure_bsa = NULL;
1701 node->accumulator = g_new (SignalAccumulator, 1);
1702 node->accumulator->func = accumulator;
1703 node->accumulator->data = accu_data;
1706 node->accumulator = NULL;
1708 builtin_c_marshaller = NULL;
1709 va_marshaller = NULL;
1711 /* Pick up built-in va marshallers for standard types, and
1712 instead of generic marshaller if no marshaller specified */
1713 if (n_params == 0 && return_type == G_TYPE_NONE)
1715 builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1716 va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1718 else if (n_params == 1 && return_type == G_TYPE_NONE)
1720 #define ADD_CHECK(__type__) \
1721 else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \
1723 builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \
1724 va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \
1747 if (c_marshaller == NULL)
1749 if (builtin_c_marshaller)
1750 c_marshaller = builtin_c_marshaller;
1753 c_marshaller = g_cclosure_marshal_generic;
1754 va_marshaller = g_cclosure_marshal_generic_va;
1758 node->c_marshaller = c_marshaller;
1759 node->va_marshaller = va_marshaller;
1760 node->emission_hooks = NULL;
1762 signal_add_class_closure (node, 0, class_closure);
1772 g_signal_set_va_marshaller (guint signal_id,
1773 GType instance_type,
1774 GSignalCVaMarshaller va_marshaller)
1778 g_return_if_fail (signal_id > 0);
1779 g_return_if_fail (va_marshaller != NULL);
1782 node = LOOKUP_SIGNAL_NODE (signal_id);
1785 node->va_marshaller = va_marshaller;
1786 if (node->class_closure_bsa)
1788 ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1789 if (cc->closure->marshal == node->c_marshaller)
1790 _g_closure_set_va_marshal (cc->closure, va_marshaller);
1793 node->single_va_closure_is_valid = FALSE;
1801 * g_signal_new_valist:
1802 * @signal_name: the name for the signal
1803 * @itype: the type this signal pertains to. It will also pertain to
1804 * types which are derived from this type.
1805 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1806 * the default handler is to be invoked. You should at least specify
1807 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1808 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1809 * @accumulator: the accumulator for this signal; may be %NULL.
1810 * @accu_data: user data for the @accumulator.
1811 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1812 * values to signal emissions into C language callback invocations or %NULL.
1813 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1814 * without a return value.
1815 * @n_params: the number of parameter types in @args.
1816 * @args: va_list of #GType, one for each parameter.
1818 * Creates a new signal. (This is usually done in the class initializer.)
1820 * See g_signal_new() for details on allowed signal names.
1822 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1823 * the marshaller for this signal.
1825 * Returns: the signal id
1828 g_signal_new_valist (const gchar *signal_name,
1830 GSignalFlags signal_flags,
1831 GClosure *class_closure,
1832 GSignalAccumulator accumulator,
1834 GSignalCMarshaller c_marshaller,
1845 param_types = g_new (GType, n_params);
1847 for (i = 0; i < n_params; i++)
1848 param_types[i] = va_arg (args, GType);
1853 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1854 class_closure, accumulator, accu_data, c_marshaller,
1855 return_type, n_params, param_types);
1856 g_free (param_types);
1862 signal_destroy_R (SignalNode *signal_node)
1864 SignalNode node = *signal_node;
1866 signal_node->destroyed = TRUE;
1868 /* reentrancy caution, zero out real contents first */
1869 signal_node->single_va_closure_is_valid = FALSE;
1870 signal_node->n_params = 0;
1871 signal_node->param_types = NULL;
1872 signal_node->return_type = 0;
1873 signal_node->class_closure_bsa = NULL;
1874 signal_node->accumulator = NULL;
1875 signal_node->c_marshaller = NULL;
1876 signal_node->va_marshaller = NULL;
1877 signal_node->emission_hooks = NULL;
1879 #ifdef G_ENABLE_DEBUG
1880 /* check current emissions */
1884 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1885 emission; emission = emission->next)
1886 if (emission->ihint.signal_id == node.signal_id)
1887 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1888 node.name, emission->instance);
1892 /* free contents that need to
1895 g_free (node.param_types);
1896 if (node.class_closure_bsa)
1900 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1902 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1904 g_closure_unref (cc->closure);
1906 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1908 g_free (node.accumulator);
1909 if (node.emission_hooks)
1911 g_hook_list_clear (node.emission_hooks);
1912 g_free (node.emission_hooks);
1918 * g_signal_override_class_closure:
1919 * @signal_id: the signal id
1920 * @instance_type: the instance type on which to override the class closure
1922 * @class_closure: the closure.
1924 * Overrides the class closure (i.e. the default handler) for the given signal
1925 * for emissions on instances of @instance_type. @instance_type must be derived
1926 * from the type to which the signal belongs.
1928 * See g_signal_chain_from_overridden() and
1929 * g_signal_chain_from_overridden_handler() for how to chain up to the
1930 * parent class closure from inside the overridden one.
1933 g_signal_override_class_closure (guint signal_id,
1934 GType instance_type,
1935 GClosure *class_closure)
1939 g_return_if_fail (signal_id > 0);
1940 g_return_if_fail (class_closure != NULL);
1943 node = LOOKUP_SIGNAL_NODE (signal_id);
1944 node_check_deprecated (node);
1945 if (!g_type_is_a (instance_type, node->itype))
1946 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1949 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1951 if (cc && cc->instance_type == instance_type)
1952 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1954 signal_add_class_closure (node, instance_type, class_closure);
1960 * g_signal_override_class_handler:
1961 * @signal_name: the name for the signal
1962 * @instance_type: the instance type on which to override the class handler
1964 * @class_handler: the handler.
1966 * Overrides the class closure (i.e. the default handler) for the
1967 * given signal for emissions on instances of @instance_type with
1968 * callabck @class_handler. @instance_type must be derived from the
1969 * type to which the signal belongs.
1971 * See g_signal_chain_from_overridden() and
1972 * g_signal_chain_from_overridden_handler() for how to chain up to the
1973 * parent class closure from inside the overridden one.
1978 g_signal_override_class_handler (const gchar *signal_name,
1979 GType instance_type,
1980 GCallback class_handler)
1984 g_return_if_fail (signal_name != NULL);
1985 g_return_if_fail (instance_type != G_TYPE_NONE);
1986 g_return_if_fail (class_handler != NULL);
1988 signal_id = g_signal_lookup (signal_name, instance_type);
1991 g_signal_override_class_closure (signal_id, instance_type,
1992 g_cclosure_new (class_handler, NULL, NULL));
1994 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1995 G_STRLOC, signal_name, instance_type);
2000 * g_signal_chain_from_overridden:
2001 * @instance_and_params: (array) the argument list of the signal emission.
2002 * The first element in the array is a #GValue for the instance the signal
2003 * is being emitted on. The rest are any arguments to be passed to the signal.
2004 * @return_value: Location for the return value.
2006 * Calls the original class closure of a signal. This function should only
2007 * be called from an overridden class closure; see
2008 * g_signal_override_class_closure() and
2009 * g_signal_override_class_handler().
2012 g_signal_chain_from_overridden (const GValue *instance_and_params,
2013 GValue *return_value)
2015 GType chain_type = 0, restore_type = 0;
2016 Emission *emission = NULL;
2017 GClosure *closure = NULL;
2021 g_return_if_fail (instance_and_params != NULL);
2022 instance = g_value_peek_pointer (instance_and_params);
2023 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2026 emission = emission_find_innermost (instance);
2029 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2031 g_assert (node != NULL); /* paranoid */
2033 /* we should probably do the same parameter checks as g_signal_emit() here.
2035 if (emission->chain_type != G_TYPE_NONE)
2037 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2039 g_assert (cc != NULL); /* closure currently in call stack */
2041 n_params = node->n_params;
2042 restore_type = cc->instance_type;
2043 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2044 if (cc && cc->instance_type != restore_type)
2046 closure = cc->closure;
2047 chain_type = cc->instance_type;
2051 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
2054 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
2058 emission->chain_type = chain_type;
2060 g_closure_invoke (closure,
2063 instance_and_params,
2066 emission->chain_type = restore_type;
2072 * g_signal_chain_from_overridden_handler:
2073 * @instance: the instance the signal is being emitted on.
2074 * @...: parameters to be passed to the parent class closure, followed by a
2075 * location for the return value. If the return type of the signal
2076 * is #G_TYPE_NONE, the return value location can be omitted.
2078 * Calls the original class closure of a signal. This function should
2079 * only be called from an overridden class closure; see
2080 * g_signal_override_class_closure() and
2081 * g_signal_override_class_handler().
2086 g_signal_chain_from_overridden_handler (gpointer instance,
2089 GType chain_type = 0, restore_type = 0;
2090 Emission *emission = NULL;
2091 GClosure *closure = NULL;
2095 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2098 emission = emission_find_innermost (instance);
2101 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2103 g_assert (node != NULL); /* paranoid */
2105 /* we should probably do the same parameter checks as g_signal_emit() here.
2107 if (emission->chain_type != G_TYPE_NONE)
2109 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2111 g_assert (cc != NULL); /* closure currently in call stack */
2113 n_params = node->n_params;
2114 restore_type = cc->instance_type;
2115 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2116 if (cc && cc->instance_type != restore_type)
2118 closure = cc->closure;
2119 chain_type = cc->instance_type;
2123 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
2126 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
2130 GValue *instance_and_params;
2131 GType signal_return_type;
2132 GValue *param_values;
2136 va_start (var_args, instance);
2138 signal_return_type = node->return_type;
2139 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
2140 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
2141 param_values = instance_and_params + 1;
2143 for (i = 0; i < node->n_params; i++)
2146 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2147 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2150 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2152 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2156 g_warning ("%s: %s", G_STRLOC, error);
2159 /* we purposely leak the value here, it might not be
2160 * in a sane state if an error condition occoured
2163 g_value_unset (param_values + i);
2172 instance_and_params->g_type = 0;
2173 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2174 g_value_set_instance (instance_and_params, instance);
2177 emission->chain_type = chain_type;
2180 if (signal_return_type == G_TYPE_NONE)
2182 g_closure_invoke (closure,
2185 instance_and_params,
2190 GValue return_value = G_VALUE_INIT;
2191 gchar *error = NULL;
2192 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2193 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2195 g_value_init (&return_value, rtype);
2197 g_closure_invoke (closure,
2200 instance_and_params,
2203 G_VALUE_LCOPY (&return_value,
2205 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2209 g_value_unset (&return_value);
2213 g_warning ("%s: %s", G_STRLOC, error);
2216 /* we purposely leak the value here, it might not be
2217 * in a sane state if an error condition occurred
2222 for (i = 0; i < n_params; i++)
2223 g_value_unset (param_values + i);
2224 g_value_unset (instance_and_params);
2229 emission->chain_type = restore_type;
2235 * g_signal_get_invocation_hint:
2236 * @instance: the instance to query
2238 * Returns the invocation hint of the innermost signal emission of instance.
2240 * Returns: (transfer none): the invocation hint of the innermost signal emission.
2242 GSignalInvocationHint*
2243 g_signal_get_invocation_hint (gpointer instance)
2245 Emission *emission = NULL;
2247 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2250 emission = emission_find_innermost (instance);
2253 return emission ? &emission->ihint : NULL;
2257 * g_signal_connect_closure_by_id:
2258 * @instance: the instance to connect to.
2259 * @signal_id: the id of the signal.
2260 * @detail: the detail.
2261 * @closure: the closure to connect.
2262 * @after: whether the handler should be called before or after the
2263 * default handler of the signal.
2265 * Connects a closure to a signal for a particular object.
2267 * Returns: the handler id
2270 g_signal_connect_closure_by_id (gpointer instance,
2277 gulong handler_seq_no = 0;
2279 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2280 g_return_val_if_fail (signal_id > 0, 0);
2281 g_return_val_if_fail (closure != NULL, 0);
2284 node = LOOKUP_SIGNAL_NODE (signal_id);
2287 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2288 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2289 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2290 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2293 Handler *handler = handler_new (after);
2295 handler_seq_no = handler->sequential_number;
2296 handler->detail = detail;
2297 handler->closure = g_closure_ref (closure);
2298 g_closure_sink (closure);
2299 add_invalid_closure_notify (handler, instance);
2300 handler_insert (signal_id, instance, handler);
2301 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2303 g_closure_set_marshal (closure, node->c_marshaller);
2304 if (node->va_marshaller)
2305 _g_closure_set_va_marshal (closure, node->va_marshaller);
2310 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2313 return handler_seq_no;
2317 * g_signal_connect_closure:
2318 * @instance: the instance to connect to.
2319 * @detailed_signal: a string of the form "signal-name::detail".
2320 * @closure: the closure to connect.
2321 * @after: whether the handler should be called before or after the
2322 * default handler of the signal.
2324 * Connects a closure to a signal for a particular object.
2326 * Returns: the handler id
2329 g_signal_connect_closure (gpointer instance,
2330 const gchar *detailed_signal,
2335 gulong handler_seq_no = 0;
2339 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2340 g_return_val_if_fail (detailed_signal != NULL, 0);
2341 g_return_val_if_fail (closure != NULL, 0);
2344 itype = G_TYPE_FROM_INSTANCE (instance);
2345 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2348 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2350 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2351 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2352 else if (!g_type_is_a (itype, node->itype))
2353 g_warning ("%s: signal `%s' is invalid for instance `%p' of type `%s'",
2354 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2357 Handler *handler = handler_new (after);
2359 handler_seq_no = handler->sequential_number;
2360 handler->detail = detail;
2361 handler->closure = g_closure_ref (closure);
2362 g_closure_sink (closure);
2363 add_invalid_closure_notify (handler, instance);
2364 handler_insert (signal_id, instance, handler);
2365 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2367 g_closure_set_marshal (handler->closure, node->c_marshaller);
2368 if (node->va_marshaller)
2369 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2374 g_warning ("%s: signal `%s' is invalid for instance `%p' of type `%s'",
2375 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2378 return handler_seq_no;
2382 node_check_deprecated (const SignalNode *node)
2384 static const gchar * g_enable_diagnostic = NULL;
2386 if (G_UNLIKELY (!g_enable_diagnostic))
2388 g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2389 if (!g_enable_diagnostic)
2390 g_enable_diagnostic = "0";
2393 if (g_enable_diagnostic[0] == '1')
2395 if (node->flags & G_SIGNAL_DEPRECATED)
2397 g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2398 "anymore. It will be removed in a future version.",
2399 type_debug_name (node->itype), node->name);
2405 * g_signal_connect_data:
2406 * @instance: the instance to connect to.
2407 * @detailed_signal: a string of the form "signal-name::detail".
2408 * @c_handler: the #GCallback to connect.
2409 * @data: data to pass to @c_handler calls.
2410 * @destroy_data: a #GClosureNotify for @data.
2411 * @connect_flags: a combination of #GConnectFlags.
2413 * Connects a #GCallback function to a signal for a particular object. Similar
2414 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2415 * which will be called when the signal handler is disconnected and no longer
2416 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
2417 * <literal>..._swapped()</literal> variants of this function.
2419 * Returns: the handler id
2422 g_signal_connect_data (gpointer instance,
2423 const gchar *detailed_signal,
2424 GCallback c_handler,
2426 GClosureNotify destroy_data,
2427 GConnectFlags connect_flags)
2430 gulong handler_seq_no = 0;
2433 gboolean swapped, after;
2435 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2436 g_return_val_if_fail (detailed_signal != NULL, 0);
2437 g_return_val_if_fail (c_handler != NULL, 0);
2439 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2440 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2443 itype = G_TYPE_FROM_INSTANCE (instance);
2444 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2447 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2449 node_check_deprecated (node);
2451 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2452 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2453 else if (!g_type_is_a (itype, node->itype))
2454 g_warning ("%s: signal `%s' is invalid for instance `%p' of type `%s'",
2455 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2458 Handler *handler = handler_new (after);
2460 handler_seq_no = handler->sequential_number;
2461 handler->detail = detail;
2462 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2463 g_closure_sink (handler->closure);
2464 handler_insert (signal_id, instance, handler);
2465 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2467 g_closure_set_marshal (handler->closure, node->c_marshaller);
2468 if (node->va_marshaller)
2469 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2474 g_warning ("%s: signal `%s' is invalid for instance `%p' of type `%s'",
2475 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2478 return handler_seq_no;
2482 * g_signal_handler_block:
2483 * @instance: The instance to block the signal handler of.
2484 * @handler_id: Handler id of the handler to be blocked.
2486 * Blocks a handler of an instance so it will not be called during any
2487 * signal emissions unless it is unblocked again. Thus "blocking" a
2488 * signal handler means to temporarily deactive it, a signal handler
2489 * has to be unblocked exactly the same amount of times it has been
2490 * blocked before to become active again.
2492 * The @handler_id has to be a valid signal handler id, connected to a
2493 * signal of @instance.
2496 g_signal_handler_block (gpointer instance,
2501 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2502 g_return_if_fail (handler_id > 0);
2505 handler = handler_lookup (instance, handler_id, NULL, NULL);
2508 #ifndef G_DISABLE_CHECKS
2509 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2510 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2512 handler->block_count += 1;
2515 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2520 * g_signal_handler_unblock:
2521 * @instance: The instance to unblock the signal handler of.
2522 * @handler_id: Handler id of the handler to be unblocked.
2524 * Undoes the effect of a previous g_signal_handler_block() call. A
2525 * blocked handler is skipped during signal emissions and will not be
2526 * invoked, unblocking it (for exactly the amount of times it has been
2527 * blocked before) reverts its "blocked" state, so the handler will be
2528 * recognized by the signal system and is called upon future or
2529 * currently ongoing signal emissions (since the order in which
2530 * handlers are called during signal emissions is deterministic,
2531 * whether the unblocked handler in question is called as part of a
2532 * currently ongoing emission depends on how far that emission has
2535 * The @handler_id has to be a valid id of a signal handler that is
2536 * connected to a signal of @instance and is currently blocked.
2539 g_signal_handler_unblock (gpointer instance,
2544 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2545 g_return_if_fail (handler_id > 0);
2548 handler = handler_lookup (instance, handler_id, NULL, NULL);
2551 if (handler->block_count)
2552 handler->block_count -= 1;
2554 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2557 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2562 * g_signal_handler_disconnect:
2563 * @instance: The instance to remove the signal handler from.
2564 * @handler_id: Handler id of the handler to be disconnected.
2566 * Disconnects a handler from an instance so it will not be called during
2567 * any future or currently ongoing emissions of the signal it has been
2568 * connected to. The @handler_id becomes invalid and may be reused.
2570 * The @handler_id has to be a valid signal handler id, connected to a
2571 * signal of @instance.
2574 g_signal_handler_disconnect (gpointer instance,
2580 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2581 g_return_if_fail (handler_id > 0);
2584 handler = handler_lookup (instance, handler_id, NULL, &signal_id);
2587 handler->sequential_number = 0;
2588 handler->block_count = 1;
2589 remove_invalid_closure_notify (handler, instance);
2590 handler_unref_R (signal_id, instance, handler);
2593 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2598 * g_signal_handler_is_connected:
2599 * @instance: The instance where a signal handler is sought.
2600 * @handler_id: the handler id.
2602 * Returns whether @handler_id is the id of a handler connected to @instance.
2604 * Returns: whether @handler_id identifies a handler connected to @instance.
2607 g_signal_handler_is_connected (gpointer instance,
2613 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2616 handler = handler_lookup (instance, handler_id, NULL, NULL);
2617 connected = handler != NULL;
2624 g_signal_handlers_destroy (gpointer instance)
2626 GBSearchArray *hlbsa;
2628 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2631 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2636 /* reentrancy caution, delete instance trace first */
2637 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2639 for (i = 0; i < hlbsa->n_nodes; i++)
2641 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2642 Handler *handler = hlist->handlers;
2646 Handler *tmp = handler;
2648 handler = tmp->next;
2649 tmp->block_count = 1;
2650 /* cruel unlink, this works because _all_ handlers vanish */
2653 if (tmp->sequential_number)
2655 remove_invalid_closure_notify (tmp, instance);
2656 tmp->sequential_number = 0;
2657 handler_unref_R (0, NULL, tmp);
2661 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2667 * g_signal_handler_find:
2668 * @instance: The instance owning the signal handler to be found.
2669 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2670 * and/or @data the handler has to match.
2671 * @signal_id: Signal the handler has to be connected to.
2672 * @detail: Signal detail the handler has to be connected to.
2673 * @closure: (allow-none): The closure the handler will invoke.
2674 * @func: The C closure callback of the handler (useless for non-C closures).
2675 * @data: The closure data of the handler's closure.
2677 * Finds the first signal handler that matches certain selection criteria.
2678 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2679 * flags, and the criteria values are passed as arguments.
2680 * The match @mask has to be non-0 for successful matches.
2681 * If no handler was found, 0 is returned.
2683 * Returns: A valid non-0 signal handler id for a successful match.
2686 g_signal_handler_find (gpointer instance,
2687 GSignalMatchType mask,
2694 gulong handler_seq_no = 0;
2696 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2697 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2699 if (mask & G_SIGNAL_MATCH_MASK)
2701 HandlerMatch *mlist;
2704 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2707 handler_seq_no = mlist->handler->sequential_number;
2708 handler_match_free1_R (mlist, instance);
2713 return handler_seq_no;
2717 signal_handlers_foreach_matched_R (gpointer instance,
2718 GSignalMatchType mask,
2724 void (*callback) (gpointer instance,
2725 gulong handler_seq_no))
2727 HandlerMatch *mlist;
2728 guint n_handlers = 0;
2730 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2734 if (mlist->handler->sequential_number)
2737 callback (instance, mlist->handler->sequential_number);
2740 mlist = handler_match_free1_R (mlist, instance);
2747 * g_signal_handlers_block_matched:
2748 * @instance: The instance to block handlers from.
2749 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2750 * and/or @data the handlers have to match.
2751 * @signal_id: Signal the handlers have to be connected to.
2752 * @detail: Signal detail the handlers have to be connected to.
2753 * @closure: (allow-none): The closure the handlers will invoke.
2754 * @func: The C closure callback of the handlers (useless for non-C closures).
2755 * @data: The closure data of the handlers' closures.
2757 * Blocks all handlers on an instance that match a certain selection criteria.
2758 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2759 * flags, and the criteria values are passed as arguments.
2760 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2761 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2762 * If no handlers were found, 0 is returned, the number of blocked handlers
2765 * Returns: The number of handlers that matched.
2768 g_signal_handlers_block_matched (gpointer instance,
2769 GSignalMatchType mask,
2776 guint n_handlers = 0;
2778 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2779 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2781 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2784 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2785 closure, func, data,
2786 g_signal_handler_block);
2794 * g_signal_handlers_unblock_matched:
2795 * @instance: The instance to unblock handlers from.
2796 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2797 * and/or @data the handlers have to match.
2798 * @signal_id: Signal the handlers have to be connected to.
2799 * @detail: Signal detail the handlers have to be connected to.
2800 * @closure: (allow-none): The closure the handlers will invoke.
2801 * @func: The C closure callback of the handlers (useless for non-C closures).
2802 * @data: The closure data of the handlers' closures.
2804 * Unblocks all handlers on an instance that match a certain selection
2805 * criteria. The criteria mask is passed as an OR-ed combination of
2806 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2807 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2808 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2809 * If no handlers were found, 0 is returned, the number of unblocked handlers
2810 * otherwise. The match criteria should not apply to any handlers that are
2811 * not currently blocked.
2813 * Returns: The number of handlers that matched.
2816 g_signal_handlers_unblock_matched (gpointer instance,
2817 GSignalMatchType mask,
2824 guint n_handlers = 0;
2826 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2827 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2829 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2832 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2833 closure, func, data,
2834 g_signal_handler_unblock);
2842 * g_signal_handlers_disconnect_matched:
2843 * @instance: The instance to remove handlers from.
2844 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2845 * and/or @data the handlers have to match.
2846 * @signal_id: Signal the handlers have to be connected to.
2847 * @detail: Signal detail the handlers have to be connected to.
2848 * @closure: (allow-none): The closure the handlers will invoke.
2849 * @func: The C closure callback of the handlers (useless for non-C closures).
2850 * @data: The closure data of the handlers' closures.
2852 * Disconnects all handlers on an instance that match a certain
2853 * selection criteria. The criteria mask is passed as an OR-ed
2854 * combination of #GSignalMatchType flags, and the criteria values are
2855 * passed as arguments. Passing at least one of the
2856 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2857 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2858 * matches. If no handlers were found, 0 is returned, the number of
2859 * disconnected handlers otherwise.
2861 * Returns: The number of handlers that matched.
2864 g_signal_handlers_disconnect_matched (gpointer instance,
2865 GSignalMatchType mask,
2872 guint n_handlers = 0;
2874 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2875 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2877 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2880 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2881 closure, func, data,
2882 g_signal_handler_disconnect);
2890 * g_signal_has_handler_pending:
2891 * @instance: the object whose signal handlers are sought.
2892 * @signal_id: the signal id.
2893 * @detail: the detail.
2894 * @may_be_blocked: whether blocked handlers should count as match.
2896 * Returns whether there are any handlers connected to @instance for the
2897 * given signal id and detail.
2899 * One example of when you might use this is when the arguments to the
2900 * signal are difficult to compute. A class implementor may opt to not
2901 * emit the signal if no one is attached anyway, thus saving the cost
2902 * of building the arguments.
2904 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2908 g_signal_has_handler_pending (gpointer instance,
2911 gboolean may_be_blocked)
2913 HandlerMatch *mlist;
2914 gboolean has_pending;
2916 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2917 g_return_val_if_fail (signal_id > 0, FALSE);
2922 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2924 if (!(node->flags & G_SIGNAL_DETAILED))
2926 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2931 mlist = handlers_find (instance,
2932 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2933 signal_id, detail, NULL, NULL, NULL, TRUE);
2937 handler_match_free1_R (mlist, instance);
2940 has_pending = FALSE;
2948 * @instance_and_params: (array): argument list for the signal emission.
2949 * The first element in the array is a #GValue for the instance the signal
2950 * is being emitted on. The rest are any arguments to be passed to the signal.
2951 * @signal_id: the signal id
2952 * @detail: the detail
2953 * @return_value: Location to store the return value of the signal emission.
2957 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2958 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2961 g_signal_emitv (const GValue *instance_and_params,
2964 GValue *return_value)
2968 #ifdef G_ENABLE_DEBUG
2969 const GValue *param_values;
2973 g_return_if_fail (instance_and_params != NULL);
2974 instance = g_value_peek_pointer (instance_and_params);
2975 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2976 g_return_if_fail (signal_id > 0);
2978 #ifdef G_ENABLE_DEBUG
2979 param_values = instance_and_params + 1;
2983 node = LOOKUP_SIGNAL_NODE (signal_id);
2984 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2986 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2990 #ifdef G_ENABLE_DEBUG
2991 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2993 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2997 for (i = 0; i < node->n_params; i++)
2998 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3000 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
3002 type_debug_name (node->param_types[i]),
3005 G_VALUE_TYPE_NAME (param_values + i));
3009 if (node->return_type != G_TYPE_NONE)
3013 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
3015 type_debug_name (node->return_type),
3020 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3022 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
3024 type_debug_name (node->return_type),
3026 G_VALUE_TYPE_NAME (return_value));
3032 return_value = NULL;
3033 #endif /* G_ENABLE_DEBUG */
3035 /* optimize NOP emissions */
3036 if (!node->single_va_closure_is_valid)
3037 node_update_single_va_closure (node);
3039 if (node->single_va_closure != NULL &&
3040 (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3041 _g_closure_is_void (node->single_va_closure, instance))
3042 #ifdef G_ENABLE_DEBUG
3043 && !COND_DEBUG (SIGNALS, g_trace_instance_signals != instance &&
3044 g_trap_instance_signals == instance)
3045 #endif /* G_ENABLE_DEBUG */
3048 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3049 if (hlist == NULL || hlist->handlers == NULL)
3051 /* nothing to do to emit this signal */
3053 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3059 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
3062 static inline gboolean
3063 accumulate (GSignalInvocationHint *ihint,
3064 GValue *return_accu,
3065 GValue *handler_return,
3066 SignalAccumulator *accumulator)
3068 gboolean continue_emission;
3073 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3074 g_value_reset (handler_return);
3076 return continue_emission;
3080 * g_signal_emit_valist:
3081 * @instance: the instance the signal is being emitted on.
3082 * @signal_id: the signal id
3083 * @detail: the detail
3084 * @var_args: a list of parameters to be passed to the signal, followed by a
3085 * location for the return value. If the return type of the signal
3086 * is #G_TYPE_NONE, the return value location can be omitted.
3090 * Note that g_signal_emit_valist() resets the return value to the default
3091 * if no handlers are connected, in contrast to g_signal_emitv().
3094 g_signal_emit_valist (gpointer instance,
3099 GValue *instance_and_params;
3100 GType signal_return_type;
3101 GValue *param_values;
3105 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3106 g_return_if_fail (signal_id > 0);
3109 node = LOOKUP_SIGNAL_NODE (signal_id);
3110 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3112 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
3116 #ifndef G_DISABLE_CHECKS
3117 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3119 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3123 #endif /* !G_DISABLE_CHECKS */
3125 if (!node->single_va_closure_is_valid)
3126 node_update_single_va_closure (node);
3128 if (node->single_va_closure != NULL
3129 #ifdef G_ENABLE_DEBUG
3130 && !COND_DEBUG (SIGNALS, g_trace_instance_signals != instance &&
3131 g_trap_instance_signals == instance)
3132 #endif /* G_ENABLE_DEBUG */
3135 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3136 Handler *fastpath_handler = NULL;
3138 GClosure *closure = NULL;
3139 gboolean fastpath = TRUE;
3140 GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3142 if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3143 !_g_closure_is_void (node->single_va_closure, instance))
3145 if (_g_closure_supports_invoke_va (node->single_va_closure))
3147 closure = node->single_va_closure;
3148 if (node->single_va_closure_is_after)
3149 run_type = G_SIGNAL_RUN_LAST;
3151 run_type = G_SIGNAL_RUN_FIRST;
3157 for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3159 if (!l->block_count &&
3160 (!l->detail || l->detail == detail))
3162 if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3169 fastpath_handler = l;
3170 closure = l->closure;
3172 run_type = G_SIGNAL_RUN_LAST;
3174 run_type = G_SIGNAL_RUN_FIRST;
3179 if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3185 /* Don't allow no-recurse emission as we might have to restart, which means
3186 we will run multiple handlers and thus must ref all arguments */
3187 if (closure != NULL && (node->flags & (G_SIGNAL_NO_RECURSE)) != 0)
3192 SignalAccumulator *accumulator;
3194 GValue *return_accu, accu = G_VALUE_INIT;
3196 GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3197 GValue emission_return = G_VALUE_INIT;
3198 GType rtype = node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3199 gboolean static_scope = node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3201 signal_id = node->signal_id;
3202 accumulator = node->accumulator;
3203 if (rtype == G_TYPE_NONE)
3205 else if (accumulator)
3206 return_accu = &accu;
3208 return_accu = &emission_return;
3210 emission.instance = instance;
3211 emission.ihint.signal_id = signal_id;
3212 emission.ihint.detail = detail;
3213 emission.ihint.run_type = run_type;
3214 emission.state = EMISSION_RUN;
3215 emission.chain_type = instance_type;
3216 emission_push (&g_recursive_emissions, &emission);
3218 if (fastpath_handler)
3219 handler_ref (fastpath_handler);
3223 TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3225 if (rtype != G_TYPE_NONE)
3226 g_value_init (&emission_return, rtype);
3229 g_value_init (&accu, rtype);
3231 if (closure != NULL)
3233 g_object_ref (instance);
3234 _g_closure_invoke_va (closure,
3240 accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3245 emission.chain_type = G_TYPE_NONE;
3246 emission_pop (&g_recursive_emissions, &emission);
3248 if (fastpath_handler)
3249 handler_unref_R (signal_id, instance, fastpath_handler);
3254 g_value_unset (&accu);
3256 if (rtype != G_TYPE_NONE)
3258 gchar *error = NULL;
3259 for (i = 0; i < node->n_params; i++)
3261 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3262 G_VALUE_COLLECT_SKIP (ptype, var_args);
3265 G_VALUE_LCOPY (&emission_return,
3267 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3270 g_value_unset (&emission_return);
3273 g_warning ("%s: %s", G_STRLOC, error);
3275 /* we purposely leak the value here, it might not be
3276 * in a sane state if an error condition occurred
3281 TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3283 if (closure != NULL)
3284 g_object_unref (instance);
3290 n_params = node->n_params;
3291 signal_return_type = node->return_type;
3292 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
3293 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
3294 param_values = instance_and_params + 1;
3296 for (i = 0; i < node->n_params; i++)
3299 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3300 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3303 G_VALUE_COLLECT_INIT (param_values + i, ptype,
3305 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3309 g_warning ("%s: %s", G_STRLOC, error);
3312 /* we purposely leak the value here, it might not be
3313 * in a sane state if an error condition occoured
3316 g_value_unset (param_values + i);
3324 instance_and_params->g_type = 0;
3325 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
3326 g_value_set_instance (instance_and_params, instance);
3327 if (signal_return_type == G_TYPE_NONE)
3328 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
3331 GValue return_value = G_VALUE_INIT;
3332 gchar *error = NULL;
3333 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3334 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3336 g_value_init (&return_value, rtype);
3338 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3340 G_VALUE_LCOPY (&return_value,
3342 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3345 g_value_unset (&return_value);
3348 g_warning ("%s: %s", G_STRLOC, error);
3351 /* we purposely leak the value here, it might not be
3352 * in a sane state if an error condition occurred
3356 for (i = 0; i < n_params; i++)
3357 g_value_unset (param_values + i);
3358 g_value_unset (instance_and_params);
3363 * @instance: the instance the signal is being emitted on.
3364 * @signal_id: the signal id
3365 * @detail: the detail
3366 * @...: parameters to be passed to the signal, followed by a
3367 * location for the return value. If the return type of the signal
3368 * is #G_TYPE_NONE, the return value location can be omitted.
3372 * Note that g_signal_emit() resets the return value to the default
3373 * if no handlers are connected, in contrast to g_signal_emitv().
3376 g_signal_emit (gpointer instance,
3383 va_start (var_args, detail);
3384 g_signal_emit_valist (instance, signal_id, detail, var_args);
3389 * g_signal_emit_by_name:
3390 * @instance: the instance the signal is being emitted on.
3391 * @detailed_signal: a string of the form "signal-name::detail".
3392 * @...: parameters to be passed to the signal, followed by a
3393 * location for the return value. If the return type of the signal
3394 * is #G_TYPE_NONE, the return value location can be omitted.
3398 * Note that g_signal_emit_by_name() resets the return value to the default
3399 * if no handlers are connected, in contrast to g_signal_emitv().
3402 g_signal_emit_by_name (gpointer instance,
3403 const gchar *detailed_signal,
3410 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3411 g_return_if_fail (detailed_signal != NULL);
3413 itype = G_TYPE_FROM_INSTANCE (instance);
3416 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3423 va_start (var_args, detailed_signal);
3424 g_signal_emit_valist (instance, signal_id, detail, var_args);
3428 g_warning ("%s: signal name `%s' is invalid for instance `%p' of type `%s'",
3429 G_STRLOC, detailed_signal, instance, g_type_name (itype));
3433 signal_emit_unlocked_R (SignalNode *node,
3436 GValue *emission_return,
3437 const GValue *instance_and_params)
3439 SignalAccumulator *accumulator;
3441 GClosure *class_closure;
3443 Handler *handler_list = NULL;
3444 GValue *return_accu, accu = G_VALUE_INIT;
3446 gulong max_sequential_handler_number;
3447 gboolean return_value_altered = FALSE;
3449 #ifdef G_ENABLE_DEBUG
3450 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
3452 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
3453 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
3456 if (g_trap_instance_signals == instance)
3459 #endif /* G_ENABLE_DEBUG */
3461 TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3464 signal_id = node->signal_id;
3466 if (node->flags & G_SIGNAL_NO_RECURSE)
3468 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3472 node->state = EMISSION_RESTART;
3474 return return_value_altered;
3477 accumulator = node->accumulator;
3481 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3482 return_accu = &accu;
3486 return_accu = emission_return;
3487 emission.instance = instance;
3488 emission.ihint.signal_id = node->signal_id;
3489 emission.ihint.detail = detail;
3490 emission.ihint.run_type = 0;
3492 emission.chain_type = G_TYPE_NONE;
3493 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3494 class_closure = signal_lookup_closure (node, instance);
3499 handler_unref_R (signal_id, instance, handler_list);
3500 max_sequential_handler_number = g_handler_sequential_number;
3501 hlist = handler_list_lookup (signal_id, instance);
3502 handler_list = hlist ? hlist->handlers : NULL;
3504 handler_ref (handler_list);
3506 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3508 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3510 emission.state = EMISSION_RUN;
3512 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3514 g_closure_invoke (class_closure,
3517 instance_and_params,
3519 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3520 emission.state == EMISSION_RUN)
3521 emission.state = EMISSION_STOP;
3523 emission.chain_type = G_TYPE_NONE;
3524 return_value_altered = TRUE;
3526 if (emission.state == EMISSION_STOP)
3528 else if (emission.state == EMISSION_RESTART)
3532 if (node->emission_hooks)
3534 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3537 emission.state = EMISSION_HOOK;
3538 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3541 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3543 if (!signal_hook->detail || signal_hook->detail == detail)
3545 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3547 was_in_call = G_HOOK_IN_CALL (hook);
3548 hook->flags |= G_HOOK_FLAG_IN_CALL;
3550 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3553 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3555 g_hook_destroy_link (node->emission_hooks, hook);
3557 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3560 if (emission.state == EMISSION_RESTART)
3566 Handler *handler = handler_list;
3568 emission.state = EMISSION_RUN;
3569 handler_ref (handler);
3576 handler_unref_R (signal_id, instance, handler_list);
3577 handler_list = handler;
3580 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3581 handler->sequential_number < max_sequential_handler_number)
3584 g_closure_invoke (handler->closure,
3587 instance_and_params,
3589 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3590 emission.state == EMISSION_RUN)
3591 emission.state = EMISSION_STOP;
3593 return_value_altered = TRUE;
3595 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3598 tmp = handler->next;
3602 handler_unref_R (signal_id, instance, handler_list);
3603 handler_list = handler;
3608 if (emission.state == EMISSION_STOP)
3610 else if (emission.state == EMISSION_RESTART)
3614 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3616 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3618 emission.state = EMISSION_RUN;
3620 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3622 g_closure_invoke (class_closure,
3625 instance_and_params,
3627 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3628 emission.state == EMISSION_RUN)
3629 emission.state = EMISSION_STOP;
3631 emission.chain_type = G_TYPE_NONE;
3632 return_value_altered = TRUE;
3634 if (emission.state == EMISSION_STOP)
3636 else if (emission.state == EMISSION_RESTART)
3642 Handler *handler = handler_list;
3644 emission.state = EMISSION_RUN;
3645 handler_ref (handler);
3650 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3651 handler->sequential_number < max_sequential_handler_number)
3654 g_closure_invoke (handler->closure,
3657 instance_and_params,
3659 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3660 emission.state == EMISSION_RUN)
3661 emission.state = EMISSION_STOP;
3663 return_value_altered = TRUE;
3665 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3668 tmp = handler->next;
3672 handler_unref_R (signal_id, instance, handler);
3677 if (emission.state == EMISSION_STOP)
3679 else if (emission.state == EMISSION_RESTART)
3685 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3687 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3689 gboolean need_unset = FALSE;
3691 emission.state = EMISSION_STOP;
3693 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3695 if (node->return_type != G_TYPE_NONE && !accumulator)
3697 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3700 g_closure_invoke (class_closure,
3701 node->return_type != G_TYPE_NONE ? &accu : NULL,
3703 instance_and_params,
3706 g_value_unset (&accu);
3708 emission.chain_type = G_TYPE_NONE;
3710 if (emission.state == EMISSION_RESTART)
3715 handler_unref_R (signal_id, instance, handler_list);
3717 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3720 g_value_unset (&accu);
3722 TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3724 return return_value_altered;
3728 add_invalid_closure_notify (Handler *handler,
3731 g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3732 handler->has_invalid_closure_notify = 1;
3736 remove_invalid_closure_notify (Handler *handler,
3739 if (handler->has_invalid_closure_notify)
3741 g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3742 handler->has_invalid_closure_notify = 0;
3747 invalid_closure_notify (gpointer instance,
3755 handler = handler_lookup (instance, 0, closure, &signal_id);
3756 g_assert (handler->closure == closure);
3758 handler->sequential_number = 0;
3759 handler->block_count = 1;
3760 handler_unref_R (signal_id, instance, handler);
3766 type_debug_name (GType type)
3770 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3771 return name ? name : "<unknown>";
3778 * g_signal_accumulator_true_handled:
3779 * @ihint: standard #GSignalAccumulator parameter
3780 * @return_accu: standard #GSignalAccumulator parameter
3781 * @handler_return: standard #GSignalAccumulator parameter
3782 * @dummy: standard #GSignalAccumulator parameter
3784 * A predefined #GSignalAccumulator for signals that return a
3785 * boolean values. The behavior that this accumulator gives is
3786 * that a return of %TRUE stops the signal emission: no further
3787 * callbacks will be invoked, while a return of %FALSE allows
3788 * the emission to continue. The idea here is that a %TRUE return
3789 * indicates that the callback <emphasis>handled</emphasis> the signal,
3790 * and no further handling is needed.
3794 * Returns: standard #GSignalAccumulator result
3797 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3798 GValue *return_accu,
3799 const GValue *handler_return,
3802 gboolean continue_emission;
3803 gboolean signal_handled;
3805 signal_handled = g_value_get_boolean (handler_return);
3806 g_value_set_boolean (return_accu, signal_handled);
3807 continue_emission = !signal_handled;
3809 return continue_emission;
3813 * g_signal_accumulator_first_wins:
3814 * @ihint: standard #GSignalAccumulator parameter
3815 * @return_accu: standard #GSignalAccumulator parameter
3816 * @handler_return: standard #GSignalAccumulator parameter
3817 * @dummy: standard #GSignalAccumulator parameter
3819 * A predefined #GSignalAccumulator for signals intended to be used as a
3820 * hook for application code to provide a particular value. Usually
3821 * only one such value is desired and multiple handlers for the same
3822 * signal don't make much sense (except for the case of the default
3823 * handler defined in the class structure, in which case you will
3824 * usually want the signal connection to override the class handler).
3826 * This accumulator will use the return value from the first signal
3827 * handler that is run as the return value for the signal and not run
3828 * any further handlers (ie: the first handler "wins").
3830 * Returns: standard #GSignalAccumulator result
3835 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
3836 GValue *return_accu,
3837 const GValue *handler_return,
3840 g_value_copy (handler_return, return_accu);