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, see <http://www.gnu.org/licenses/>.
17 * this code is based on the original GtkSignal implementation
18 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
31 #include "gtype-private.h"
32 #include "gbsearcharray.h"
33 #include "gvaluecollector.h"
34 #include "gvaluetypes.h"
37 #include "gobject_trace.h"
42 * @short_description: A means for customization of object behaviour
43 * and a general purpose notification mechanism
46 * The basic concept of the signal system is that of the emission
47 * of a signal. Signals are introduced per-type and are identified
48 * through strings. Signals introduced for a parent type are available
49 * in derived types as well, so basically they are a per-type facility
52 * A signal emission mainly involves invocation of a certain set of
53 * callbacks in precisely defined manner. There are two main categories
54 * of such callbacks, per-object ones and user provided ones.
55 * (Although signals can deal with any kind of instantiatable type, I'm
56 * referring to those types as "object types" in the following, simply
57 * because that is the context most users will encounter signals in.)
58 * The per-object callbacks are most often referred to as "object method
59 * handler" or "default (signal) handler", while user provided callbacks are
60 * usually just called "signal handler".
62 * The object method handler is provided at signal creation time (this most
63 * frequently happens at the end of an object class' creation), while user
64 * provided handlers are frequently connected and disconnected to/from a
65 * certain signal on certain object instances.
67 * A signal emission consists of five stages, unless prematurely stopped:
69 * <varlistentry><term></term><listitem><para>
70 * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
71 * </para></listitem></varlistentry>
72 * <varlistentry><term></term><listitem><para>
73 * 2 - Invocation of normal user-provided signal handlers (where the @after flag is not set)
74 * </para></listitem></varlistentry>
75 * <varlistentry><term></term><listitem><para>
76 * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
77 * </para></listitem></varlistentry>
78 * <varlistentry><term></term><listitem><para>
79 * 4 - Invocation of user provided signal handlers (where the @after flag is set)
80 * </para></listitem></varlistentry>
81 * <varlistentry><term></term><listitem><para>
82 * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
83 * </para></listitem></varlistentry>
85 * The user-provided signal handlers are called in the order they were
88 * All handlers may prematurely stop a signal emission, and any number of
89 * 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.
95 * First, user handlers may be blocked. Blocked handlers are omitted during
96 * callback invocation, to return from the blocked state, a handler has to
97 * get unblocked exactly the same amount of times it has been blocked before.
99 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
100 * @detail argument passed in to g_signal_emit() has to match the detail
101 * argument of the signal handler currently subject to invocation.
102 * Specification of no detail argument for signal handlers (omission of the
103 * detail part of the signal specification upon connection) serves as a
104 * wildcard and matches any detail argument passed in to emission.
108 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
110 /* --- typedefs --- */
111 typedef struct _SignalNode SignalNode;
112 typedef struct _SignalKey SignalKey;
113 typedef struct _Emission Emission;
114 typedef struct _Handler Handler;
115 typedef struct _HandlerList HandlerList;
116 typedef struct _HandlerMatch HandlerMatch;
126 /* --- prototypes --- */
127 static inline guint signal_id_lookup (GQuark quark,
129 static void signal_destroy_R (SignalNode *signal_node);
130 static inline HandlerList* handler_list_ensure (guint signal_id,
132 static inline HandlerList* handler_list_lookup (guint signal_id,
134 static inline Handler* handler_new (gboolean after);
135 static void handler_insert (guint signal_id,
138 static Handler* handler_lookup (gpointer instance,
142 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
145 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
147 static HandlerMatch* handlers_find (gpointer instance,
148 GSignalMatchType mask,
154 gboolean one_and_only);
155 static inline void handler_ref (Handler *handler);
156 static inline void handler_unref_R (guint signal_id,
159 static gint handler_lists_cmp (gconstpointer node1,
160 gconstpointer node2);
161 static inline void emission_push (Emission **emission_list_p,
163 static inline void emission_pop (Emission **emission_list_p,
165 static inline Emission* emission_find (Emission *emission_list,
169 static gint class_closures_cmp (gconstpointer node1,
170 gconstpointer node2);
171 static gint signal_key_cmp (gconstpointer node1,
172 gconstpointer node2);
173 static gboolean signal_emit_unlocked_R (SignalNode *node,
176 GValue *return_value,
177 const GValue *instance_and_params);
178 static void add_invalid_closure_notify (Handler *handler,
180 static void remove_invalid_closure_notify (Handler *handler,
182 static void invalid_closure_notify (gpointer data,
184 static const gchar * type_debug_name (GType type);
185 static void node_check_deprecated (const SignalNode *node);
186 static void node_update_single_va_closure (SignalNode *node);
189 /* --- structures --- */
192 GSignalAccumulator func;
200 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
204 /* permanent portion */
210 /* reinitializable portion */
213 guint single_va_closure_is_valid : 1;
214 guint single_va_closure_is_after : 1;
215 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
216 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
217 GBSearchArray *class_closure_bsa;
218 SignalAccumulator *accumulator;
219 GSignalCMarshaller c_marshaller;
220 GSignalCVaMarshaller va_marshaller;
221 GHookList *emission_hooks;
223 GClosure *single_va_closure;
226 #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1) /* indicates single_va_closure is valid but empty */
239 GSignalInvocationHint ihint;
248 Handler *tail_before; /* normal signal handlers are appended here */
249 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
254 gulong sequential_number;
259 guint block_count : 16;
260 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
262 guint has_invalid_closure_notify : 1;
274 GType instance_type; /* 0 for default closure */
279 /* --- variables --- */
280 static GBSearchArray *g_signal_key_bsa = NULL;
281 static const GBSearchConfig g_signal_key_bconfig = {
284 G_BSEARCH_ARRAY_ALIGN_POWER2,
286 static GBSearchConfig g_signal_hlbsa_bconfig = {
287 sizeof (HandlerList),
291 static GBSearchConfig g_class_closure_bconfig = {
292 sizeof (ClassClosure),
296 static GHashTable *g_handler_list_bsa_ht = NULL;
297 static Emission *g_recursive_emissions = NULL;
298 static Emission *g_restart_emissions = NULL;
299 static gulong g_handler_sequential_number = 1;
300 G_LOCK_DEFINE_STATIC (g_signal_mutex);
301 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
302 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
305 /* --- signal nodes --- */
306 static guint g_n_signal_nodes = 0;
307 static SignalNode **g_signal_nodes = NULL;
309 static inline SignalNode*
310 LOOKUP_SIGNAL_NODE (register guint signal_id)
312 if (signal_id < g_n_signal_nodes)
313 return g_signal_nodes[signal_id];
319 /* --- functions --- */
321 signal_id_lookup (GQuark quark,
324 GType *ifaces, type = itype;
330 /* try looking up signals for this type and its ancestors */
333 SignalKey *signal_key;
336 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
339 return signal_key->signal_id;
341 type = g_type_parent (type);
345 /* no luck, try interfaces it exports */
346 ifaces = g_type_interfaces (itype, &n_ifaces);
349 SignalKey *signal_key;
351 key.itype = ifaces[n_ifaces];
352 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
357 return signal_key->signal_id;
366 class_closures_cmp (gconstpointer node1,
369 const ClassClosure *c1 = node1, *c2 = node2;
371 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
375 handler_lists_cmp (gconstpointer node1,
378 const HandlerList *hlist1 = node1, *hlist2 = node2;
380 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
383 static inline HandlerList*
384 handler_list_ensure (guint signal_id,
387 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
390 key.signal_id = signal_id;
392 key.tail_before = NULL;
393 key.tail_after = NULL;
396 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
397 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
398 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
402 GBSearchArray *o = hlbsa;
404 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
406 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
408 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
411 static inline HandlerList*
412 handler_list_lookup (guint signal_id,
415 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
418 key.signal_id = signal_id;
420 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
424 handler_lookup (gpointer instance,
429 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
435 for (i = 0; i < hlbsa->n_nodes; i++)
437 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
440 for (handler = hlist->handlers; handler; handler = handler->next)
441 if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id))
444 *signal_id_p = hlist->signal_id;
454 static inline HandlerMatch*
455 handler_match_prepend (HandlerMatch *list,
461 node = g_slice_new (HandlerMatch);
462 node->handler = handler;
464 node->signal_id = signal_id;
465 handler_ref (handler);
469 static inline HandlerMatch*
470 handler_match_free1_R (HandlerMatch *node,
473 HandlerMatch *next = node->next;
475 handler_unref_R (node->signal_id, instance, node->handler);
476 g_slice_free (HandlerMatch, node);
482 handlers_find (gpointer instance,
483 GSignalMatchType mask,
489 gboolean one_and_only)
491 HandlerMatch *mlist = NULL;
493 if (mask & G_SIGNAL_MATCH_ID)
495 HandlerList *hlist = handler_list_lookup (signal_id, instance);
497 SignalNode *node = NULL;
499 if (mask & G_SIGNAL_MATCH_FUNC)
501 node = LOOKUP_SIGNAL_NODE (signal_id);
502 if (!node || !node->c_marshaller)
507 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
508 if (handler->sequential_number &&
509 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
510 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
511 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
512 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
513 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
514 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
515 ((GCClosure*) handler->closure)->callback == func)))
517 mlist = handler_match_prepend (mlist, handler, signal_id);
524 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
531 for (i = 0; i < hlbsa->n_nodes; i++)
533 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
534 SignalNode *node = NULL;
537 if (!(mask & G_SIGNAL_MATCH_FUNC))
539 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
540 if (!node->c_marshaller)
544 for (handler = hlist->handlers; handler; handler = handler->next)
545 if (handler->sequential_number &&
546 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
547 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
548 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
549 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
550 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
551 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
552 ((GCClosure*) handler->closure)->callback == func)))
554 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
565 static inline Handler*
566 handler_new (gboolean after)
568 Handler *handler = g_slice_new (Handler);
569 #ifndef G_DISABLE_CHECKS
570 if (g_handler_sequential_number < 1)
571 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
574 handler->sequential_number = g_handler_sequential_number++;
575 handler->prev = NULL;
576 handler->next = NULL;
578 handler->ref_count = 1;
579 handler->block_count = 0;
580 handler->after = after != FALSE;
581 handler->closure = NULL;
582 handler->has_invalid_closure_notify = 0;
588 handler_ref (Handler *handler)
590 g_return_if_fail (handler->ref_count > 0);
592 handler->ref_count++;
596 handler_unref_R (guint signal_id,
600 g_return_if_fail (handler->ref_count > 0);
602 handler->ref_count--;
604 if (G_UNLIKELY (handler->ref_count == 0))
606 HandlerList *hlist = NULL;
609 handler->next->prev = handler->prev;
610 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
611 handler->prev->next = handler->next;
614 hlist = handler_list_lookup (signal_id, instance);
615 hlist->handlers = handler->next;
620 /* check if we are removing the handler pointed to by tail_before */
621 if (!handler->after && (!handler->next || handler->next->after))
624 hlist = handler_list_lookup (signal_id, instance);
627 g_assert (hlist->tail_before == handler); /* paranoid */
628 hlist->tail_before = handler->prev;
632 /* check if we are removing the handler pointed to by tail_after */
636 hlist = handler_list_lookup (signal_id, instance);
639 g_assert (hlist->tail_after == handler); /* paranoid */
640 hlist->tail_after = handler->prev;
646 g_closure_unref (handler->closure);
648 g_slice_free (Handler, handler);
653 handler_insert (guint signal_id,
659 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
661 hlist = handler_list_ensure (signal_id, instance);
662 if (!hlist->handlers)
664 hlist->handlers = handler;
666 hlist->tail_before = handler;
668 else if (handler->after)
670 handler->prev = hlist->tail_after;
671 hlist->tail_after->next = handler;
675 if (hlist->tail_before)
677 handler->next = hlist->tail_before->next;
679 handler->next->prev = handler;
680 handler->prev = hlist->tail_before;
681 hlist->tail_before->next = handler;
683 else /* insert !after handler into a list of only after handlers */
685 handler->next = hlist->handlers;
687 handler->next->prev = handler;
688 hlist->handlers = handler;
690 hlist->tail_before = handler;
694 hlist->tail_after = handler;
698 node_update_single_va_closure (SignalNode *node)
700 GClosure *closure = NULL;
701 gboolean is_after = FALSE;
703 /* Fast path single-handler without boxing the arguments in GValues */
704 if (G_TYPE_IS_OBJECT (node->itype) &&
705 (node->flags & (G_SIGNAL_MUST_COLLECT)) == 0 &&
706 (node->emission_hooks == NULL || node->emission_hooks->hooks == NULL))
708 GSignalFlags run_type;
710 GBSearchArray *bsa = node->class_closure_bsa;
712 if (bsa == NULL || bsa->n_nodes == 0)
713 closure = SINGLE_VA_CLOSURE_EMPTY_MAGIC;
714 else if (bsa->n_nodes == 1)
716 /* Look for default class closure (can't support non-default as it
717 chains up using GValues */
718 cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
719 if (cc->instance_type == 0)
721 run_type = node->flags & (G_SIGNAL_RUN_FIRST|G_SIGNAL_RUN_LAST|G_SIGNAL_RUN_CLEANUP);
722 /* Only support *one* of run-first or run-last, not multiple or cleanup */
723 if (run_type == G_SIGNAL_RUN_FIRST ||
724 run_type == G_SIGNAL_RUN_LAST)
726 closure = cc->closure;
727 is_after = (run_type == G_SIGNAL_RUN_LAST);
733 node->single_va_closure_is_valid = TRUE;
734 node->single_va_closure = closure;
735 node->single_va_closure_is_after = is_after;
739 emission_push (Emission **emission_list_p,
742 emission->next = *emission_list_p;
743 *emission_list_p = emission;
747 emission_pop (Emission **emission_list_p,
750 Emission *node, *last = NULL;
752 for (node = *emission_list_p; node; last = node, node = last->next)
753 if (node == emission)
756 last->next = node->next;
758 *emission_list_p = node->next;
761 g_assert_not_reached ();
764 static inline Emission*
765 emission_find (Emission *emission_list,
772 for (emission = emission_list; emission; emission = emission->next)
773 if (emission->instance == instance &&
774 emission->ihint.signal_id == signal_id &&
775 emission->ihint.detail == detail)
780 static inline Emission*
781 emission_find_innermost (gpointer instance)
783 Emission *emission, *s = NULL, *c = NULL;
785 for (emission = g_restart_emissions; emission; emission = emission->next)
786 if (emission->instance == instance)
791 for (emission = g_recursive_emissions; emission; emission = emission->next)
792 if (emission->instance == instance)
802 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
806 signal_key_cmp (gconstpointer node1,
809 const SignalKey *key1 = node1, *key2 = node2;
811 if (key1->itype == key2->itype)
812 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
814 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
818 _g_signal_init (void)
821 if (!g_n_signal_nodes)
823 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
824 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
825 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
827 /* invalid (0) signal_id */
828 g_n_signal_nodes = 1;
829 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
830 g_signal_nodes[0] = NULL;
836 _g_signals_destroy (GType itype)
841 for (i = 1; i < g_n_signal_nodes; i++)
843 SignalNode *node = g_signal_nodes[i];
845 if (node->itype == itype)
848 g_warning (G_STRLOC ": signal \"%s\" of type '%s' already destroyed",
850 type_debug_name (node->itype));
852 signal_destroy_R (node);
859 * g_signal_stop_emission:
860 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
861 * @signal_id: the signal identifier, as returned by g_signal_lookup().
862 * @detail: the detail which the signal was emitted with.
864 * Stops a signal's current emission.
866 * This will prevent the default method from running, if the signal was
867 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
870 * Prints a warning if used on a signal which isn't being emitted.
873 g_signal_stop_emission (gpointer instance,
879 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
880 g_return_if_fail (signal_id > 0);
883 node = LOOKUP_SIGNAL_NODE (signal_id);
884 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
886 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
890 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
892 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
893 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
897 if (emission->state == EMISSION_HOOK)
898 g_warning (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
899 node->name, instance);
900 else if (emission->state == EMISSION_RUN)
901 emission->state = EMISSION_STOP;
904 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
905 node->name, instance);
908 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
913 signal_finalize_hook (GHookList *hook_list,
916 GDestroyNotify destroy = hook->destroy;
920 hook->destroy = NULL;
922 destroy (hook->data);
928 * g_signal_add_emission_hook:
929 * @signal_id: the signal identifier, as returned by g_signal_lookup().
930 * @detail: the detail on which to call the hook.
931 * @hook_func: a #GSignalEmissionHook function.
932 * @hook_data: user data for @hook_func.
933 * @data_destroy: a #GDestroyNotify for @hook_data.
935 * Adds an emission hook for a signal, which will get called for any emission
936 * of that signal, independent of the instance. This is possible only
937 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
939 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
942 g_signal_add_emission_hook (guint signal_id,
944 GSignalEmissionHook hook_func,
946 GDestroyNotify data_destroy)
948 static gulong seq_hook_id = 1;
951 SignalHook *signal_hook;
953 g_return_val_if_fail (signal_id > 0, 0);
954 g_return_val_if_fail (hook_func != NULL, 0);
957 node = LOOKUP_SIGNAL_NODE (signal_id);
958 if (!node || node->destroyed)
960 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
964 if (node->flags & G_SIGNAL_NO_HOOKS)
966 g_warning ("%s: signal id '%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
970 if (detail && !(node->flags & G_SIGNAL_DETAILED))
972 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
976 node->single_va_closure_is_valid = FALSE;
977 if (!node->emission_hooks)
979 node->emission_hooks = g_new (GHookList, 1);
980 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
981 node->emission_hooks->finalize_hook = signal_finalize_hook;
984 node_check_deprecated (node);
986 hook = g_hook_alloc (node->emission_hooks);
987 hook->data = hook_data;
988 hook->func = (gpointer) hook_func;
989 hook->destroy = data_destroy;
990 signal_hook = SIGNAL_HOOK (hook);
991 signal_hook->detail = detail;
992 node->emission_hooks->seq_id = seq_hook_id;
993 g_hook_append (node->emission_hooks, hook);
994 seq_hook_id = node->emission_hooks->seq_id;
998 return hook->hook_id;
1002 * g_signal_remove_emission_hook:
1003 * @signal_id: the id of the signal
1004 * @hook_id: the id of the emission hook, as returned by
1005 * g_signal_add_emission_hook()
1007 * Deletes an emission hook.
1010 g_signal_remove_emission_hook (guint signal_id,
1015 g_return_if_fail (signal_id > 0);
1016 g_return_if_fail (hook_id > 0);
1019 node = LOOKUP_SIGNAL_NODE (signal_id);
1020 if (!node || node->destroyed)
1022 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1025 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
1026 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
1028 node->single_va_closure_is_valid = FALSE;
1035 signal_parse_name (const gchar *name,
1038 gboolean force_quark)
1040 const gchar *colon = strchr (name, ':');
1045 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1046 if (signal_id && detail_p)
1049 else if (colon[1] == ':')
1052 guint l = colon - name;
1056 memcpy (buffer, name, l);
1058 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1062 gchar *signal = g_new (gchar, l + 1);
1064 memcpy (signal, name, l);
1066 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1070 if (signal_id && detail_p)
1071 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1079 * g_signal_parse_name:
1080 * @detailed_signal: a string of the form "signal-name::detail".
1081 * @itype: The interface/instance type that introduced "signal-name".
1082 * @signal_id_p: (out): Location to store the signal id.
1083 * @detail_p: (out): Location to store the detail quark.
1084 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1086 * Internal function to parse a signal name into its @signal_id
1087 * and @detail quark.
1089 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1092 g_signal_parse_name (const gchar *detailed_signal,
1096 gboolean force_detail_quark)
1102 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1103 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1106 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1109 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1110 if (!node || node->destroyed ||
1111 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1115 *signal_id_p = signal_id;
1123 * g_signal_stop_emission_by_name:
1124 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
1125 * @detailed_signal: a string of the form "signal-name::detail".
1127 * Stops a signal's current emission.
1129 * This is just like g_signal_stop_emission() except it will look up the
1130 * signal id for you.
1133 g_signal_stop_emission_by_name (gpointer instance,
1134 const gchar *detailed_signal)
1140 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1141 g_return_if_fail (detailed_signal != NULL);
1144 itype = G_TYPE_FROM_INSTANCE (instance);
1145 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1148 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1150 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1151 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
1152 else if (!g_type_is_a (itype, node->itype))
1153 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1154 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1157 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1158 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1162 if (emission->state == EMISSION_HOOK)
1163 g_warning (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1164 node->name, instance);
1165 else if (emission->state == EMISSION_RUN)
1166 emission->state = EMISSION_STOP;
1169 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1170 node->name, instance);
1174 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1175 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1181 * @name: the signal's name.
1182 * @itype: the type that the signal operates on.
1184 * Given the name of the signal and the type of object it connects to, gets
1185 * the signal's identifying integer. Emitting the signal by number is
1186 * somewhat faster than using the name each time.
1188 * Also tries the ancestors of the given type.
1190 * See g_signal_new() for details on allowed signal names.
1192 * Returns: the signal's identifying number, or 0 if no signal was found.
1195 g_signal_lookup (const gchar *name,
1199 g_return_val_if_fail (name != NULL, 0);
1200 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1203 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1207 /* give elaborate warnings */
1208 if (!g_type_name (itype))
1209 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id '%"G_GSIZE_FORMAT"'",
1211 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1212 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type '%s'",
1213 name, g_type_name (itype));
1214 else if (!g_type_class_peek (itype))
1215 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type '%s'",
1216 name, g_type_name (itype));
1223 * g_signal_list_ids:
1224 * @itype: Instance or interface type.
1225 * @n_ids: Location to store the number of signal ids for @itype.
1227 * Lists the signals by id that a certain instance or interface type
1228 * created. Further information about the signals can be acquired through
1231 * Returns: (array length=n_ids): Newly allocated array of signal IDs.
1234 g_signal_list_ids (GType itype,
1242 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1243 g_return_val_if_fail (n_ids != NULL, NULL);
1246 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1247 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1248 result = g_array_new (FALSE, FALSE, sizeof (guint));
1250 for (i = 0; i < n_nodes; i++)
1251 if (keys[i].itype == itype)
1253 const gchar *name = g_quark_to_string (keys[i].quark);
1255 /* Signal names with "_" in them are aliases to the same
1256 * name with "-" instead of "_".
1258 if (!strchr (name, '_'))
1259 g_array_append_val (result, keys[i].signal_id);
1261 *n_ids = result->len;
1265 /* give elaborate warnings */
1266 if (!g_type_name (itype))
1267 g_warning (G_STRLOC ": unable to list signals for invalid type id '%"G_GSIZE_FORMAT"'",
1269 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1270 g_warning (G_STRLOC ": unable to list signals of non instantiatable type '%s'",
1271 g_type_name (itype));
1272 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1273 g_warning (G_STRLOC ": unable to list signals of unloaded type '%s'",
1274 g_type_name (itype));
1277 return (guint*) g_array_free (result, FALSE);
1282 * @signal_id: the signal's identifying number.
1284 * Given the signal's identifier, finds its name.
1286 * Two different signals may have the same name, if they have differing types.
1288 * Returns: the signal name, or %NULL if the signal number was invalid.
1291 g_signal_name (guint signal_id)
1297 node = LOOKUP_SIGNAL_NODE (signal_id);
1298 name = node ? node->name : NULL;
1301 return (char*) name;
1306 * @signal_id: The signal id of the signal to query information for.
1307 * @query: (out caller-allocates): A user provided structure that is
1308 * filled in with constant values upon success.
1310 * Queries the signal system for in-depth information about a
1311 * specific signal. This function will fill in a user-provided
1312 * structure to hold signal-specific information. If an invalid
1313 * signal id is passed in, the @signal_id member of the #GSignalQuery
1314 * is 0. All members filled into the #GSignalQuery structure should
1315 * be considered constant and have to be left untouched.
1318 g_signal_query (guint signal_id,
1319 GSignalQuery *query)
1323 g_return_if_fail (query != NULL);
1326 node = LOOKUP_SIGNAL_NODE (signal_id);
1327 if (!node || node->destroyed)
1328 query->signal_id = 0;
1331 query->signal_id = node->signal_id;
1332 query->signal_name = node->name;
1333 query->itype = node->itype;
1334 query->signal_flags = node->flags;
1335 query->return_type = node->return_type;
1336 query->n_params = node->n_params;
1337 query->param_types = node->param_types;
1344 * @signal_name: the name for the signal
1345 * @itype: the type this signal pertains to. It will also pertain to
1346 * types which are derived from this type.
1347 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1348 * the default handler is to be invoked. You should at least specify
1349 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1350 * @class_offset: The offset of the function pointer in the class structure
1351 * for this type. Used to invoke a class method generically. Pass 0 to
1352 * not associate a class method slot with this signal.
1353 * @accumulator: the accumulator for this signal; may be %NULL.
1354 * @accu_data: user data for the @accumulator.
1355 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1356 * values to signal emissions into C language callback invocations or %NULL.
1357 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1358 * without a return value.
1359 * @n_params: the number of parameter types to follow.
1360 * @...: a list of types, one for each parameter.
1362 * Creates a new signal. (This is usually done in the class initializer.)
1364 * A signal name consists of segments consisting of ASCII letters and
1365 * digits, separated by either the '-' or '_' character. The first
1366 * character of a signal name must be a letter. Names which violate these
1367 * rules lead to undefined behaviour of the GSignal system.
1369 * When registering a signal and looking up a signal, either separator can
1370 * be used, but they cannot be mixed.
1372 * If 0 is used for @class_offset subclasses cannot override the class handler
1373 * in their <code>class_init</code> method by doing
1374 * <code>super_class->signal_handler = my_signal_handler</code>. Instead they
1375 * will have to use g_signal_override_class_handler().
1377 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1378 * the marshaller for this signal.
1380 * Returns: the signal id
1383 g_signal_new (const gchar *signal_name,
1385 GSignalFlags signal_flags,
1387 GSignalAccumulator accumulator,
1389 GSignalCMarshaller c_marshaller,
1397 g_return_val_if_fail (signal_name != NULL, 0);
1399 va_start (args, n_params);
1401 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1402 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1403 accumulator, accu_data, c_marshaller,
1404 return_type, n_params, args);
1412 * g_signal_new_class_handler:
1413 * @signal_name: the name for the signal
1414 * @itype: the type this signal pertains to. It will also pertain to
1415 * types which are derived from this type.
1416 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1417 * the default handler is to be invoked. You should at least specify
1418 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1419 * @class_handler: a #GCallback which acts as class implementation of
1420 * this signal. Used to invoke a class method generically. Pass %NULL to
1421 * not associate a class method with this signal.
1422 * @accumulator: the accumulator for this signal; may be %NULL.
1423 * @accu_data: user data for the @accumulator.
1424 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1425 * values to signal emissions into C language callback invocations or %NULL.
1426 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1427 * without a return value.
1428 * @n_params: the number of parameter types to follow.
1429 * @...: a list of types, one for each parameter.
1431 * Creates a new signal. (This is usually done in the class initializer.)
1433 * This is a variant of g_signal_new() that takes a C callback instead
1434 * off a class offset for the signal's class handler. This function
1435 * doesn't need a function pointer exposed in the class structure of
1436 * an object definition, instead the function pointer is passed
1437 * directly and can be overriden by derived classes with
1438 * g_signal_override_class_closure() or
1439 * g_signal_override_class_handler()and chained to with
1440 * g_signal_chain_from_overridden() or
1441 * g_signal_chain_from_overridden_handler().
1443 * See g_signal_new() for information about signal names.
1445 * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1446 * the marshaller for this signal.
1448 * Returns: the signal id
1453 g_signal_new_class_handler (const gchar *signal_name,
1455 GSignalFlags signal_flags,
1456 GCallback class_handler,
1457 GSignalAccumulator accumulator,
1459 GSignalCMarshaller c_marshaller,
1467 g_return_val_if_fail (signal_name != NULL, 0);
1469 va_start (args, n_params);
1471 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1472 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1473 accumulator, accu_data, c_marshaller,
1474 return_type, n_params, args);
1481 static inline ClassClosure*
1482 signal_find_class_closure (SignalNode *node,
1485 GBSearchArray *bsa = node->class_closure_bsa;
1492 /* cc->instance_type is 0 for default closure */
1494 key.instance_type = itype;
1495 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1496 while (!cc && key.instance_type)
1498 key.instance_type = g_type_parent (key.instance_type);
1499 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1507 static inline GClosure*
1508 signal_lookup_closure (SignalNode *node,
1509 GTypeInstance *instance)
1513 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1515 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1516 if (cc && cc->instance_type == 0) /* check for default closure */
1519 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1520 return cc ? cc->closure : NULL;
1524 signal_add_class_closure (SignalNode *node,
1530 node->single_va_closure_is_valid = FALSE;
1532 if (!node->class_closure_bsa)
1533 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1534 key.instance_type = itype;
1535 key.closure = g_closure_ref (closure);
1536 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1537 &g_class_closure_bconfig,
1539 g_closure_sink (closure);
1540 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1542 g_closure_set_marshal (closure, node->c_marshaller);
1543 if (node->va_marshaller)
1544 _g_closure_set_va_marshal (closure, node->va_marshaller);
1550 * @signal_name: the name for the signal
1551 * @itype: the type this signal pertains to. It will also pertain to
1552 * types which are derived from this type
1553 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1554 * the default handler is to be invoked. You should at least specify
1555 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1556 * @class_closure: (allow-none): The closure to invoke on signal emission;
1558 * @accumulator: (allow-none): the accumulator for this signal; may be %NULL
1559 * @accu_data: user data for the @accumulator
1560 * @c_marshaller: (allow-none): the function to translate arrays of
1561 * parameter values to signal emissions into C language callback
1562 * invocations or %NULL
1563 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1564 * without a return value
1565 * @n_params: the length of @param_types
1566 * @param_types: (array length=n_params): an array of types, one for
1569 * Creates a new signal. (This is usually done in the class initializer.)
1571 * See g_signal_new() for details on allowed signal names.
1573 * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1574 * the marshaller for this signal.
1576 * Returns: the signal id
1579 g_signal_newv (const gchar *signal_name,
1581 GSignalFlags signal_flags,
1582 GClosure *class_closure,
1583 GSignalAccumulator accumulator,
1585 GSignalCMarshaller c_marshaller,
1593 GSignalCMarshaller builtin_c_marshaller;
1594 GSignalCVaMarshaller va_marshaller;
1596 g_return_val_if_fail (signal_name != NULL, 0);
1597 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1599 g_return_val_if_fail (param_types != NULL, 0);
1600 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1601 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1602 g_return_val_if_fail (accumulator == NULL, 0);
1604 g_return_val_if_fail (accu_data == NULL, 0);
1606 name = g_strdup (signal_name);
1607 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1611 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1612 node = LOOKUP_SIGNAL_NODE (signal_id);
1613 if (node && !node->destroyed)
1615 g_warning (G_STRLOC ": signal \"%s\" already exists in the '%s' %s",
1617 type_debug_name (node->itype),
1618 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1623 if (node && node->itype != itype)
1625 g_warning (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'",
1627 type_debug_name (itype),
1628 type_debug_name (node->itype));
1633 for (i = 0; i < n_params; i++)
1634 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1636 g_warning (G_STRLOC ": parameter %d of type '%s' for signal \"%s::%s\" is not a value type",
1637 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1642 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1644 g_warning (G_STRLOC ": return value of type '%s' for signal \"%s::%s\" is not a value type",
1645 type_debug_name (return_type), type_debug_name (itype), name);
1650 if (return_type != G_TYPE_NONE &&
1651 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1653 g_warning (G_STRLOC ": signal \"%s::%s\" has return type '%s' and is only G_SIGNAL_RUN_FIRST",
1654 type_debug_name (itype), name, type_debug_name (return_type));
1660 /* setup permanent portion of signal node */
1665 signal_id = g_n_signal_nodes++;
1666 node = g_new (SignalNode, 1);
1667 node->signal_id = signal_id;
1668 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1669 g_signal_nodes[signal_id] = node;
1670 node->itype = itype;
1673 key.quark = g_quark_from_string (node->name);
1674 key.signal_id = signal_id;
1675 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1676 g_strdelimit (name, "_", '-');
1677 node->name = g_intern_string (name);
1678 key.quark = g_quark_from_string (name);
1679 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1681 TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1683 node->destroyed = FALSE;
1685 /* setup reinitializable portion */
1686 node->single_va_closure_is_valid = FALSE;
1687 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1688 node->n_params = n_params;
1689 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1690 node->return_type = return_type;
1691 node->class_closure_bsa = NULL;
1694 node->accumulator = g_new (SignalAccumulator, 1);
1695 node->accumulator->func = accumulator;
1696 node->accumulator->data = accu_data;
1699 node->accumulator = NULL;
1701 builtin_c_marshaller = NULL;
1702 va_marshaller = NULL;
1704 /* Pick up built-in va marshallers for standard types, and
1705 instead of generic marshaller if no marshaller specified */
1706 if (n_params == 0 && return_type == G_TYPE_NONE)
1708 builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1709 va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1711 else if (n_params == 1 && return_type == G_TYPE_NONE)
1713 #define ADD_CHECK(__type__) \
1714 else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \
1716 builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \
1717 va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \
1740 if (c_marshaller == NULL)
1742 if (builtin_c_marshaller)
1743 c_marshaller = builtin_c_marshaller;
1746 c_marshaller = g_cclosure_marshal_generic;
1747 va_marshaller = g_cclosure_marshal_generic_va;
1751 node->c_marshaller = c_marshaller;
1752 node->va_marshaller = va_marshaller;
1753 node->emission_hooks = NULL;
1755 signal_add_class_closure (node, 0, class_closure);
1765 g_signal_set_va_marshaller (guint signal_id,
1766 GType instance_type,
1767 GSignalCVaMarshaller va_marshaller)
1771 g_return_if_fail (signal_id > 0);
1772 g_return_if_fail (va_marshaller != NULL);
1775 node = LOOKUP_SIGNAL_NODE (signal_id);
1778 node->va_marshaller = va_marshaller;
1779 if (node->class_closure_bsa)
1781 ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1782 if (cc->closure->marshal == node->c_marshaller)
1783 _g_closure_set_va_marshal (cc->closure, va_marshaller);
1786 node->single_va_closure_is_valid = FALSE;
1794 * g_signal_new_valist:
1795 * @signal_name: the name for the signal
1796 * @itype: the type this signal pertains to. It will also pertain to
1797 * types which are derived from this type.
1798 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1799 * the default handler is to be invoked. You should at least specify
1800 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1801 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1802 * @accumulator: the accumulator for this signal; may be %NULL.
1803 * @accu_data: user data for the @accumulator.
1804 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1805 * values to signal emissions into C language callback invocations or %NULL.
1806 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1807 * without a return value.
1808 * @n_params: the number of parameter types in @args.
1809 * @args: va_list of #GType, one for each parameter.
1811 * Creates a new signal. (This is usually done in the class initializer.)
1813 * See g_signal_new() for details on allowed signal names.
1815 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1816 * the marshaller for this signal.
1818 * Returns: the signal id
1821 g_signal_new_valist (const gchar *signal_name,
1823 GSignalFlags signal_flags,
1824 GClosure *class_closure,
1825 GSignalAccumulator accumulator,
1827 GSignalCMarshaller c_marshaller,
1838 param_types = g_new (GType, n_params);
1840 for (i = 0; i < n_params; i++)
1841 param_types[i] = va_arg (args, GType);
1846 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1847 class_closure, accumulator, accu_data, c_marshaller,
1848 return_type, n_params, param_types);
1849 g_free (param_types);
1855 signal_destroy_R (SignalNode *signal_node)
1857 SignalNode node = *signal_node;
1859 signal_node->destroyed = TRUE;
1861 /* reentrancy caution, zero out real contents first */
1862 signal_node->single_va_closure_is_valid = FALSE;
1863 signal_node->n_params = 0;
1864 signal_node->param_types = NULL;
1865 signal_node->return_type = 0;
1866 signal_node->class_closure_bsa = NULL;
1867 signal_node->accumulator = NULL;
1868 signal_node->c_marshaller = NULL;
1869 signal_node->va_marshaller = NULL;
1870 signal_node->emission_hooks = NULL;
1872 #ifdef G_ENABLE_DEBUG
1873 /* check current emissions */
1877 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1878 emission; emission = emission->next)
1879 if (emission->ihint.signal_id == node.signal_id)
1880 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')",
1881 node.name, emission->instance);
1885 /* free contents that need to
1888 g_free (node.param_types);
1889 if (node.class_closure_bsa)
1893 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1895 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1897 g_closure_unref (cc->closure);
1899 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1901 g_free (node.accumulator);
1902 if (node.emission_hooks)
1904 g_hook_list_clear (node.emission_hooks);
1905 g_free (node.emission_hooks);
1911 * g_signal_override_class_closure:
1912 * @signal_id: the signal id
1913 * @instance_type: the instance type on which to override the class closure
1915 * @class_closure: the closure.
1917 * Overrides the class closure (i.e. the default handler) for the given signal
1918 * for emissions on instances of @instance_type. @instance_type must be derived
1919 * from the type to which the signal belongs.
1921 * See g_signal_chain_from_overridden() and
1922 * g_signal_chain_from_overridden_handler() for how to chain up to the
1923 * parent class closure from inside the overridden one.
1926 g_signal_override_class_closure (guint signal_id,
1927 GType instance_type,
1928 GClosure *class_closure)
1932 g_return_if_fail (signal_id > 0);
1933 g_return_if_fail (class_closure != NULL);
1936 node = LOOKUP_SIGNAL_NODE (signal_id);
1937 node_check_deprecated (node);
1938 if (!g_type_is_a (instance_type, node->itype))
1939 g_warning ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1942 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1944 if (cc && cc->instance_type == instance_type)
1945 g_warning ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1947 signal_add_class_closure (node, instance_type, class_closure);
1953 * g_signal_override_class_handler:
1954 * @signal_name: the name for the signal
1955 * @instance_type: the instance type on which to override the class handler
1957 * @class_handler: the handler.
1959 * Overrides the class closure (i.e. the default handler) for the
1960 * given signal for emissions on instances of @instance_type with
1961 * callback @class_handler. @instance_type must be derived from the
1962 * type to which the signal belongs.
1964 * See g_signal_chain_from_overridden() and
1965 * g_signal_chain_from_overridden_handler() for how to chain up to the
1966 * parent class closure from inside the overridden one.
1971 g_signal_override_class_handler (const gchar *signal_name,
1972 GType instance_type,
1973 GCallback class_handler)
1977 g_return_if_fail (signal_name != NULL);
1978 g_return_if_fail (instance_type != G_TYPE_NONE);
1979 g_return_if_fail (class_handler != NULL);
1981 signal_id = g_signal_lookup (signal_name, instance_type);
1984 g_signal_override_class_closure (signal_id, instance_type,
1985 g_cclosure_new (class_handler, NULL, NULL));
1987 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1988 G_STRLOC, signal_name, instance_type);
1993 * g_signal_chain_from_overridden:
1994 * @instance_and_params: (array) the argument list of the signal emission.
1995 * The first element in the array is a #GValue for the instance the signal
1996 * is being emitted on. The rest are any arguments to be passed to the signal.
1997 * @return_value: Location for the return value.
1999 * Calls the original class closure of a signal. This function should only
2000 * be called from an overridden class closure; see
2001 * g_signal_override_class_closure() and
2002 * g_signal_override_class_handler().
2005 g_signal_chain_from_overridden (const GValue *instance_and_params,
2006 GValue *return_value)
2008 GType chain_type = 0, restore_type = 0;
2009 Emission *emission = NULL;
2010 GClosure *closure = NULL;
2014 g_return_if_fail (instance_and_params != NULL);
2015 instance = g_value_peek_pointer (instance_and_params);
2016 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2019 emission = emission_find_innermost (instance);
2022 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2024 g_assert (node != NULL); /* paranoid */
2026 /* we should probably do the same parameter checks as g_signal_emit() here.
2028 if (emission->chain_type != G_TYPE_NONE)
2030 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2032 g_assert (cc != NULL); /* closure currently in call stack */
2034 n_params = node->n_params;
2035 restore_type = cc->instance_type;
2036 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2037 if (cc && cc->instance_type != restore_type)
2039 closure = cc->closure;
2040 chain_type = cc->instance_type;
2044 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2047 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2051 emission->chain_type = chain_type;
2053 g_closure_invoke (closure,
2056 instance_and_params,
2059 emission->chain_type = restore_type;
2065 * g_signal_chain_from_overridden_handler: (skip)
2066 * @instance: the instance the signal is being emitted on.
2067 * @...: parameters to be passed to the parent class closure, followed by a
2068 * location for the return value. If the return type of the signal
2069 * is #G_TYPE_NONE, the return value location can be omitted.
2071 * Calls the original class closure of a signal. This function should
2072 * only be called from an overridden class closure; see
2073 * g_signal_override_class_closure() and
2074 * g_signal_override_class_handler().
2079 g_signal_chain_from_overridden_handler (gpointer instance,
2082 GType chain_type = 0, restore_type = 0;
2083 Emission *emission = NULL;
2084 GClosure *closure = NULL;
2088 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2091 emission = emission_find_innermost (instance);
2094 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2096 g_assert (node != NULL); /* paranoid */
2098 /* we should probably do the same parameter checks as g_signal_emit() here.
2100 if (emission->chain_type != G_TYPE_NONE)
2102 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2104 g_assert (cc != NULL); /* closure currently in call stack */
2106 n_params = node->n_params;
2107 restore_type = cc->instance_type;
2108 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2109 if (cc && cc->instance_type != restore_type)
2111 closure = cc->closure;
2112 chain_type = cc->instance_type;
2116 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2119 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2123 GValue *instance_and_params;
2124 GType signal_return_type;
2125 GValue *param_values;
2129 va_start (var_args, instance);
2131 signal_return_type = node->return_type;
2132 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
2133 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
2134 param_values = instance_and_params + 1;
2136 for (i = 0; i < node->n_params; i++)
2139 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2140 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2143 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2145 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2149 g_warning ("%s: %s", G_STRLOC, error);
2152 /* we purposely leak the value here, it might not be
2153 * in a sane state if an error condition occoured
2156 g_value_unset (param_values + i);
2165 instance_and_params->g_type = 0;
2166 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2167 g_value_set_instance (instance_and_params, instance);
2170 emission->chain_type = chain_type;
2173 if (signal_return_type == G_TYPE_NONE)
2175 g_closure_invoke (closure,
2178 instance_and_params,
2183 GValue return_value = G_VALUE_INIT;
2184 gchar *error = NULL;
2185 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2186 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2188 g_value_init (&return_value, rtype);
2190 g_closure_invoke (closure,
2193 instance_and_params,
2196 G_VALUE_LCOPY (&return_value,
2198 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2202 g_value_unset (&return_value);
2206 g_warning ("%s: %s", G_STRLOC, error);
2209 /* we purposely leak the value here, it might not be
2210 * in a sane state if an error condition occurred
2215 for (i = 0; i < n_params; i++)
2216 g_value_unset (param_values + i);
2217 g_value_unset (instance_and_params);
2222 emission->chain_type = restore_type;
2228 * g_signal_get_invocation_hint:
2229 * @instance: (type GObject.Object): the instance to query
2231 * Returns the invocation hint of the innermost signal emission of instance.
2233 * Returns: (transfer none): the invocation hint of the innermost signal emission.
2235 GSignalInvocationHint*
2236 g_signal_get_invocation_hint (gpointer instance)
2238 Emission *emission = NULL;
2240 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2243 emission = emission_find_innermost (instance);
2246 return emission ? &emission->ihint : NULL;
2250 * g_signal_connect_closure_by_id:
2251 * @instance: (type GObject.Object): the instance to connect to.
2252 * @signal_id: the id of the signal.
2253 * @detail: the detail.
2254 * @closure: the closure to connect.
2255 * @after: whether the handler should be called before or after the
2256 * default handler of the signal.
2258 * Connects a closure to a signal for a particular object.
2260 * Returns: the handler id (always greater than 0 for successful connections)
2263 g_signal_connect_closure_by_id (gpointer instance,
2270 gulong handler_seq_no = 0;
2272 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2273 g_return_val_if_fail (signal_id > 0, 0);
2274 g_return_val_if_fail (closure != NULL, 0);
2277 node = LOOKUP_SIGNAL_NODE (signal_id);
2280 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2281 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2282 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2283 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2286 Handler *handler = handler_new (after);
2288 handler_seq_no = handler->sequential_number;
2289 handler->detail = detail;
2290 handler->closure = g_closure_ref (closure);
2291 g_closure_sink (closure);
2292 add_invalid_closure_notify (handler, instance);
2293 handler_insert (signal_id, instance, handler);
2294 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2296 g_closure_set_marshal (closure, node->c_marshaller);
2297 if (node->va_marshaller)
2298 _g_closure_set_va_marshal (closure, node->va_marshaller);
2303 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2306 return handler_seq_no;
2310 * g_signal_connect_closure:
2311 * @instance: (type GObject.Object): the instance to connect to.
2312 * @detailed_signal: a string of the form "signal-name::detail".
2313 * @closure: the closure to connect.
2314 * @after: whether the handler should be called before or after the
2315 * default handler of the signal.
2317 * Connects a closure to a signal for a particular object.
2319 * Returns: the handler id (always greater than 0 for successful connections)
2322 g_signal_connect_closure (gpointer instance,
2323 const gchar *detailed_signal,
2328 gulong handler_seq_no = 0;
2332 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2333 g_return_val_if_fail (detailed_signal != NULL, 0);
2334 g_return_val_if_fail (closure != NULL, 0);
2337 itype = G_TYPE_FROM_INSTANCE (instance);
2338 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2341 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2343 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2344 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2345 else if (!g_type_is_a (itype, node->itype))
2346 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2347 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2350 Handler *handler = handler_new (after);
2352 handler_seq_no = handler->sequential_number;
2353 handler->detail = detail;
2354 handler->closure = g_closure_ref (closure);
2355 g_closure_sink (closure);
2356 add_invalid_closure_notify (handler, instance);
2357 handler_insert (signal_id, instance, handler);
2358 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2360 g_closure_set_marshal (handler->closure, node->c_marshaller);
2361 if (node->va_marshaller)
2362 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2367 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2368 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2371 return handler_seq_no;
2375 node_check_deprecated (const SignalNode *node)
2377 static const gchar * g_enable_diagnostic = NULL;
2379 if (G_UNLIKELY (!g_enable_diagnostic))
2381 g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2382 if (!g_enable_diagnostic)
2383 g_enable_diagnostic = "0";
2386 if (g_enable_diagnostic[0] == '1')
2388 if (node->flags & G_SIGNAL_DEPRECATED)
2390 g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2391 "anymore. It will be removed in a future version.",
2392 type_debug_name (node->itype), node->name);
2398 * g_signal_connect_data:
2399 * @instance: (type GObject.Object): the instance to connect to.
2400 * @detailed_signal: a string of the form "signal-name::detail".
2401 * @c_handler: the #GCallback to connect.
2402 * @data: data to pass to @c_handler calls.
2403 * @destroy_data: a #GClosureNotify for @data.
2404 * @connect_flags: a combination of #GConnectFlags.
2406 * Connects a #GCallback function to a signal for a particular object. Similar
2407 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2408 * which will be called when the signal handler is disconnected and no longer
2409 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
2410 * <literal>..._swapped()</literal> variants of this function.
2412 * Returns: the handler id (always greater than 0 for successful connections)
2415 g_signal_connect_data (gpointer instance,
2416 const gchar *detailed_signal,
2417 GCallback c_handler,
2419 GClosureNotify destroy_data,
2420 GConnectFlags connect_flags)
2423 gulong handler_seq_no = 0;
2426 gboolean swapped, after;
2428 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2429 g_return_val_if_fail (detailed_signal != NULL, 0);
2430 g_return_val_if_fail (c_handler != NULL, 0);
2432 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2433 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2436 itype = G_TYPE_FROM_INSTANCE (instance);
2437 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2440 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2442 node_check_deprecated (node);
2444 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2445 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2446 else if (!g_type_is_a (itype, node->itype))
2447 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2448 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2451 Handler *handler = handler_new (after);
2453 handler_seq_no = handler->sequential_number;
2454 handler->detail = detail;
2455 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2456 g_closure_sink (handler->closure);
2457 handler_insert (signal_id, instance, handler);
2458 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2460 g_closure_set_marshal (handler->closure, node->c_marshaller);
2461 if (node->va_marshaller)
2462 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2467 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2468 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2471 return handler_seq_no;
2475 * g_signal_handler_block:
2476 * @instance: (type GObject.Object): The instance to block the signal handler of.
2477 * @handler_id: Handler id of the handler to be blocked.
2479 * Blocks a handler of an instance so it will not be called during any
2480 * signal emissions unless it is unblocked again. Thus "blocking" a
2481 * signal handler means to temporarily deactive it, a signal handler
2482 * has to be unblocked exactly the same amount of times it has been
2483 * blocked before to become active again.
2485 * The @handler_id has to be a valid signal handler id, connected to a
2486 * signal of @instance.
2489 g_signal_handler_block (gpointer instance,
2494 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2495 g_return_if_fail (handler_id > 0);
2498 handler = handler_lookup (instance, handler_id, NULL, NULL);
2501 #ifndef G_DISABLE_CHECKS
2502 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2503 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2505 handler->block_count += 1;
2508 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2513 * g_signal_handler_unblock:
2514 * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2515 * @handler_id: Handler id of the handler to be unblocked.
2517 * Undoes the effect of a previous g_signal_handler_block() call. A
2518 * blocked handler is skipped during signal emissions and will not be
2519 * invoked, unblocking it (for exactly the amount of times it has been
2520 * blocked before) reverts its "blocked" state, so the handler will be
2521 * recognized by the signal system and is called upon future or
2522 * currently ongoing signal emissions (since the order in which
2523 * handlers are called during signal emissions is deterministic,
2524 * whether the unblocked handler in question is called as part of a
2525 * currently ongoing emission depends on how far that emission has
2528 * The @handler_id has to be a valid id of a signal handler that is
2529 * connected to a signal of @instance and is currently blocked.
2532 g_signal_handler_unblock (gpointer instance,
2537 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2538 g_return_if_fail (handler_id > 0);
2541 handler = handler_lookup (instance, handler_id, NULL, NULL);
2544 if (handler->block_count)
2545 handler->block_count -= 1;
2547 g_warning (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2550 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2555 * g_signal_handler_disconnect:
2556 * @instance: (type GObject.Object): The instance to remove the signal handler from.
2557 * @handler_id: Handler id of the handler to be disconnected.
2559 * Disconnects a handler from an instance so it will not be called during
2560 * any future or currently ongoing emissions of the signal it has been
2561 * connected to. The @handler_id becomes invalid and may be reused.
2563 * The @handler_id has to be a valid signal handler id, connected to a
2564 * signal of @instance.
2567 g_signal_handler_disconnect (gpointer instance,
2573 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2574 g_return_if_fail (handler_id > 0);
2577 handler = handler_lookup (instance, handler_id, NULL, &signal_id);
2580 handler->sequential_number = 0;
2581 handler->block_count = 1;
2582 remove_invalid_closure_notify (handler, instance);
2583 handler_unref_R (signal_id, instance, handler);
2586 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2591 * g_signal_handler_is_connected:
2592 * @instance: (type GObject.Object): The instance where a signal handler is sought.
2593 * @handler_id: the handler id.
2595 * Returns whether @handler_id is the id of a handler connected to @instance.
2597 * Returns: whether @handler_id identifies a handler connected to @instance.
2600 g_signal_handler_is_connected (gpointer instance,
2606 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2609 handler = handler_lookup (instance, handler_id, NULL, NULL);
2610 connected = handler != NULL;
2617 * g_signal_handlers_destroy:
2618 * @instance: (type GObject.Object): The instance where a signal handler is sought.
2621 g_signal_handlers_destroy (gpointer instance)
2623 GBSearchArray *hlbsa;
2625 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2628 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2633 /* reentrancy caution, delete instance trace first */
2634 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2636 for (i = 0; i < hlbsa->n_nodes; i++)
2638 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2639 Handler *handler = hlist->handlers;
2643 Handler *tmp = handler;
2645 handler = tmp->next;
2646 tmp->block_count = 1;
2647 /* cruel unlink, this works because _all_ handlers vanish */
2650 if (tmp->sequential_number)
2652 remove_invalid_closure_notify (tmp, instance);
2653 tmp->sequential_number = 0;
2654 handler_unref_R (0, NULL, tmp);
2658 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2664 * g_signal_handler_find:
2665 * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2666 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2667 * and/or @data the handler has to match.
2668 * @signal_id: Signal the handler has to be connected to.
2669 * @detail: Signal detail the handler has to be connected to.
2670 * @closure: (allow-none): The closure the handler will invoke.
2671 * @func: The C closure callback of the handler (useless for non-C closures).
2672 * @data: The closure data of the handler's closure.
2674 * Finds the first signal handler that matches certain selection criteria.
2675 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2676 * flags, and the criteria values are passed as arguments.
2677 * The match @mask has to be non-0 for successful matches.
2678 * If no handler was found, 0 is returned.
2680 * Returns: A valid non-0 signal handler id for a successful match.
2683 g_signal_handler_find (gpointer instance,
2684 GSignalMatchType mask,
2691 gulong handler_seq_no = 0;
2693 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2694 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2696 if (mask & G_SIGNAL_MATCH_MASK)
2698 HandlerMatch *mlist;
2701 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2704 handler_seq_no = mlist->handler->sequential_number;
2705 handler_match_free1_R (mlist, instance);
2710 return handler_seq_no;
2714 signal_handlers_foreach_matched_R (gpointer instance,
2715 GSignalMatchType mask,
2721 void (*callback) (gpointer instance,
2722 gulong handler_seq_no))
2724 HandlerMatch *mlist;
2725 guint n_handlers = 0;
2727 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2731 if (mlist->handler->sequential_number)
2734 callback (instance, mlist->handler->sequential_number);
2737 mlist = handler_match_free1_R (mlist, instance);
2744 * g_signal_handlers_block_matched:
2745 * @instance: (type GObject.Object): The instance to block handlers from.
2746 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2747 * and/or @data the handlers have to match.
2748 * @signal_id: Signal the handlers have to be connected to.
2749 * @detail: Signal detail the handlers have to be connected to.
2750 * @closure: (allow-none): The closure the handlers will invoke.
2751 * @func: The C closure callback of the handlers (useless for non-C closures).
2752 * @data: The closure data of the handlers' closures.
2754 * Blocks all handlers on an instance that match a certain selection criteria.
2755 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2756 * flags, and the criteria values are passed as arguments.
2757 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2758 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2759 * If no handlers were found, 0 is returned, the number of blocked handlers
2762 * Returns: The number of handlers that matched.
2765 g_signal_handlers_block_matched (gpointer instance,
2766 GSignalMatchType mask,
2773 guint n_handlers = 0;
2775 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2776 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2778 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2781 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2782 closure, func, data,
2783 g_signal_handler_block);
2791 * g_signal_handlers_unblock_matched:
2792 * @instance: (type GObject.Object): The instance to unblock handlers from.
2793 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2794 * and/or @data the handlers have to match.
2795 * @signal_id: Signal the handlers have to be connected to.
2796 * @detail: Signal detail the handlers have to be connected to.
2797 * @closure: (allow-none): The closure the handlers will invoke.
2798 * @func: The C closure callback of the handlers (useless for non-C closures).
2799 * @data: The closure data of the handlers' closures.
2801 * Unblocks all handlers on an instance that match a certain selection
2802 * criteria. The criteria mask is passed as an OR-ed combination of
2803 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2804 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2805 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2806 * If no handlers were found, 0 is returned, the number of unblocked handlers
2807 * otherwise. The match criteria should not apply to any handlers that are
2808 * not currently blocked.
2810 * Returns: The number of handlers that matched.
2813 g_signal_handlers_unblock_matched (gpointer instance,
2814 GSignalMatchType mask,
2821 guint n_handlers = 0;
2823 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2824 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2826 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2829 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2830 closure, func, data,
2831 g_signal_handler_unblock);
2839 * g_signal_handlers_disconnect_matched:
2840 * @instance: (type GObject.Object): The instance to remove handlers from.
2841 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2842 * and/or @data the handlers have to match.
2843 * @signal_id: Signal the handlers have to be connected to.
2844 * @detail: Signal detail the handlers have to be connected to.
2845 * @closure: (allow-none): The closure the handlers will invoke.
2846 * @func: The C closure callback of the handlers (useless for non-C closures).
2847 * @data: The closure data of the handlers' closures.
2849 * Disconnects all handlers on an instance that match a certain
2850 * selection criteria. The criteria mask is passed as an OR-ed
2851 * combination of #GSignalMatchType flags, and the criteria values are
2852 * passed as arguments. Passing at least one of the
2853 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2854 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2855 * matches. If no handlers were found, 0 is returned, the number of
2856 * disconnected handlers otherwise.
2858 * Returns: The number of handlers that matched.
2861 g_signal_handlers_disconnect_matched (gpointer instance,
2862 GSignalMatchType mask,
2869 guint n_handlers = 0;
2871 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2872 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2874 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2877 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2878 closure, func, data,
2879 g_signal_handler_disconnect);
2887 * g_signal_has_handler_pending:
2888 * @instance: (type GObject.Object): the object whose signal handlers are sought.
2889 * @signal_id: the signal id.
2890 * @detail: the detail.
2891 * @may_be_blocked: whether blocked handlers should count as match.
2893 * Returns whether there are any handlers connected to @instance for the
2894 * given signal id and detail.
2896 * One example of when you might use this is when the arguments to the
2897 * signal are difficult to compute. A class implementor may opt to not
2898 * emit the signal if no one is attached anyway, thus saving the cost
2899 * of building the arguments.
2901 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2905 g_signal_has_handler_pending (gpointer instance,
2908 gboolean may_be_blocked)
2910 HandlerMatch *mlist;
2911 gboolean has_pending;
2913 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2914 g_return_val_if_fail (signal_id > 0, FALSE);
2919 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2921 if (!(node->flags & G_SIGNAL_DETAILED))
2923 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2928 mlist = handlers_find (instance,
2929 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2930 signal_id, detail, NULL, NULL, NULL, TRUE);
2934 handler_match_free1_R (mlist, instance);
2937 has_pending = FALSE;
2945 * @instance_and_params: (array): argument list for the signal emission.
2946 * The first element in the array is a #GValue for the instance the signal
2947 * is being emitted on. The rest are any arguments to be passed to the signal.
2948 * @signal_id: the signal id
2949 * @detail: the detail
2950 * @return_value: Location to store the return value of the signal emission.
2954 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2955 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2958 g_signal_emitv (const GValue *instance_and_params,
2961 GValue *return_value)
2965 #ifdef G_ENABLE_DEBUG
2966 const GValue *param_values;
2970 g_return_if_fail (instance_and_params != NULL);
2971 instance = g_value_peek_pointer (instance_and_params);
2972 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2973 g_return_if_fail (signal_id > 0);
2975 #ifdef G_ENABLE_DEBUG
2976 param_values = instance_and_params + 1;
2980 node = LOOKUP_SIGNAL_NODE (signal_id);
2981 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2983 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2987 #ifdef G_ENABLE_DEBUG
2988 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2990 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2994 for (i = 0; i < node->n_params; i++)
2995 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2997 g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
2999 type_debug_name (node->param_types[i]),
3002 G_VALUE_TYPE_NAME (param_values + i));
3006 if (node->return_type != G_TYPE_NONE)
3010 g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3012 type_debug_name (node->return_type),
3017 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3019 g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3021 type_debug_name (node->return_type),
3023 G_VALUE_TYPE_NAME (return_value));
3029 return_value = NULL;
3030 #endif /* G_ENABLE_DEBUG */
3032 /* optimize NOP emissions */
3033 if (!node->single_va_closure_is_valid)
3034 node_update_single_va_closure (node);
3036 if (node->single_va_closure != NULL &&
3037 (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3038 _g_closure_is_void (node->single_va_closure, instance)))
3040 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3041 if (hlist == NULL || hlist->handlers == NULL)
3043 /* nothing to do to emit this signal */
3045 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3051 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
3054 static inline gboolean
3055 accumulate (GSignalInvocationHint *ihint,
3056 GValue *return_accu,
3057 GValue *handler_return,
3058 SignalAccumulator *accumulator)
3060 gboolean continue_emission;
3065 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3066 g_value_reset (handler_return);
3068 return continue_emission;
3072 * g_signal_emit_valist: (skip)
3073 * @instance: the instance the signal is being emitted on.
3074 * @signal_id: the signal id
3075 * @detail: the detail
3076 * @var_args: a list of parameters to be passed to the signal, followed by a
3077 * location for the return value. If the return type of the signal
3078 * is #G_TYPE_NONE, the return value location can be omitted.
3082 * Note that g_signal_emit_valist() resets the return value to the default
3083 * if no handlers are connected, in contrast to g_signal_emitv().
3086 g_signal_emit_valist (gpointer instance,
3091 GValue *instance_and_params;
3092 GType signal_return_type;
3093 GValue *param_values;
3097 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3098 g_return_if_fail (signal_id > 0);
3101 node = LOOKUP_SIGNAL_NODE (signal_id);
3102 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3104 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3108 #ifndef G_DISABLE_CHECKS
3109 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3111 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3115 #endif /* !G_DISABLE_CHECKS */
3117 if (!node->single_va_closure_is_valid)
3118 node_update_single_va_closure (node);
3120 if (node->single_va_closure != NULL)
3122 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3123 Handler *fastpath_handler = NULL;
3125 GClosure *closure = NULL;
3126 gboolean fastpath = TRUE;
3127 GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3129 if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3130 !_g_closure_is_void (node->single_va_closure, instance))
3132 if (_g_closure_supports_invoke_va (node->single_va_closure))
3134 closure = node->single_va_closure;
3135 if (node->single_va_closure_is_after)
3136 run_type = G_SIGNAL_RUN_LAST;
3138 run_type = G_SIGNAL_RUN_FIRST;
3144 for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3146 if (!l->block_count &&
3147 (!l->detail || l->detail == detail))
3149 if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3156 fastpath_handler = l;
3157 closure = l->closure;
3159 run_type = G_SIGNAL_RUN_LAST;
3161 run_type = G_SIGNAL_RUN_FIRST;
3166 if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3172 /* Don't allow no-recurse emission as we might have to restart, which means
3173 we will run multiple handlers and thus must ref all arguments */
3174 if (closure != NULL && (node->flags & (G_SIGNAL_NO_RECURSE)) != 0)
3179 SignalAccumulator *accumulator;
3181 GValue *return_accu, accu = G_VALUE_INIT;
3183 GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3184 GValue emission_return = G_VALUE_INIT;
3185 GType rtype = node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3186 gboolean static_scope = node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3188 signal_id = node->signal_id;
3189 accumulator = node->accumulator;
3190 if (rtype == G_TYPE_NONE)
3192 else if (accumulator)
3193 return_accu = &accu;
3195 return_accu = &emission_return;
3197 emission.instance = instance;
3198 emission.ihint.signal_id = signal_id;
3199 emission.ihint.detail = detail;
3200 emission.ihint.run_type = run_type;
3201 emission.state = EMISSION_RUN;
3202 emission.chain_type = instance_type;
3203 emission_push (&g_recursive_emissions, &emission);
3205 if (fastpath_handler)
3206 handler_ref (fastpath_handler);
3210 TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3212 if (rtype != G_TYPE_NONE)
3213 g_value_init (&emission_return, rtype);
3216 g_value_init (&accu, rtype);
3218 if (closure != NULL)
3220 g_object_ref (instance);
3221 _g_closure_invoke_va (closure,
3227 accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3232 emission.chain_type = G_TYPE_NONE;
3233 emission_pop (&g_recursive_emissions, &emission);
3235 if (fastpath_handler)
3236 handler_unref_R (signal_id, instance, fastpath_handler);
3241 g_value_unset (&accu);
3243 if (rtype != G_TYPE_NONE)
3245 gchar *error = NULL;
3246 for (i = 0; i < node->n_params; i++)
3248 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3249 G_VALUE_COLLECT_SKIP (ptype, var_args);
3252 G_VALUE_LCOPY (&emission_return,
3254 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3257 g_value_unset (&emission_return);
3260 g_warning ("%s: %s", G_STRLOC, error);
3262 /* we purposely leak the value here, it might not be
3263 * in a sane state if an error condition occurred
3268 TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3270 if (closure != NULL)
3271 g_object_unref (instance);
3278 n_params = node->n_params;
3279 signal_return_type = node->return_type;
3280 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
3281 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
3282 param_values = instance_and_params + 1;
3284 for (i = 0; i < node->n_params; i++)
3287 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3288 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3290 G_VALUE_COLLECT_INIT (param_values + i, ptype,
3292 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3296 g_warning ("%s: %s", G_STRLOC, error);
3299 /* we purposely leak the value here, it might not be
3300 * in a sane state if an error condition occoured
3303 g_value_unset (param_values + i);
3309 instance_and_params->g_type = 0;
3310 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
3311 g_value_set_instance (instance_and_params, instance);
3312 if (signal_return_type == G_TYPE_NONE)
3313 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
3316 GValue return_value = G_VALUE_INIT;
3317 gchar *error = NULL;
3318 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3319 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3321 g_value_init (&return_value, rtype);
3323 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3325 G_VALUE_LCOPY (&return_value,
3327 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3330 g_value_unset (&return_value);
3333 g_warning ("%s: %s", G_STRLOC, error);
3336 /* we purposely leak the value here, it might not be
3337 * in a sane state if an error condition occurred
3341 for (i = 0; i < n_params; i++)
3342 g_value_unset (param_values + i);
3343 g_value_unset (instance_and_params);
3348 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3349 * @signal_id: the signal id
3350 * @detail: the detail
3351 * @...: parameters to be passed to the signal, followed by a
3352 * location for the return value. If the return type of the signal
3353 * is #G_TYPE_NONE, the return value location can be omitted.
3357 * Note that g_signal_emit() resets the return value to the default
3358 * if no handlers are connected, in contrast to g_signal_emitv().
3361 g_signal_emit (gpointer instance,
3368 va_start (var_args, detail);
3369 g_signal_emit_valist (instance, signal_id, detail, var_args);
3374 * g_signal_emit_by_name:
3375 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3376 * @detailed_signal: a string of the form "signal-name::detail".
3377 * @...: parameters to be passed to the signal, followed by a
3378 * location for the return value. If the return type of the signal
3379 * is #G_TYPE_NONE, the return value location can be omitted.
3383 * Note that g_signal_emit_by_name() resets the return value to the default
3384 * if no handlers are connected, in contrast to g_signal_emitv().
3387 g_signal_emit_by_name (gpointer instance,
3388 const gchar *detailed_signal,
3395 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3396 g_return_if_fail (detailed_signal != NULL);
3398 itype = G_TYPE_FROM_INSTANCE (instance);
3401 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3408 va_start (var_args, detailed_signal);
3409 g_signal_emit_valist (instance, signal_id, detail, var_args);
3413 g_warning ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3414 G_STRLOC, detailed_signal, instance, g_type_name (itype));
3418 signal_emit_unlocked_R (SignalNode *node,
3421 GValue *emission_return,
3422 const GValue *instance_and_params)
3424 SignalAccumulator *accumulator;
3426 GClosure *class_closure;
3428 Handler *handler_list = NULL;
3429 GValue *return_accu, accu = G_VALUE_INIT;
3431 gulong max_sequential_handler_number;
3432 gboolean return_value_altered = FALSE;
3434 TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3437 signal_id = node->signal_id;
3439 if (node->flags & G_SIGNAL_NO_RECURSE)
3441 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3445 node->state = EMISSION_RESTART;
3447 return return_value_altered;
3450 accumulator = node->accumulator;
3454 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3455 return_accu = &accu;
3459 return_accu = emission_return;
3460 emission.instance = instance;
3461 emission.ihint.signal_id = node->signal_id;
3462 emission.ihint.detail = detail;
3463 emission.ihint.run_type = 0;
3465 emission.chain_type = G_TYPE_NONE;
3466 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3467 class_closure = signal_lookup_closure (node, instance);
3472 handler_unref_R (signal_id, instance, handler_list);
3473 max_sequential_handler_number = g_handler_sequential_number;
3474 hlist = handler_list_lookup (signal_id, instance);
3475 handler_list = hlist ? hlist->handlers : NULL;
3477 handler_ref (handler_list);
3479 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3481 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3483 emission.state = EMISSION_RUN;
3485 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3487 g_closure_invoke (class_closure,
3490 instance_and_params,
3492 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3493 emission.state == EMISSION_RUN)
3494 emission.state = EMISSION_STOP;
3496 emission.chain_type = G_TYPE_NONE;
3497 return_value_altered = TRUE;
3499 if (emission.state == EMISSION_STOP)
3501 else if (emission.state == EMISSION_RESTART)
3505 if (node->emission_hooks)
3507 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3510 emission.state = EMISSION_HOOK;
3511 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3514 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3516 if (!signal_hook->detail || signal_hook->detail == detail)
3518 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3520 was_in_call = G_HOOK_IN_CALL (hook);
3521 hook->flags |= G_HOOK_FLAG_IN_CALL;
3523 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3526 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3528 g_hook_destroy_link (node->emission_hooks, hook);
3530 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3533 if (emission.state == EMISSION_RESTART)
3539 Handler *handler = handler_list;
3541 emission.state = EMISSION_RUN;
3542 handler_ref (handler);
3549 handler_unref_R (signal_id, instance, handler_list);
3550 handler_list = handler;
3553 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3554 handler->sequential_number < max_sequential_handler_number)
3557 g_closure_invoke (handler->closure,
3560 instance_and_params,
3562 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3563 emission.state == EMISSION_RUN)
3564 emission.state = EMISSION_STOP;
3566 return_value_altered = TRUE;
3568 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3571 tmp = handler->next;
3575 handler_unref_R (signal_id, instance, handler_list);
3576 handler_list = handler;
3581 if (emission.state == EMISSION_STOP)
3583 else if (emission.state == EMISSION_RESTART)
3587 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3589 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3591 emission.state = EMISSION_RUN;
3593 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3595 g_closure_invoke (class_closure,
3598 instance_and_params,
3600 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3601 emission.state == EMISSION_RUN)
3602 emission.state = EMISSION_STOP;
3604 emission.chain_type = G_TYPE_NONE;
3605 return_value_altered = TRUE;
3607 if (emission.state == EMISSION_STOP)
3609 else if (emission.state == EMISSION_RESTART)
3615 Handler *handler = handler_list;
3617 emission.state = EMISSION_RUN;
3618 handler_ref (handler);
3623 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3624 handler->sequential_number < max_sequential_handler_number)
3627 g_closure_invoke (handler->closure,
3630 instance_and_params,
3632 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3633 emission.state == EMISSION_RUN)
3634 emission.state = EMISSION_STOP;
3636 return_value_altered = TRUE;
3638 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3641 tmp = handler->next;
3645 handler_unref_R (signal_id, instance, handler);
3650 if (emission.state == EMISSION_STOP)
3652 else if (emission.state == EMISSION_RESTART)
3658 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3660 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3662 gboolean need_unset = FALSE;
3664 emission.state = EMISSION_STOP;
3666 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3668 if (node->return_type != G_TYPE_NONE && !accumulator)
3670 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3673 g_closure_invoke (class_closure,
3674 node->return_type != G_TYPE_NONE ? &accu : NULL,
3676 instance_and_params,
3679 g_value_unset (&accu);
3681 emission.chain_type = G_TYPE_NONE;
3683 if (emission.state == EMISSION_RESTART)
3688 handler_unref_R (signal_id, instance, handler_list);
3690 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3693 g_value_unset (&accu);
3695 TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3697 return return_value_altered;
3701 add_invalid_closure_notify (Handler *handler,
3704 g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3705 handler->has_invalid_closure_notify = 1;
3709 remove_invalid_closure_notify (Handler *handler,
3712 if (handler->has_invalid_closure_notify)
3714 g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3715 handler->has_invalid_closure_notify = 0;
3720 invalid_closure_notify (gpointer instance,
3728 handler = handler_lookup (instance, 0, closure, &signal_id);
3729 g_assert (handler->closure == closure);
3731 handler->sequential_number = 0;
3732 handler->block_count = 1;
3733 handler_unref_R (signal_id, instance, handler);
3739 type_debug_name (GType type)
3743 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3744 return name ? name : "<unknown>";
3751 * g_signal_accumulator_true_handled:
3752 * @ihint: standard #GSignalAccumulator parameter
3753 * @return_accu: standard #GSignalAccumulator parameter
3754 * @handler_return: standard #GSignalAccumulator parameter
3755 * @dummy: standard #GSignalAccumulator parameter
3757 * A predefined #GSignalAccumulator for signals that return a
3758 * boolean values. The behavior that this accumulator gives is
3759 * that a return of %TRUE stops the signal emission: no further
3760 * callbacks will be invoked, while a return of %FALSE allows
3761 * the emission to continue. The idea here is that a %TRUE return
3762 * indicates that the callback handled the signal, and no further
3763 * handling is needed.
3767 * Returns: standard #GSignalAccumulator result
3770 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3771 GValue *return_accu,
3772 const GValue *handler_return,
3775 gboolean continue_emission;
3776 gboolean signal_handled;
3778 signal_handled = g_value_get_boolean (handler_return);
3779 g_value_set_boolean (return_accu, signal_handled);
3780 continue_emission = !signal_handled;
3782 return continue_emission;
3786 * g_signal_accumulator_first_wins:
3787 * @ihint: standard #GSignalAccumulator parameter
3788 * @return_accu: standard #GSignalAccumulator parameter
3789 * @handler_return: standard #GSignalAccumulator parameter
3790 * @dummy: standard #GSignalAccumulator parameter
3792 * A predefined #GSignalAccumulator for signals intended to be used as a
3793 * hook for application code to provide a particular value. Usually
3794 * only one such value is desired and multiple handlers for the same
3795 * signal don't make much sense (except for the case of the default
3796 * handler defined in the class structure, in which case you will
3797 * usually want the signal connection to override the class handler).
3799 * This accumulator will use the return value from the first signal
3800 * handler that is run as the return value for the signal and not run
3801 * any further handlers (ie: the first handler "wins").
3803 * Returns: standard #GSignalAccumulator result
3808 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
3809 GValue *return_accu,
3810 const GValue *handler_return,
3813 g_value_copy (handler_return, return_accu);