1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
33 #include "gtype-private.h"
34 #include "gbsearcharray.h"
35 #include "gvaluecollector.h"
36 #include "gvaluetypes.h"
39 #include "gobject_trace.h"
44 * @short_description: A means for customization of object behaviour
45 * and a general purpose notification mechanism
48 * The basic concept of the signal system is that of the emission
49 * of a signal. Signals are introduced per-type and are identified
50 * through strings. Signals introduced for a parent type are available
51 * in derived types as well, so basically they are a per-type facility
54 * A signal emission mainly involves invocation of a certain set of
55 * callbacks in precisely defined manner. There are two main categories
56 * of such callbacks, per-object ones and user provided ones.
57 * (Although signals can deal with any kind of instantiatable type, I'm
58 * referring to those types as "object types" in the following, simply
59 * because that is the context most users will encounter signals in.)
60 * The per-object callbacks are most often referred to as "object method
61 * handler" or "default (signal) handler", while user provided callbacks are
62 * usually just called "signal handler".
64 * The object method handler is provided at signal creation time (this most
65 * frequently happens at the end of an object class' creation), while user
66 * provided handlers are frequently connected and disconnected to/from a
67 * certain signal on certain object instances.
69 * A signal emission consists of five stages, unless prematurely stopped:
71 * 1. Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
73 * 2. Invocation of normal user-provided signal handlers (where the @after
76 * 3. Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
78 * 4. Invocation of user provided signal handlers (where the @after flag is set)
80 * 5. Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
82 * The user-provided signal handlers are called in the order they were
85 * All handlers may prematurely stop a signal emission, and any number of
86 * handlers may be connected, disconnected, blocked or unblocked during
89 * There are certain criteria for skipping user handlers in stages 2 and 4
90 * of a signal emission.
92 * First, user handlers may be blocked. Blocked handlers are omitted during
93 * callback invocation, to return from the blocked state, a handler has to
94 * get unblocked exactly the same amount of times it has been blocked before.
96 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
97 * @detail argument passed in to g_signal_emit() has to match the detail
98 * argument of the signal handler currently subject to invocation.
99 * Specification of no detail argument for signal handlers (omission of the
100 * detail part of the signal specification upon connection) serves as a
101 * wildcard and matches any detail argument passed in to emission.
103 * While the @detail argument is typically used to pass an object property name
104 * (as with #GObject::notify), no specific format is mandated for the detail
105 * string, other than that it must be non-empty.
107 * ## Memory management of signal handlers # {#signal-memory-management}
109 * If you are connecting handlers to signals and using a #GObject instance as
110 * your signal handler user data, you should remember to pair calls to
111 * g_signal_connect() with calls to g_signal_handler_disconnect() or
112 * g_signal_handlers_disconnect_by_func(). While signal handlers are
113 * automatically disconnected when the object emitting the signal is finalised,
114 * they are not automatically disconnected when the signal handler user data is
115 * destroyed. If this user data is a #GObject instance, using it from a
116 * signal handler after it has been finalised is an error.
118 * There are two strategies for managing such user data. The first is to
119 * disconnect the signal handler (using g_signal_handler_disconnect() or
120 * g_signal_handlers_disconnect_by_func()) when the user data (object) is
121 * finalised; this has to be implemented manually. For non-threaded programs,
122 * g_signal_connect_object() can be used to implement this automatically.
123 * Currently, however, it is unsafe to use in threaded programs.
125 * The second is to hold a strong reference on the user data until after the
126 * signal is disconnected for other reasons. This can be implemented
127 * automatically using g_signal_connect_data().
129 * The first approach is recommended, as the second approach can result in
130 * effective memory leaks of the user data if the signal handler is never
131 * disconnected for some reason.
135 #define REPORT_BUG "please report occurrence circumstances to https://gitlab.gnome.org/GNOME/glib/issues/new"
137 /* --- typedefs --- */
138 typedef struct _SignalNode SignalNode;
139 typedef struct _SignalKey SignalKey;
140 typedef struct _Emission Emission;
141 typedef struct _Handler Handler;
142 typedef struct _HandlerList HandlerList;
143 typedef struct _HandlerMatch HandlerMatch;
153 /* --- prototypes --- */
154 static inline guint signal_id_lookup (const gchar *name,
156 static void signal_destroy_R (SignalNode *signal_node);
157 static inline HandlerList* handler_list_ensure (guint signal_id,
159 static inline HandlerList* handler_list_lookup (guint signal_id,
161 static inline Handler* handler_new (guint signal_id,
164 static void handler_insert (guint signal_id,
167 static Handler* handler_lookup (gpointer instance,
171 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
174 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
176 static HandlerMatch* handlers_find (gpointer instance,
177 GSignalMatchType mask,
183 gboolean one_and_only);
184 static inline void handler_ref (Handler *handler);
185 static inline void handler_unref_R (guint signal_id,
188 static gint handler_lists_cmp (gconstpointer node1,
189 gconstpointer node2);
190 static inline void emission_push (Emission *emission);
191 static inline void emission_pop (Emission *emission);
192 static inline Emission* emission_find (guint signal_id,
195 static gint class_closures_cmp (gconstpointer node1,
196 gconstpointer node2);
197 static gint signal_key_cmp (gconstpointer node1,
198 gconstpointer node2);
199 static gboolean signal_emit_unlocked_R (SignalNode *node,
202 GValue *return_value,
203 const GValue *instance_and_params);
204 static void add_invalid_closure_notify (Handler *handler,
206 static void remove_invalid_closure_notify (Handler *handler,
208 static void invalid_closure_notify (gpointer data,
210 static const gchar * type_debug_name (GType type);
211 static void node_check_deprecated (const SignalNode *node);
212 static void node_update_single_va_closure (SignalNode *node);
215 /* --- structures --- */
218 GSignalAccumulator func;
226 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
230 /* permanent portion */
236 /* reinitializable portion */
239 guint single_va_closure_is_valid : 1;
240 guint single_va_closure_is_after : 1;
241 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
242 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
243 GBSearchArray *class_closure_bsa;
244 SignalAccumulator *accumulator;
245 GSignalCMarshaller c_marshaller;
246 GSignalCVaMarshaller va_marshaller;
247 GHookList *emission_hooks;
249 GClosure *single_va_closure;
252 #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1) /* indicates single_va_closure is valid but empty */
265 GSignalInvocationHint ihint;
274 Handler *tail_before; /* normal signal handlers are appended here */
275 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
280 gulong sequential_number;
286 guint block_count : 16;
287 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
289 guint has_invalid_closure_notify : 1;
302 GType instance_type; /* 0 for default closure */
307 /* --- variables --- */
308 static GBSearchArray *g_signal_key_bsa = NULL;
309 static const GBSearchConfig g_signal_key_bconfig = {
312 G_BSEARCH_ARRAY_ALIGN_POWER2,
314 static GBSearchConfig g_signal_hlbsa_bconfig = {
315 sizeof (HandlerList),
319 static GBSearchConfig g_class_closure_bconfig = {
320 sizeof (ClassClosure),
324 static GHashTable *g_handler_list_bsa_ht = NULL;
325 static Emission *g_emissions = NULL;
326 static gulong g_handler_sequential_number = 1;
327 static GHashTable *g_handlers = NULL;
329 G_LOCK_DEFINE_STATIC (g_signal_mutex);
330 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
331 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
334 /* --- signal nodes --- */
335 static guint g_n_signal_nodes = 0;
336 static SignalNode **g_signal_nodes = NULL;
338 static inline SignalNode*
339 LOOKUP_SIGNAL_NODE (guint signal_id)
341 if (signal_id < g_n_signal_nodes)
342 return g_signal_nodes[signal_id];
348 /* --- functions --- */
349 /* @key must have already been validated with is_valid()
350 * Modifies @key in place. */
352 canonicalize_key (gchar *key)
356 for (p = key; *p != 0; p++)
365 /* @key must have already been validated with is_valid() */
367 is_canonical (const gchar *key)
369 return (strchr (key, '_') == NULL);
373 * g_signal_is_valid_name:
374 * @name: the canonical name of the signal
376 * Validate a signal name. This can be useful for dynamically-generated signals
377 * which need to be validated at run-time before actually trying to create them.
379 * See [canonical parameter names][canonical-parameter-names] for details of
380 * the rules for valid names. The rules for signal names are the same as those
381 * for property names.
383 * Returns: %TRUE if @name is a valid signal name, %FALSE otherwise.
387 g_signal_is_valid_name (const gchar *name)
389 /* FIXME: We allow this, against our own documentation (the leading `-` is
390 * invalid), because GTK has historically used this. */
391 if (g_str_equal (name, "-gtk-private-changed"))
394 return g_param_spec_is_valid_name (name);
398 signal_id_lookup (const gchar *name,
402 GType *ifaces, type = itype;
406 quark = g_quark_try_string (name);
409 /* try looking up signals for this type and its ancestors */
412 SignalKey *signal_key;
415 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
418 return signal_key->signal_id;
420 type = g_type_parent (type);
424 /* no luck, try interfaces it exports */
425 ifaces = g_type_interfaces (itype, &n_ifaces);
428 SignalKey *signal_key;
430 key.itype = ifaces[n_ifaces];
431 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
436 return signal_key->signal_id;
441 /* If the @name is non-canonical, try again. This is the slow path — people
442 * should use canonical names in their queries if they want performance. */
443 if (!is_canonical (name))
446 gchar *name_copy = g_strdup (name);
447 canonicalize_key (name_copy);
449 signal_id = signal_id_lookup (name_copy, itype);
460 class_closures_cmp (gconstpointer node1,
463 const ClassClosure *c1 = node1, *c2 = node2;
465 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
469 handler_lists_cmp (gconstpointer node1,
472 const HandlerList *hlist1 = node1, *hlist2 = node2;
474 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
477 static inline HandlerList*
478 handler_list_ensure (guint signal_id,
481 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
484 key.signal_id = signal_id;
486 key.tail_before = NULL;
487 key.tail_after = NULL;
490 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
491 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
492 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
496 GBSearchArray *o = hlbsa;
498 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
500 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
502 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
505 static inline HandlerList*
506 handler_list_lookup (guint signal_id,
509 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
512 key.signal_id = signal_id;
514 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
518 handler_hash (gconstpointer key)
520 return (guint)((Handler*)key)->sequential_number;
524 handler_equal (gconstpointer a, gconstpointer b)
526 Handler *ha = (Handler *)a;
527 Handler *hb = (Handler *)b;
528 return (ha->sequential_number == hb->sequential_number) &&
529 (ha->instance == hb->instance);
533 handler_lookup (gpointer instance,
538 GBSearchArray *hlbsa;
543 key.sequential_number = handler_id;
544 key.instance = instance;
545 return g_hash_table_lookup (g_handlers, &key);
549 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
555 for (i = 0; i < hlbsa->n_nodes; i++)
557 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
560 for (handler = hlist->handlers; handler; handler = handler->next)
561 if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id))
564 *signal_id_p = hlist->signal_id;
574 static inline HandlerMatch*
575 handler_match_prepend (HandlerMatch *list,
581 node = g_slice_new (HandlerMatch);
582 node->handler = handler;
584 node->signal_id = signal_id;
585 handler_ref (handler);
589 static inline HandlerMatch*
590 handler_match_free1_R (HandlerMatch *node,
593 HandlerMatch *next = node->next;
595 handler_unref_R (node->signal_id, instance, node->handler);
596 g_slice_free (HandlerMatch, node);
602 handlers_find (gpointer instance,
603 GSignalMatchType mask,
609 gboolean one_and_only)
611 HandlerMatch *mlist = NULL;
613 if (mask & G_SIGNAL_MATCH_ID)
615 HandlerList *hlist = handler_list_lookup (signal_id, instance);
617 SignalNode *node = NULL;
619 if (mask & G_SIGNAL_MATCH_FUNC)
621 node = LOOKUP_SIGNAL_NODE (signal_id);
622 if (!node || !node->c_marshaller)
627 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
628 if (handler->sequential_number &&
629 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
630 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
631 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
632 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
633 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
634 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
635 ((GCClosure*) handler->closure)->callback == func)))
637 mlist = handler_match_prepend (mlist, handler, signal_id);
644 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
651 for (i = 0; i < hlbsa->n_nodes; i++)
653 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
654 SignalNode *node = NULL;
657 if (!(mask & G_SIGNAL_MATCH_FUNC))
659 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
660 if (!node->c_marshaller)
664 for (handler = hlist->handlers; handler; handler = handler->next)
665 if (handler->sequential_number &&
666 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
667 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
668 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
669 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
670 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
671 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
672 ((GCClosure*) handler->closure)->callback == func)))
674 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
685 static inline Handler*
686 handler_new (guint signal_id, gpointer instance, gboolean after)
688 Handler *handler = g_slice_new (Handler);
689 #ifndef G_DISABLE_CHECKS
690 if (g_handler_sequential_number < 1)
691 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
694 handler->sequential_number = g_handler_sequential_number++;
695 handler->prev = NULL;
696 handler->next = NULL;
698 handler->signal_id = signal_id;
699 handler->instance = instance;
700 handler->ref_count = 1;
701 handler->block_count = 0;
702 handler->after = after != FALSE;
703 handler->closure = NULL;
704 handler->has_invalid_closure_notify = 0;
706 g_hash_table_add (g_handlers, handler);
712 handler_ref (Handler *handler)
714 g_return_if_fail (handler->ref_count > 0);
716 handler->ref_count++;
720 handler_unref_R (guint signal_id,
724 g_return_if_fail (handler->ref_count > 0);
726 handler->ref_count--;
728 if (G_UNLIKELY (handler->ref_count == 0))
730 HandlerList *hlist = NULL;
733 handler->next->prev = handler->prev;
734 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
735 handler->prev->next = handler->next;
738 hlist = handler_list_lookup (signal_id, instance);
739 g_assert (hlist != NULL);
740 hlist->handlers = handler->next;
745 /* check if we are removing the handler pointed to by tail_before */
746 if (!handler->after && (!handler->next || handler->next->after))
749 hlist = handler_list_lookup (signal_id, instance);
752 g_assert (hlist->tail_before == handler); /* paranoid */
753 hlist->tail_before = handler->prev;
757 /* check if we are removing the handler pointed to by tail_after */
761 hlist = handler_list_lookup (signal_id, instance);
764 g_assert (hlist->tail_after == handler); /* paranoid */
765 hlist->tail_after = handler->prev;
771 g_closure_unref (handler->closure);
773 g_slice_free (Handler, handler);
778 handler_insert (guint signal_id,
784 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
786 hlist = handler_list_ensure (signal_id, instance);
787 if (!hlist->handlers)
789 hlist->handlers = handler;
791 hlist->tail_before = handler;
793 else if (handler->after)
795 handler->prev = hlist->tail_after;
796 hlist->tail_after->next = handler;
800 if (hlist->tail_before)
802 handler->next = hlist->tail_before->next;
804 handler->next->prev = handler;
805 handler->prev = hlist->tail_before;
806 hlist->tail_before->next = handler;
808 else /* insert !after handler into a list of only after handlers */
810 handler->next = hlist->handlers;
812 handler->next->prev = handler;
813 hlist->handlers = handler;
815 hlist->tail_before = handler;
819 hlist->tail_after = handler;
823 node_update_single_va_closure (SignalNode *node)
825 GClosure *closure = NULL;
826 gboolean is_after = FALSE;
828 /* Fast path single-handler without boxing the arguments in GValues */
829 if (G_TYPE_IS_OBJECT (node->itype) &&
830 (node->flags & (G_SIGNAL_MUST_COLLECT)) == 0 &&
831 (node->emission_hooks == NULL || node->emission_hooks->hooks == NULL))
833 GSignalFlags run_type;
835 GBSearchArray *bsa = node->class_closure_bsa;
837 if (bsa == NULL || bsa->n_nodes == 0)
838 closure = SINGLE_VA_CLOSURE_EMPTY_MAGIC;
839 else if (bsa->n_nodes == 1)
841 /* Look for default class closure (can't support non-default as it
842 chains up using GValues */
843 cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
844 if (cc->instance_type == 0)
846 run_type = node->flags & (G_SIGNAL_RUN_FIRST|G_SIGNAL_RUN_LAST|G_SIGNAL_RUN_CLEANUP);
847 /* Only support *one* of run-first or run-last, not multiple or cleanup */
848 if (run_type == G_SIGNAL_RUN_FIRST ||
849 run_type == G_SIGNAL_RUN_LAST)
851 closure = cc->closure;
852 is_after = (run_type == G_SIGNAL_RUN_LAST);
858 node->single_va_closure_is_valid = TRUE;
859 node->single_va_closure = closure;
860 node->single_va_closure_is_after = is_after;
864 emission_push (Emission *emission)
866 emission->next = g_emissions;
867 g_emissions = emission;
871 emission_pop (Emission *emission)
873 Emission *node, *last = NULL;
875 for (node = g_emissions; node; last = node, node = last->next)
876 if (node == emission)
879 last->next = node->next;
881 g_emissions = node->next;
884 g_assert_not_reached ();
887 static inline Emission*
888 emission_find (guint signal_id,
894 for (emission = g_emissions; emission; emission = emission->next)
895 if (emission->instance == instance &&
896 emission->ihint.signal_id == signal_id &&
897 emission->ihint.detail == detail)
902 static inline Emission*
903 emission_find_innermost (gpointer instance)
907 for (emission = g_emissions; emission; emission = emission->next)
908 if (emission->instance == instance)
915 signal_key_cmp (gconstpointer node1,
918 const SignalKey *key1 = node1, *key2 = node2;
920 if (key1->itype == key2->itype)
921 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
923 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
927 _g_signal_init (void)
930 if (!g_n_signal_nodes)
932 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
933 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
934 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
936 /* invalid (0) signal_id */
937 g_n_signal_nodes = 1;
938 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
939 g_signal_nodes[0] = NULL;
940 g_handlers = g_hash_table_new (handler_hash, handler_equal);
946 _g_signals_destroy (GType itype)
951 for (i = 1; i < g_n_signal_nodes; i++)
953 SignalNode *node = g_signal_nodes[i];
955 if (node->itype == itype)
958 g_critical (G_STRLOC ": signal \"%s\" of type '%s' already destroyed",
960 type_debug_name (node->itype));
962 signal_destroy_R (node);
969 * g_signal_stop_emission:
970 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
971 * @signal_id: the signal identifier, as returned by g_signal_lookup().
972 * @detail: the detail which the signal was emitted with.
974 * Stops a signal's current emission.
976 * This will prevent the default method from running, if the signal was
977 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
980 * Prints a warning if used on a signal which isn't being emitted.
983 g_signal_stop_emission (gpointer instance,
989 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
990 g_return_if_fail (signal_id > 0);
993 node = LOOKUP_SIGNAL_NODE (signal_id);
994 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
996 g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1000 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1002 Emission *emission = emission_find (signal_id, detail, instance);
1006 if (emission->state == EMISSION_HOOK)
1007 g_critical (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1008 node->name, instance);
1009 else if (emission->state == EMISSION_RUN)
1010 emission->state = EMISSION_STOP;
1013 g_critical (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1014 node->name, instance);
1017 g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
1022 signal_finalize_hook (GHookList *hook_list,
1025 GDestroyNotify destroy = hook->destroy;
1029 hook->destroy = NULL;
1031 destroy (hook->data);
1037 * g_signal_add_emission_hook:
1038 * @signal_id: the signal identifier, as returned by g_signal_lookup().
1039 * @detail: the detail on which to call the hook.
1040 * @hook_func: (not nullable): a #GSignalEmissionHook function.
1041 * @hook_data: (nullable) (closure hook_func): user data for @hook_func.
1042 * @data_destroy: (nullable) (destroy hook_data): a #GDestroyNotify for @hook_data.
1044 * Adds an emission hook for a signal, which will get called for any emission
1045 * of that signal, independent of the instance. This is possible only
1046 * for signals which don't have %G_SIGNAL_NO_HOOKS flag set.
1048 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
1051 g_signal_add_emission_hook (guint signal_id,
1053 GSignalEmissionHook hook_func,
1055 GDestroyNotify data_destroy)
1057 static gulong seq_hook_id = 1;
1060 SignalHook *signal_hook;
1062 g_return_val_if_fail (signal_id > 0, 0);
1063 g_return_val_if_fail (hook_func != NULL, 0);
1066 node = LOOKUP_SIGNAL_NODE (signal_id);
1067 if (!node || node->destroyed)
1069 g_critical ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1073 if (node->flags & G_SIGNAL_NO_HOOKS)
1075 g_critical ("%s: signal id '%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
1079 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1081 g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1085 node->single_va_closure_is_valid = FALSE;
1086 if (!node->emission_hooks)
1088 node->emission_hooks = g_new (GHookList, 1);
1089 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
1090 node->emission_hooks->finalize_hook = signal_finalize_hook;
1093 node_check_deprecated (node);
1095 hook = g_hook_alloc (node->emission_hooks);
1096 hook->data = hook_data;
1097 hook->func = (gpointer) hook_func;
1098 hook->destroy = data_destroy;
1099 signal_hook = SIGNAL_HOOK (hook);
1100 signal_hook->detail = detail;
1101 node->emission_hooks->seq_id = seq_hook_id;
1102 g_hook_append (node->emission_hooks, hook);
1103 seq_hook_id = node->emission_hooks->seq_id;
1107 return hook->hook_id;
1111 * g_signal_remove_emission_hook:
1112 * @signal_id: the id of the signal
1113 * @hook_id: the id of the emission hook, as returned by
1114 * g_signal_add_emission_hook()
1116 * Deletes an emission hook.
1119 g_signal_remove_emission_hook (guint signal_id,
1124 g_return_if_fail (signal_id > 0);
1125 g_return_if_fail (hook_id > 0);
1128 node = LOOKUP_SIGNAL_NODE (signal_id);
1129 if (!node || node->destroyed)
1131 g_critical ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1134 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
1135 g_critical ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
1137 node->single_va_closure_is_valid = FALSE;
1144 signal_parse_name (const gchar *name,
1147 gboolean force_quark)
1149 const gchar *colon = strchr (name, ':');
1154 signal_id = signal_id_lookup (name, itype);
1155 if (signal_id && detail_p)
1158 else if (colon[1] == ':')
1161 guint l = colon - name;
1163 if (colon[2] == '\0')
1168 memcpy (buffer, name, l);
1170 signal_id = signal_id_lookup (buffer, itype);
1174 gchar *signal = g_new (gchar, l + 1);
1176 memcpy (signal, name, l);
1178 signal_id = signal_id_lookup (signal, itype);
1182 if (signal_id && detail_p)
1183 *detail_p = (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2);
1191 * g_signal_parse_name:
1192 * @detailed_signal: a string of the form "signal-name::detail".
1193 * @itype: The interface/instance type that introduced "signal-name".
1194 * @signal_id_p: (out): Location to store the signal id.
1195 * @detail_p: (out): Location to store the detail quark.
1196 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1198 * Internal function to parse a signal name into its @signal_id
1199 * and @detail quark.
1201 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1204 g_signal_parse_name (const gchar *detailed_signal,
1208 gboolean force_detail_quark)
1214 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1215 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1218 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1220 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1222 if (!node || node->destroyed ||
1223 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1232 *signal_id_p = signal_id;
1240 * g_signal_stop_emission_by_name:
1241 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
1242 * @detailed_signal: a string of the form "signal-name::detail".
1244 * Stops a signal's current emission.
1246 * This is just like g_signal_stop_emission() except it will look up the
1247 * signal id for you.
1250 g_signal_stop_emission_by_name (gpointer instance,
1251 const gchar *detailed_signal)
1257 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1258 g_return_if_fail (detailed_signal != NULL);
1261 itype = G_TYPE_FROM_INSTANCE (instance);
1262 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1265 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1267 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1268 g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
1269 else if (!g_type_is_a (itype, node->itype))
1270 g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1271 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1274 Emission *emission = emission_find (signal_id, detail, instance);
1278 if (emission->state == EMISSION_HOOK)
1279 g_critical (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1280 node->name, instance);
1281 else if (emission->state == EMISSION_RUN)
1282 emission->state = EMISSION_STOP;
1285 g_critical (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1286 node->name, instance);
1290 g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1291 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1297 * @name: the signal's name.
1298 * @itype: the type that the signal operates on.
1300 * Given the name of the signal and the type of object it connects to, gets
1301 * the signal's identifying integer. Emitting the signal by number is
1302 * somewhat faster than using the name each time.
1304 * Also tries the ancestors of the given type.
1306 * The type class passed as @itype must already have been instantiated (for
1307 * example, using g_type_class_ref()) for this function to work, as signals are
1308 * always installed during class initialization.
1310 * See g_signal_new() for details on allowed signal names.
1312 * Returns: the signal's identifying number, or 0 if no signal was found.
1315 g_signal_lookup (const gchar *name,
1319 g_return_val_if_fail (name != NULL, 0);
1320 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1323 signal_id = signal_id_lookup (name, itype);
1327 /* give elaborate warnings */
1328 if (!g_type_name (itype))
1329 g_critical (G_STRLOC ": unable to look up signal \"%s\" for invalid type id '%"G_GSIZE_FORMAT"'",
1331 else if (!g_signal_is_valid_name (name))
1332 g_critical (G_STRLOC ": unable to look up invalid signal name \"%s\" on type '%s'",
1333 name, g_type_name (itype));
1340 * g_signal_list_ids:
1341 * @itype: Instance or interface type.
1342 * @n_ids: Location to store the number of signal ids for @itype.
1344 * Lists the signals by id that a certain instance or interface type
1345 * created. Further information about the signals can be acquired through
1348 * Returns: (array length=n_ids) (transfer full): Newly allocated array of signal IDs.
1351 g_signal_list_ids (GType itype,
1359 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1360 g_return_val_if_fail (n_ids != NULL, NULL);
1363 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1364 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1365 result = g_array_new (FALSE, FALSE, sizeof (guint));
1367 for (i = 0; i < n_nodes; i++)
1368 if (keys[i].itype == itype)
1370 g_array_append_val (result, keys[i].signal_id);
1372 *n_ids = result->len;
1376 /* give elaborate warnings */
1377 if (!g_type_name (itype))
1378 g_critical (G_STRLOC ": unable to list signals for invalid type id '%"G_GSIZE_FORMAT"'",
1380 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1381 g_critical (G_STRLOC ": unable to list signals of non instantiatable type '%s'",
1382 g_type_name (itype));
1383 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1384 g_critical (G_STRLOC ": unable to list signals of unloaded type '%s'",
1385 g_type_name (itype));
1388 return (guint*) g_array_free (result, FALSE);
1393 * @signal_id: the signal's identifying number.
1395 * Given the signal's identifier, finds its name.
1397 * Two different signals may have the same name, if they have differing types.
1399 * Returns: (nullable): the signal name, or %NULL if the signal number was invalid.
1402 g_signal_name (guint signal_id)
1408 node = LOOKUP_SIGNAL_NODE (signal_id);
1409 name = node ? node->name : NULL;
1412 return (char*) name;
1417 * @signal_id: The signal id of the signal to query information for.
1418 * @query: (out caller-allocates) (not optional): A user provided structure that is
1419 * filled in with constant values upon success.
1421 * Queries the signal system for in-depth information about a
1422 * specific signal. This function will fill in a user-provided
1423 * structure to hold signal-specific information. If an invalid
1424 * signal id is passed in, the @signal_id member of the #GSignalQuery
1425 * is 0. All members filled into the #GSignalQuery structure should
1426 * be considered constant and have to be left untouched.
1429 g_signal_query (guint signal_id,
1430 GSignalQuery *query)
1434 g_return_if_fail (query != NULL);
1437 node = LOOKUP_SIGNAL_NODE (signal_id);
1438 if (!node || node->destroyed)
1439 query->signal_id = 0;
1442 query->signal_id = node->signal_id;
1443 query->signal_name = node->name;
1444 query->itype = node->itype;
1445 query->signal_flags = node->flags;
1446 query->return_type = node->return_type;
1447 query->n_params = node->n_params;
1448 query->param_types = node->param_types;
1455 * @signal_name: the name for the signal
1456 * @itype: the type this signal pertains to. It will also pertain to
1457 * types which are derived from this type.
1458 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1459 * the default handler is to be invoked. You should at least specify
1460 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1461 * @class_offset: The offset of the function pointer in the class structure
1462 * for this type. Used to invoke a class method generically. Pass 0 to
1463 * not associate a class method slot with this signal.
1464 * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1465 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1466 * @c_marshaller: (nullable): the function to translate arrays of parameter
1467 * values to signal emissions into C language callback invocations or %NULL.
1468 * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1469 * without a return value.
1470 * @n_params: the number of parameter types to follow.
1471 * @...: a list of types, one for each parameter.
1473 * Creates a new signal. (This is usually done in the class initializer.)
1475 * A signal name consists of segments consisting of ASCII letters and
1476 * digits, separated by either the `-` or `_` character. The first
1477 * character of a signal name must be a letter. Names which violate these
1478 * rules lead to undefined behaviour. These are the same rules as for property
1479 * naming (see g_param_spec_internal()).
1481 * When registering a signal and looking up a signal, either separator can
1482 * be used, but they cannot be mixed. Using `-` is considerably more efficient.
1483 * Using `_` is discouraged.
1485 * If 0 is used for @class_offset subclasses cannot override the class handler
1486 * in their class_init method by doing super_class->signal_handler = my_signal_handler.
1487 * Instead they will have to use g_signal_override_class_handler().
1489 * If @c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1490 * the marshaller for this signal. In some simple cases, g_signal_new()
1491 * will use a more optimized c_marshaller and va_marshaller for the signal
1492 * instead of g_cclosure_marshal_generic().
1494 * If @c_marshaller is non-%NULL, you need to also specify a va_marshaller
1495 * using g_signal_set_va_marshaller() or the generic va_marshaller will
1498 * Returns: the signal id
1501 g_signal_new (const gchar *signal_name,
1503 GSignalFlags signal_flags,
1505 GSignalAccumulator accumulator,
1507 GSignalCMarshaller c_marshaller,
1515 g_return_val_if_fail (signal_name != NULL, 0);
1517 va_start (args, n_params);
1519 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1520 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1521 accumulator, accu_data, c_marshaller,
1522 return_type, n_params, args);
1530 * g_signal_new_class_handler:
1531 * @signal_name: the name for the signal
1532 * @itype: the type this signal pertains to. It will also pertain to
1533 * types which are derived from this type.
1534 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1535 * the default handler is to be invoked. You should at least specify
1536 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1537 * @class_handler: (nullable): a #GCallback which acts as class implementation of
1538 * this signal. Used to invoke a class method generically. Pass %NULL to
1539 * not associate a class method with this signal.
1540 * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1541 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1542 * @c_marshaller: (nullable): the function to translate arrays of parameter
1543 * values to signal emissions into C language callback invocations or %NULL.
1544 * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1545 * without a return value.
1546 * @n_params: the number of parameter types to follow.
1547 * @...: a list of types, one for each parameter.
1549 * Creates a new signal. (This is usually done in the class initializer.)
1551 * This is a variant of g_signal_new() that takes a C callback instead
1552 * of a class offset for the signal's class handler. This function
1553 * doesn't need a function pointer exposed in the class structure of
1554 * an object definition, instead the function pointer is passed
1555 * directly and can be overridden by derived classes with
1556 * g_signal_override_class_closure() or
1557 * g_signal_override_class_handler() and chained to with
1558 * g_signal_chain_from_overridden() or
1559 * g_signal_chain_from_overridden_handler().
1561 * See g_signal_new() for information about signal names.
1563 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1564 * the marshaller for this signal.
1566 * Returns: the signal id
1571 g_signal_new_class_handler (const gchar *signal_name,
1573 GSignalFlags signal_flags,
1574 GCallback class_handler,
1575 GSignalAccumulator accumulator,
1577 GSignalCMarshaller c_marshaller,
1585 g_return_val_if_fail (signal_name != NULL, 0);
1587 va_start (args, n_params);
1589 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1590 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1591 accumulator, accu_data, c_marshaller,
1592 return_type, n_params, args);
1599 static inline ClassClosure*
1600 signal_find_class_closure (SignalNode *node,
1603 GBSearchArray *bsa = node->class_closure_bsa;
1610 /* cc->instance_type is 0 for default closure */
1612 if (g_bsearch_array_get_n_nodes (bsa) == 1)
1614 cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
1615 if (cc && cc->instance_type == 0) /* check for default closure */
1619 key.instance_type = itype;
1620 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1621 while (!cc && key.instance_type)
1623 key.instance_type = g_type_parent (key.instance_type);
1624 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1632 static inline GClosure*
1633 signal_lookup_closure (SignalNode *node,
1634 GTypeInstance *instance)
1638 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1639 return cc ? cc->closure : NULL;
1643 signal_add_class_closure (SignalNode *node,
1649 node->single_va_closure_is_valid = FALSE;
1651 if (!node->class_closure_bsa)
1652 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1653 key.instance_type = itype;
1654 key.closure = g_closure_ref (closure);
1655 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1656 &g_class_closure_bconfig,
1658 g_closure_sink (closure);
1659 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1661 g_closure_set_marshal (closure, node->c_marshaller);
1662 if (node->va_marshaller)
1663 _g_closure_set_va_marshal (closure, node->va_marshaller);
1669 * @signal_name: the name for the signal
1670 * @itype: the type this signal pertains to. It will also pertain to
1671 * types which are derived from this type
1672 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1673 * the default handler is to be invoked. You should at least specify
1674 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1675 * @class_closure: (nullable): The closure to invoke on signal emission;
1677 * @accumulator: (nullable): the accumulator for this signal; may be %NULL
1678 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator
1679 * @c_marshaller: (nullable): the function to translate arrays of
1680 * parameter values to signal emissions into C language callback
1681 * invocations or %NULL
1682 * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1683 * without a return value
1684 * @n_params: the length of @param_types
1685 * @param_types: (array length=n_params) (nullable): an array of types, one for
1686 * each parameter (may be %NULL if @n_params is zero)
1688 * Creates a new signal. (This is usually done in the class initializer.)
1690 * See g_signal_new() for details on allowed signal names.
1692 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1693 * the marshaller for this signal.
1695 * Returns: the signal id
1698 g_signal_newv (const gchar *signal_name,
1700 GSignalFlags signal_flags,
1701 GClosure *class_closure,
1702 GSignalAccumulator accumulator,
1704 GSignalCMarshaller c_marshaller,
1710 gchar *signal_name_copy = NULL;
1713 GSignalCMarshaller builtin_c_marshaller;
1714 GSignalCVaMarshaller builtin_va_marshaller;
1715 GSignalCVaMarshaller va_marshaller;
1717 g_return_val_if_fail (signal_name != NULL, 0);
1718 g_return_val_if_fail (g_signal_is_valid_name (signal_name), 0);
1719 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1721 g_return_val_if_fail (param_types != NULL, 0);
1722 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1723 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1724 g_return_val_if_fail (accumulator == NULL, 0);
1726 g_return_val_if_fail (accu_data == NULL, 0);
1727 g_return_val_if_fail ((signal_flags & G_SIGNAL_ACCUMULATOR_FIRST_RUN) == 0, 0);
1729 if (!is_canonical (signal_name))
1731 signal_name_copy = g_strdup (signal_name);
1732 canonicalize_key (signal_name_copy);
1733 name = signal_name_copy;
1742 signal_id = signal_id_lookup (name, itype);
1743 node = LOOKUP_SIGNAL_NODE (signal_id);
1744 if (node && !node->destroyed)
1746 g_critical (G_STRLOC ": signal \"%s\" already exists in the '%s' %s",
1748 type_debug_name (node->itype),
1749 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1750 g_free (signal_name_copy);
1754 if (node && node->itype != itype)
1756 g_critical (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'",
1758 type_debug_name (itype),
1759 type_debug_name (node->itype));
1760 g_free (signal_name_copy);
1764 for (i = 0; i < n_params; i++)
1765 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1767 g_critical (G_STRLOC ": parameter %d of type '%s' for signal \"%s::%s\" is not a value type",
1768 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1769 g_free (signal_name_copy);
1773 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1775 g_critical (G_STRLOC ": return value of type '%s' for signal \"%s::%s\" is not a value type",
1776 type_debug_name (return_type), type_debug_name (itype), name);
1777 g_free (signal_name_copy);
1782 /* setup permanent portion of signal node */
1787 signal_id = g_n_signal_nodes++;
1788 node = g_new (SignalNode, 1);
1789 node->signal_id = signal_id;
1790 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1791 g_signal_nodes[signal_id] = node;
1792 node->itype = itype;
1794 key.signal_id = signal_id;
1795 node->name = g_intern_string (name);
1796 key.quark = g_quark_from_string (name);
1797 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1799 TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1801 node->destroyed = FALSE;
1803 /* setup reinitializable portion */
1804 node->single_va_closure_is_valid = FALSE;
1805 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1806 node->n_params = n_params;
1807 node->param_types = g_memdup2 (param_types, sizeof (GType) * n_params);
1808 node->return_type = return_type;
1809 node->class_closure_bsa = NULL;
1812 node->accumulator = g_new (SignalAccumulator, 1);
1813 node->accumulator->func = accumulator;
1814 node->accumulator->data = accu_data;
1817 node->accumulator = NULL;
1819 builtin_c_marshaller = NULL;
1820 builtin_va_marshaller = NULL;
1822 /* Pick up built-in va marshallers for standard types, and
1823 instead of generic marshaller if no marshaller specified */
1824 if (n_params == 0 && return_type == G_TYPE_NONE)
1826 builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1827 builtin_va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1829 else if (n_params == 1 && return_type == G_TYPE_NONE)
1831 #define ADD_CHECK(__type__) \
1832 else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \
1834 builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \
1835 builtin_va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \
1858 if (c_marshaller == NULL)
1860 if (builtin_c_marshaller)
1862 c_marshaller = builtin_c_marshaller;
1863 va_marshaller = builtin_va_marshaller;
1867 c_marshaller = g_cclosure_marshal_generic;
1868 va_marshaller = g_cclosure_marshal_generic_va;
1872 va_marshaller = NULL;
1874 node->c_marshaller = c_marshaller;
1875 node->va_marshaller = va_marshaller;
1876 node->emission_hooks = NULL;
1878 signal_add_class_closure (node, 0, class_closure);
1882 g_free (signal_name_copy);
1888 * g_signal_set_va_marshaller:
1889 * @signal_id: the signal id
1890 * @instance_type: the instance type on which to set the marshaller.
1891 * @va_marshaller: the marshaller to set.
1893 * Change the #GSignalCVaMarshaller used for a given signal. This is a
1894 * specialised form of the marshaller that can often be used for the
1895 * common case of a single connected signal handler and avoids the
1896 * overhead of #GValue. Its use is optional.
1901 g_signal_set_va_marshaller (guint signal_id,
1902 GType instance_type,
1903 GSignalCVaMarshaller va_marshaller)
1907 g_return_if_fail (signal_id > 0);
1908 g_return_if_fail (va_marshaller != NULL);
1911 node = LOOKUP_SIGNAL_NODE (signal_id);
1914 node->va_marshaller = va_marshaller;
1915 if (node->class_closure_bsa)
1917 ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1918 if (cc->closure->marshal == node->c_marshaller)
1919 _g_closure_set_va_marshal (cc->closure, va_marshaller);
1922 node->single_va_closure_is_valid = FALSE;
1930 * g_signal_new_valist:
1931 * @signal_name: the name for the signal
1932 * @itype: the type this signal pertains to. It will also pertain to
1933 * types which are derived from this type.
1934 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1935 * the default handler is to be invoked. You should at least specify
1936 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1937 * @class_closure: (nullable): The closure to invoke on signal emission; may be %NULL.
1938 * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1939 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1940 * @c_marshaller: (nullable): the function to translate arrays of parameter
1941 * values to signal emissions into C language callback invocations or %NULL.
1942 * @return_type: the type of return value, or %G_TYPE_NONE for a signal
1943 * without a return value.
1944 * @n_params: the number of parameter types in @args.
1945 * @args: va_list of #GType, one for each parameter.
1947 * Creates a new signal. (This is usually done in the class initializer.)
1949 * See g_signal_new() for details on allowed signal names.
1951 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1952 * the marshaller for this signal.
1954 * Returns: the signal id
1957 g_signal_new_valist (const gchar *signal_name,
1959 GSignalFlags signal_flags,
1960 GClosure *class_closure,
1961 GSignalAccumulator accumulator,
1963 GSignalCMarshaller c_marshaller,
1968 /* Somewhat arbitrarily reserve 200 bytes. That should cover the majority
1969 * of cases where n_params is small and still be small enough for what we
1970 * want to put on the stack. */
1971 GType param_types_stack[200 / sizeof (GType)];
1972 GType *param_types_heap = NULL;
1977 param_types = param_types_stack;
1980 if (G_UNLIKELY (n_params > G_N_ELEMENTS (param_types_stack)))
1982 param_types_heap = g_new (GType, n_params);
1983 param_types = param_types_heap;
1986 for (i = 0; i < n_params; i++)
1987 param_types[i] = va_arg (args, GType);
1990 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1991 class_closure, accumulator, accu_data, c_marshaller,
1992 return_type, n_params, param_types);
1993 g_free (param_types_heap);
1999 signal_destroy_R (SignalNode *signal_node)
2001 SignalNode node = *signal_node;
2003 signal_node->destroyed = TRUE;
2005 /* reentrancy caution, zero out real contents first */
2006 signal_node->single_va_closure_is_valid = FALSE;
2007 signal_node->n_params = 0;
2008 signal_node->param_types = NULL;
2009 signal_node->return_type = 0;
2010 signal_node->class_closure_bsa = NULL;
2011 signal_node->accumulator = NULL;
2012 signal_node->c_marshaller = NULL;
2013 signal_node->va_marshaller = NULL;
2014 signal_node->emission_hooks = NULL;
2016 #ifdef G_ENABLE_DEBUG
2017 /* check current emissions */
2021 for (emission = g_emissions; emission; emission = emission->next)
2022 if (emission->ihint.signal_id == node.signal_id)
2023 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')",
2024 node.name, emission->instance);
2028 /* free contents that need to
2031 g_free (node.param_types);
2032 if (node.class_closure_bsa)
2036 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
2038 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
2040 g_closure_unref (cc->closure);
2042 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
2044 g_free (node.accumulator);
2045 if (node.emission_hooks)
2047 g_hook_list_clear (node.emission_hooks);
2048 g_free (node.emission_hooks);
2054 * g_signal_override_class_closure:
2055 * @signal_id: the signal id
2056 * @instance_type: the instance type on which to override the class closure
2058 * @class_closure: the closure.
2060 * Overrides the class closure (i.e. the default handler) for the given signal
2061 * for emissions on instances of @instance_type. @instance_type must be derived
2062 * from the type to which the signal belongs.
2064 * See g_signal_chain_from_overridden() and
2065 * g_signal_chain_from_overridden_handler() for how to chain up to the
2066 * parent class closure from inside the overridden one.
2069 g_signal_override_class_closure (guint signal_id,
2070 GType instance_type,
2071 GClosure *class_closure)
2075 g_return_if_fail (signal_id > 0);
2076 g_return_if_fail (class_closure != NULL);
2079 node = LOOKUP_SIGNAL_NODE (signal_id);
2080 node_check_deprecated (node);
2081 if (!g_type_is_a (instance_type, node->itype))
2082 g_critical ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
2085 ClassClosure *cc = signal_find_class_closure (node, instance_type);
2087 if (cc && cc->instance_type == instance_type)
2088 g_critical ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
2090 signal_add_class_closure (node, instance_type, class_closure);
2096 * g_signal_override_class_handler:
2097 * @signal_name: the name for the signal
2098 * @instance_type: the instance type on which to override the class handler
2100 * @class_handler: the handler.
2102 * Overrides the class closure (i.e. the default handler) for the
2103 * given signal for emissions on instances of @instance_type with
2104 * callback @class_handler. @instance_type must be derived from the
2105 * type to which the signal belongs.
2107 * See g_signal_chain_from_overridden() and
2108 * g_signal_chain_from_overridden_handler() for how to chain up to the
2109 * parent class closure from inside the overridden one.
2114 g_signal_override_class_handler (const gchar *signal_name,
2115 GType instance_type,
2116 GCallback class_handler)
2120 g_return_if_fail (signal_name != NULL);
2121 g_return_if_fail (instance_type != G_TYPE_NONE);
2122 g_return_if_fail (class_handler != NULL);
2124 signal_id = g_signal_lookup (signal_name, instance_type);
2127 g_signal_override_class_closure (signal_id, instance_type,
2128 g_cclosure_new (class_handler, NULL, NULL));
2130 g_critical ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
2131 G_STRLOC, signal_name, instance_type);
2136 * g_signal_chain_from_overridden:
2137 * @instance_and_params: (array) the argument list of the signal emission.
2138 * The first element in the array is a #GValue for the instance the signal
2139 * is being emitted on. The rest are any arguments to be passed to the signal.
2140 * @return_value: Location for the return value.
2142 * Calls the original class closure of a signal. This function should only
2143 * be called from an overridden class closure; see
2144 * g_signal_override_class_closure() and
2145 * g_signal_override_class_handler().
2148 g_signal_chain_from_overridden (const GValue *instance_and_params,
2149 GValue *return_value)
2151 GType chain_type = 0, restore_type = 0;
2152 Emission *emission = NULL;
2153 GClosure *closure = NULL;
2157 g_return_if_fail (instance_and_params != NULL);
2158 instance = g_value_peek_pointer (instance_and_params);
2159 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2162 emission = emission_find_innermost (instance);
2165 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2167 g_assert (node != NULL); /* paranoid */
2169 /* we should probably do the same parameter checks as g_signal_emit() here.
2171 if (emission->chain_type != G_TYPE_NONE)
2173 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2175 g_assert (cc != NULL); /* closure currently in call stack */
2177 n_params = node->n_params;
2178 restore_type = cc->instance_type;
2179 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2180 if (cc && cc->instance_type != restore_type)
2182 closure = cc->closure;
2183 chain_type = cc->instance_type;
2187 g_critical ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2190 g_critical ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2194 emission->chain_type = chain_type;
2196 g_closure_invoke (closure,
2199 instance_and_params,
2202 emission->chain_type = restore_type;
2208 * g_signal_chain_from_overridden_handler: (skip)
2209 * @instance: (type GObject.TypeInstance): the instance the signal is being
2211 * @...: parameters to be passed to the parent class closure, followed by a
2212 * location for the return value. If the return type of the signal
2213 * is %G_TYPE_NONE, the return value location can be omitted.
2215 * Calls the original class closure of a signal. This function should
2216 * only be called from an overridden class closure; see
2217 * g_signal_override_class_closure() and
2218 * g_signal_override_class_handler().
2223 g_signal_chain_from_overridden_handler (gpointer instance,
2226 GType chain_type = 0, restore_type = 0;
2227 Emission *emission = NULL;
2228 GClosure *closure = NULL;
2229 SignalNode *node = NULL;
2232 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2235 emission = emission_find_innermost (instance);
2238 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2240 g_assert (node != NULL); /* paranoid */
2242 /* we should probably do the same parameter checks as g_signal_emit() here.
2244 if (emission->chain_type != G_TYPE_NONE)
2246 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2248 g_assert (cc != NULL); /* closure currently in call stack */
2250 n_params = node->n_params;
2251 restore_type = cc->instance_type;
2252 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2253 if (cc && cc->instance_type != restore_type)
2255 closure = cc->closure;
2256 chain_type = cc->instance_type;
2260 g_critical ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2263 g_critical ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2267 GValue *instance_and_params;
2268 GType signal_return_type;
2269 GValue *param_values;
2273 va_start (var_args, instance);
2275 signal_return_type = node->return_type;
2276 instance_and_params = g_newa0 (GValue, n_params + 1);
2277 param_values = instance_and_params + 1;
2279 for (i = 0; i < node->n_params; i++)
2282 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2283 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2286 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2288 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2292 g_critical ("%s: %s", G_STRLOC, error);
2295 /* we purposely leak the value here, it might not be
2296 * in a correct state if an error condition occurred
2299 g_value_unset (param_values + i);
2308 g_value_init_from_instance (instance_and_params, instance);
2311 emission->chain_type = chain_type;
2314 if (signal_return_type == G_TYPE_NONE)
2316 g_closure_invoke (closure,
2319 instance_and_params,
2324 GValue return_value = G_VALUE_INIT;
2325 gchar *error = NULL;
2326 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2327 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2329 g_value_init (&return_value, rtype);
2331 g_closure_invoke (closure,
2334 instance_and_params,
2337 G_VALUE_LCOPY (&return_value,
2339 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2343 g_value_unset (&return_value);
2347 g_critical ("%s: %s", G_STRLOC, error);
2350 /* we purposely leak the value here, it might not be
2351 * in a correct state if an error condition occurred
2356 for (i = 0; i < n_params; i++)
2357 g_value_unset (param_values + i);
2358 g_value_unset (instance_and_params);
2363 emission->chain_type = restore_type;
2369 * g_signal_get_invocation_hint:
2370 * @instance: (type GObject.Object): the instance to query
2372 * Returns the invocation hint of the innermost signal emission of instance.
2374 * Returns: (transfer none) (nullable): the invocation hint of the innermost
2375 * signal emission, or %NULL if not found.
2377 GSignalInvocationHint*
2378 g_signal_get_invocation_hint (gpointer instance)
2380 Emission *emission = NULL;
2382 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2385 emission = emission_find_innermost (instance);
2388 return emission ? &emission->ihint : NULL;
2392 * g_signal_connect_closure_by_id:
2393 * @instance: (type GObject.Object): the instance to connect to.
2394 * @signal_id: the id of the signal.
2395 * @detail: the detail.
2396 * @closure: (not nullable): the closure to connect.
2397 * @after: whether the handler should be called before or after the
2398 * default handler of the signal.
2400 * Connects a closure to a signal for a particular object.
2402 * If @closure is a floating reference (see g_closure_sink()), this function
2403 * takes ownership of @closure.
2405 * Returns: the handler ID (always greater than 0 for successful connections)
2408 g_signal_connect_closure_by_id (gpointer instance,
2415 gulong handler_seq_no = 0;
2417 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2418 g_return_val_if_fail (signal_id > 0, 0);
2419 g_return_val_if_fail (closure != NULL, 0);
2422 node = LOOKUP_SIGNAL_NODE (signal_id);
2425 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2426 g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2427 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2428 g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2431 Handler *handler = handler_new (signal_id, instance, after);
2433 if (G_TYPE_IS_OBJECT (node->itype))
2434 _g_object_set_has_signal_handler ((GObject *) instance, signal_id);
2436 handler_seq_no = handler->sequential_number;
2437 handler->detail = detail;
2438 handler->closure = g_closure_ref (closure);
2439 g_closure_sink (closure);
2440 add_invalid_closure_notify (handler, instance);
2441 handler_insert (signal_id, instance, handler);
2442 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2444 g_closure_set_marshal (closure, node->c_marshaller);
2445 if (node->va_marshaller)
2446 _g_closure_set_va_marshal (closure, node->va_marshaller);
2451 g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2454 return handler_seq_no;
2458 * g_signal_connect_closure:
2459 * @instance: (type GObject.Object): the instance to connect to.
2460 * @detailed_signal: a string of the form "signal-name::detail".
2461 * @closure: (not nullable): the closure to connect.
2462 * @after: whether the handler should be called before or after the
2463 * default handler of the signal.
2465 * Connects a closure to a signal for a particular object.
2467 * If @closure is a floating reference (see g_closure_sink()), this function
2468 * takes ownership of @closure.
2470 * Returns: the handler ID (always greater than 0 for successful connections)
2473 g_signal_connect_closure (gpointer instance,
2474 const gchar *detailed_signal,
2479 gulong handler_seq_no = 0;
2483 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2484 g_return_val_if_fail (detailed_signal != NULL, 0);
2485 g_return_val_if_fail (closure != NULL, 0);
2488 itype = G_TYPE_FROM_INSTANCE (instance);
2489 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2492 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2494 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2495 g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2496 else if (!g_type_is_a (itype, node->itype))
2497 g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2498 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2501 Handler *handler = handler_new (signal_id, instance, after);
2503 if (G_TYPE_IS_OBJECT (node->itype))
2504 _g_object_set_has_signal_handler ((GObject *) instance, signal_id);
2506 handler_seq_no = handler->sequential_number;
2507 handler->detail = detail;
2508 handler->closure = g_closure_ref (closure);
2509 g_closure_sink (closure);
2510 add_invalid_closure_notify (handler, instance);
2511 handler_insert (signal_id, instance, handler);
2512 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2514 g_closure_set_marshal (handler->closure, node->c_marshaller);
2515 if (node->va_marshaller)
2516 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2521 g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2522 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2525 return handler_seq_no;
2529 node_check_deprecated (const SignalNode *node)
2531 static const gchar * g_enable_diagnostic = NULL;
2533 if (G_UNLIKELY (!g_enable_diagnostic))
2535 g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2536 if (!g_enable_diagnostic)
2537 g_enable_diagnostic = "0";
2540 if (g_enable_diagnostic[0] == '1')
2542 if (node->flags & G_SIGNAL_DEPRECATED)
2544 g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2545 "anymore. It will be removed in a future version.",
2546 type_debug_name (node->itype), node->name);
2552 * g_signal_connect_data:
2553 * @instance: (type GObject.Object): the instance to connect to.
2554 * @detailed_signal: a string of the form "signal-name::detail".
2555 * @c_handler: (not nullable): the #GCallback to connect.
2556 * @data: (nullable) (closure c_handler): data to pass to @c_handler calls.
2557 * @destroy_data: (nullable) (destroy data): a #GClosureNotify for @data.
2558 * @connect_flags: a combination of #GConnectFlags.
2560 * Connects a #GCallback function to a signal for a particular object. Similar
2561 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2562 * which will be called when the signal handler is disconnected and no longer
2563 * used. Specify @connect_flags if you need `..._after()` or
2564 * `..._swapped()` variants of this function.
2566 * Returns: the handler ID (always greater than 0 for successful connections)
2569 g_signal_connect_data (gpointer instance,
2570 const gchar *detailed_signal,
2571 GCallback c_handler,
2573 GClosureNotify destroy_data,
2574 GConnectFlags connect_flags)
2577 gulong handler_seq_no = 0;
2580 gboolean swapped, after;
2582 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2583 g_return_val_if_fail (detailed_signal != NULL, 0);
2584 g_return_val_if_fail (c_handler != NULL, 0);
2586 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2587 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2590 itype = G_TYPE_FROM_INSTANCE (instance);
2591 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2594 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2596 node_check_deprecated (node);
2598 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2599 g_critical ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2600 else if (!g_type_is_a (itype, node->itype))
2601 g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2602 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2605 Handler *handler = handler_new (signal_id, instance, after);
2607 if (G_TYPE_IS_OBJECT (node->itype))
2608 _g_object_set_has_signal_handler ((GObject *) instance, signal_id);
2610 handler_seq_no = handler->sequential_number;
2611 handler->detail = detail;
2612 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2613 g_closure_sink (handler->closure);
2614 handler_insert (signal_id, instance, handler);
2615 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2617 g_closure_set_marshal (handler->closure, node->c_marshaller);
2618 if (node->va_marshaller)
2619 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2624 g_critical ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2625 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2628 return handler_seq_no;
2632 signal_handler_block_unlocked (gpointer instance,
2636 * g_signal_handler_block:
2637 * @instance: (type GObject.Object): The instance to block the signal handler of.
2638 * @handler_id: Handler id of the handler to be blocked.
2640 * Blocks a handler of an instance so it will not be called during any
2641 * signal emissions unless it is unblocked again. Thus "blocking" a
2642 * signal handler means to temporarily deactivate it, a signal handler
2643 * has to be unblocked exactly the same amount of times it has been
2644 * blocked before to become active again.
2646 * The @handler_id has to be a valid signal handler id, connected to a
2647 * signal of @instance.
2650 g_signal_handler_block (gpointer instance,
2653 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2654 g_return_if_fail (handler_id > 0);
2657 signal_handler_block_unlocked (instance, handler_id);
2662 signal_handler_block_unlocked (gpointer instance,
2667 handler = handler_lookup (instance, handler_id, NULL, NULL);
2670 #ifndef G_DISABLE_CHECKS
2671 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2672 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2674 handler->block_count += 1;
2677 g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2681 signal_handler_unblock_unlocked (gpointer instance,
2685 * g_signal_handler_unblock:
2686 * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2687 * @handler_id: Handler id of the handler to be unblocked.
2689 * Undoes the effect of a previous g_signal_handler_block() call. A
2690 * blocked handler is skipped during signal emissions and will not be
2691 * invoked, unblocking it (for exactly the amount of times it has been
2692 * blocked before) reverts its "blocked" state, so the handler will be
2693 * recognized by the signal system and is called upon future or
2694 * currently ongoing signal emissions (since the order in which
2695 * handlers are called during signal emissions is deterministic,
2696 * whether the unblocked handler in question is called as part of a
2697 * currently ongoing emission depends on how far that emission has
2700 * The @handler_id has to be a valid id of a signal handler that is
2701 * connected to a signal of @instance and is currently blocked.
2704 g_signal_handler_unblock (gpointer instance,
2707 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2708 g_return_if_fail (handler_id > 0);
2711 signal_handler_unblock_unlocked (instance, handler_id);
2716 signal_handler_unblock_unlocked (gpointer instance,
2721 handler = handler_lookup (instance, handler_id, NULL, NULL);
2724 if (handler->block_count)
2725 handler->block_count -= 1;
2727 g_critical (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2730 g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2734 signal_handler_disconnect_unlocked (gpointer instance,
2738 * g_signal_handler_disconnect:
2739 * @instance: (type GObject.Object): The instance to remove the signal handler from.
2740 * @handler_id: Handler id of the handler to be disconnected.
2742 * Disconnects a handler from an instance so it will not be called during
2743 * any future or currently ongoing emissions of the signal it has been
2744 * connected to. The @handler_id becomes invalid and may be reused.
2746 * The @handler_id has to be a valid signal handler id, connected to a
2747 * signal of @instance.
2750 g_signal_handler_disconnect (gpointer instance,
2753 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2754 g_return_if_fail (handler_id > 0);
2757 signal_handler_disconnect_unlocked (instance, handler_id);
2762 signal_handler_disconnect_unlocked (gpointer instance,
2767 handler = handler_lookup (instance, handler_id, 0, 0);
2770 g_hash_table_remove (g_handlers, handler);
2771 handler->sequential_number = 0;
2772 handler->block_count = 1;
2773 remove_invalid_closure_notify (handler, instance);
2774 handler_unref_R (handler->signal_id, instance, handler);
2777 g_critical ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2781 * g_signal_handler_is_connected:
2782 * @instance: (type GObject.Object): The instance where a signal handler is sought.
2783 * @handler_id: the handler ID.
2785 * Returns whether @handler_id is the ID of a handler connected to @instance.
2787 * Returns: whether @handler_id identifies a handler connected to @instance.
2790 g_signal_handler_is_connected (gpointer instance,
2796 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2799 handler = handler_lookup (instance, handler_id, NULL, NULL);
2800 connected = handler != NULL;
2807 * g_signal_handlers_destroy:
2808 * @instance: (type GObject.Object): The instance whose signal handlers are destroyed
2810 * Destroy all signal handlers of a type instance. This function is
2811 * an implementation detail of the #GObject dispose implementation,
2812 * and should not be used outside of the type system.
2815 g_signal_handlers_destroy (gpointer instance)
2817 GBSearchArray *hlbsa;
2819 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2822 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2827 /* reentrancy caution, delete instance trace first */
2828 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2830 for (i = 0; i < hlbsa->n_nodes; i++)
2832 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2833 Handler *handler = hlist->handlers;
2837 Handler *tmp = handler;
2839 handler = tmp->next;
2840 tmp->block_count = 1;
2841 /* cruel unlink, this works because _all_ handlers vanish */
2844 if (tmp->sequential_number)
2846 g_hash_table_remove (g_handlers, tmp);
2847 remove_invalid_closure_notify (tmp, instance);
2848 tmp->sequential_number = 0;
2849 handler_unref_R (0, NULL, tmp);
2853 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2859 * g_signal_handler_find:
2860 * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2861 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2862 * and/or @data the handler has to match.
2863 * @signal_id: Signal the handler has to be connected to.
2864 * @detail: Signal detail the handler has to be connected to.
2865 * @closure: (nullable): The closure the handler will invoke.
2866 * @func: The C closure callback of the handler (useless for non-C closures).
2867 * @data: (nullable) (closure closure): The closure data of the handler's closure.
2869 * Finds the first signal handler that matches certain selection criteria.
2870 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2871 * flags, and the criteria values are passed as arguments.
2872 * The match @mask has to be non-0 for successful matches.
2873 * If no handler was found, 0 is returned.
2875 * Returns: A valid non-0 signal handler id for a successful match.
2878 g_signal_handler_find (gpointer instance,
2879 GSignalMatchType mask,
2886 gulong handler_seq_no = 0;
2888 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2889 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2891 if (mask & G_SIGNAL_MATCH_MASK)
2893 HandlerMatch *mlist;
2896 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2899 handler_seq_no = mlist->handler->sequential_number;
2900 handler_match_free1_R (mlist, instance);
2905 return handler_seq_no;
2908 typedef void (*CallbackHandlerFunc) (gpointer instance, gulong handler_seq_no);
2911 signal_handlers_foreach_matched_unlocked_R (gpointer instance,
2912 GSignalMatchType mask,
2918 CallbackHandlerFunc callback)
2920 HandlerMatch *mlist;
2921 guint n_handlers = 0;
2923 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2927 if (mlist->handler->sequential_number)
2928 callback (instance, mlist->handler->sequential_number);
2930 mlist = handler_match_free1_R (mlist, instance);
2937 * g_signal_handlers_block_matched:
2938 * @instance: (type GObject.Object): The instance to block handlers from.
2939 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2940 * and/or @data the handlers have to match.
2941 * @signal_id: Signal the handlers have to be connected to.
2942 * @detail: Signal detail the handlers have to be connected to.
2943 * @closure: (nullable): The closure the handlers will invoke.
2944 * @func: The C closure callback of the handlers (useless for non-C closures).
2945 * @data: (nullable) (closure closure): The closure data of the handlers' closures.
2947 * Blocks all handlers on an instance that match a certain selection criteria.
2949 * The criteria mask is passed as a combination of #GSignalMatchType flags, and
2950 * the criteria values are passed as arguments. A handler must match on all
2951 * flags set in @mask to be blocked (i.e. the match is conjunctive).
2953 * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE,
2954 * %G_SIGNAL_MATCH_FUNC
2955 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2956 * If no handlers were found, 0 is returned, the number of blocked handlers
2959 * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78.
2961 * Returns: The number of handlers that matched.
2964 g_signal_handlers_block_matched (gpointer instance,
2965 GSignalMatchType mask,
2972 guint n_handlers = 0;
2974 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2975 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2977 if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2981 signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail,
2982 closure, func, data,
2983 signal_handler_block_unlocked);
2991 * g_signal_handlers_unblock_matched:
2992 * @instance: (type GObject.Object): The instance to unblock handlers from.
2993 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2994 * and/or @data the handlers have to match.
2995 * @signal_id: Signal the handlers have to be connected to.
2996 * @detail: Signal detail the handlers have to be connected to.
2997 * @closure: (nullable): The closure the handlers will invoke.
2998 * @func: The C closure callback of the handlers (useless for non-C closures).
2999 * @data: (nullable) (closure closure): The closure data of the handlers' closures.
3001 * Unblocks all handlers on an instance that match a certain selection
3004 * The criteria mask is passed as a combination of #GSignalMatchType flags, and
3005 * the criteria values are passed as arguments. A handler must match on all
3006 * flags set in @mask to be unblocked (i.e. the match is conjunctive).
3008 * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE,
3009 * %G_SIGNAL_MATCH_FUNC
3010 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
3011 * If no handlers were found, 0 is returned, the number of unblocked handlers
3012 * otherwise. The match criteria should not apply to any handlers that are
3013 * not currently blocked.
3015 * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78.
3017 * Returns: The number of handlers that matched.
3020 g_signal_handlers_unblock_matched (gpointer instance,
3021 GSignalMatchType mask,
3028 guint n_handlers = 0;
3030 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3031 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
3033 if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
3037 signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail,
3038 closure, func, data,
3039 signal_handler_unblock_unlocked);
3047 * g_signal_handlers_disconnect_matched:
3048 * @instance: (type GObject.Object): The instance to remove handlers from.
3049 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
3050 * and/or @data the handlers have to match.
3051 * @signal_id: Signal the handlers have to be connected to.
3052 * @detail: Signal detail the handlers have to be connected to.
3053 * @closure: (nullable): The closure the handlers will invoke.
3054 * @func: The C closure callback of the handlers (useless for non-C closures).
3055 * @data: (nullable) (closure closure): The closure data of the handlers' closures.
3057 * Disconnects all handlers on an instance that match a certain
3058 * selection criteria.
3060 * The criteria mask is passed as a combination of #GSignalMatchType flags, and
3061 * the criteria values are passed as arguments. A handler must match on all
3062 * flags set in @mask to be disconnected (i.e. the match is conjunctive).
3064 * Passing at least one of the %G_SIGNAL_MATCH_ID, %G_SIGNAL_MATCH_CLOSURE,
3065 * %G_SIGNAL_MATCH_FUNC or
3066 * %G_SIGNAL_MATCH_DATA match flags is required for successful
3067 * matches. If no handlers were found, 0 is returned, the number of
3068 * disconnected handlers otherwise.
3070 * Support for %G_SIGNAL_MATCH_ID was added in GLib 2.78.
3072 * Returns: The number of handlers that matched.
3075 g_signal_handlers_disconnect_matched (gpointer instance,
3076 GSignalMatchType mask,
3083 guint n_handlers = 0;
3085 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3086 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
3088 if (mask & (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
3092 signal_handlers_foreach_matched_unlocked_R (instance, mask, signal_id, detail,
3093 closure, func, data,
3094 signal_handler_disconnect_unlocked);
3102 * g_signal_has_handler_pending:
3103 * @instance: (type GObject.Object): the object whose signal handlers are sought.
3104 * @signal_id: the signal id.
3105 * @detail: the detail.
3106 * @may_be_blocked: whether blocked handlers should count as match.
3108 * Returns whether there are any handlers connected to @instance for the
3109 * given signal id and detail.
3111 * If @detail is 0 then it will only match handlers that were connected
3112 * without detail. If @detail is non-zero then it will match handlers
3113 * connected both without detail and with the given detail. This is
3114 * consistent with how a signal emitted with @detail would be delivered
3115 * to those handlers.
3117 * Since 2.46 this also checks for a non-default class closure being
3118 * installed, as this is basically always what you want.
3120 * One example of when you might use this is when the arguments to the
3121 * signal are difficult to compute. A class implementor may opt to not
3122 * emit the signal if no one is attached anyway, thus saving the cost
3123 * of building the arguments.
3125 * Returns: %TRUE if a handler is connected to the signal, %FALSE
3129 g_signal_has_handler_pending (gpointer instance,
3132 gboolean may_be_blocked)
3134 HandlerMatch *mlist;
3135 gboolean has_pending;
3138 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
3139 g_return_val_if_fail (signal_id > 0, FALSE);
3143 node = LOOKUP_SIGNAL_NODE (signal_id);
3146 if (!(node->flags & G_SIGNAL_DETAILED))
3148 g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3153 mlist = handlers_find (instance,
3154 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
3155 signal_id, detail, NULL, NULL, NULL, TRUE);
3159 handler_match_free1_R (mlist, instance);
3163 ClassClosure *class_closure = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
3164 if (class_closure != NULL && class_closure->instance_type != 0)
3167 has_pending = FALSE;
3175 signal_emitv_unlocked (const GValue *instance_and_params,
3178 GValue *return_value);
3182 * @instance_and_params: (array): argument list for the signal emission.
3183 * The first element in the array is a #GValue for the instance the signal
3184 * is being emitted on. The rest are any arguments to be passed to the signal.
3185 * @signal_id: the signal id
3186 * @detail: the detail
3187 * @return_value: (inout) (optional): Location to
3188 * store the return value of the signal emission. This must be provided if the
3189 * specified signal returns a value, but may be ignored otherwise.
3191 * Emits a signal. Signal emission is done synchronously.
3192 * The method will only return control after all handlers are called or signal emission was stopped.
3194 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
3195 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
3198 g_signal_emitv (const GValue *instance_and_params,
3201 GValue *return_value)
3204 signal_emitv_unlocked (instance_and_params, signal_id, detail, return_value);
3209 signal_emitv_unlocked (const GValue *instance_and_params,
3212 GValue *return_value)
3216 #ifdef G_ENABLE_DEBUG
3217 const GValue *param_values;
3221 g_return_if_fail (instance_and_params != NULL);
3222 instance = g_value_peek_pointer (instance_and_params);
3223 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3224 g_return_if_fail (signal_id > 0);
3226 #ifdef G_ENABLE_DEBUG
3227 param_values = instance_and_params + 1;
3230 node = LOOKUP_SIGNAL_NODE (signal_id);
3231 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3233 g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3236 #ifdef G_ENABLE_DEBUG
3237 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3239 g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3242 for (i = 0; i < node->n_params; i++)
3243 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3245 g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
3247 type_debug_name (node->param_types[i]),
3250 G_VALUE_TYPE_NAME (param_values + i));
3253 if (node->return_type != G_TYPE_NONE)
3257 g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3259 type_debug_name (node->return_type),
3263 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3265 g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3267 type_debug_name (node->return_type),
3269 G_VALUE_TYPE_NAME (return_value));
3274 return_value = NULL;
3275 #endif /* G_ENABLE_DEBUG */
3277 /* optimize NOP emissions */
3278 if (!node->single_va_closure_is_valid)
3279 node_update_single_va_closure (node);
3281 if (node->single_va_closure != NULL &&
3282 (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3283 _g_closure_is_void (node->single_va_closure, instance)))
3287 /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */
3288 if (_g_object_has_signal_handler ((GObject *)instance))
3289 hlist = handler_list_lookup (node->signal_id, instance);
3293 if (hlist == NULL || hlist->handlers == NULL)
3295 /* nothing to do to emit this signal */
3296 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3301 /* Pass a stable node pointer, whose address can't change even if the
3302 * g_signal_nodes array gets reallocated. */
3303 SignalNode node_copy = *node;
3304 signal_emit_unlocked_R (&node_copy, detail, instance, return_value, instance_and_params);
3307 static inline gboolean
3308 accumulate (GSignalInvocationHint *ihint,
3309 GValue *return_accu,
3310 GValue *handler_return,
3311 SignalAccumulator *accumulator)
3313 gboolean continue_emission;
3318 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3319 g_value_reset (handler_return);
3321 ihint->run_type &= ~G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3323 return continue_emission;
3327 signal_emit_valist_unlocked (gpointer instance,
3333 * g_signal_emit_valist: (skip)
3334 * @instance: (type GObject.TypeInstance): the instance the signal is being
3336 * @signal_id: the signal id
3337 * @detail: the detail
3338 * @var_args: a list of parameters to be passed to the signal, followed by a
3339 * location for the return value. If the return type of the signal
3340 * is %G_TYPE_NONE, the return value location can be omitted.
3342 * Emits a signal. Signal emission is done synchronously.
3343 * The method will only return control after all handlers are called or signal emission was stopped.
3345 * Note that g_signal_emit_valist() resets the return value to the default
3346 * if no handlers are connected, in contrast to g_signal_emitv().
3349 g_signal_emit_valist (gpointer instance,
3355 if (signal_emit_valist_unlocked (instance, signal_id, detail, var_args))
3360 * signal_emit_valist_unlocked:
3361 * @instance: The instance to emit from
3362 * @signal_id: Signal id to emit
3363 * @detail: Signal detail
3364 * @var_args: Call arguments
3366 * Returns: %TRUE if the signal mutex has been left locked
3369 signal_emit_valist_unlocked (gpointer instance,
3374 GValue *instance_and_params;
3375 GValue *param_values;
3379 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), TRUE);
3380 g_return_val_if_fail (signal_id > 0, TRUE);
3382 node = LOOKUP_SIGNAL_NODE (signal_id);
3383 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3385 g_critical ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3388 #ifndef G_DISABLE_CHECKS
3389 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3391 g_critical ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3394 #endif /* !G_DISABLE_CHECKS */
3396 if (!node->single_va_closure_is_valid)
3397 node_update_single_va_closure (node);
3399 /* There's no need to deep copy this, because a SignalNode instance won't
3400 * ever be destroyed, given that _g_signals_destroy() is not called in any
3401 * real program, however the SignalNode pointer could change, so just store
3402 * the struct contents references, so that we won't try to deference a
3403 * potentially invalid (or changed) pointer;
3405 SignalNode node_copy = *node;
3407 if (node->single_va_closure != NULL)
3410 Handler *fastpath_handler = NULL;
3412 GClosure *closure = NULL;
3413 gboolean fastpath = TRUE;
3414 GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3416 if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3417 !_g_closure_is_void (node->single_va_closure, instance))
3419 if (_g_closure_supports_invoke_va (node->single_va_closure))
3421 closure = node->single_va_closure;
3422 if (node->single_va_closure_is_after)
3423 run_type = G_SIGNAL_RUN_LAST;
3425 run_type = G_SIGNAL_RUN_FIRST;
3431 /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */
3432 if (_g_object_has_signal_handler ((GObject *)instance))
3433 hlist = handler_list_lookup (node->signal_id, instance);
3437 for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3439 if (!l->block_count &&
3440 (!l->detail || l->detail == detail))
3442 if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3449 fastpath_handler = l;
3450 closure = l->closure;
3452 run_type = G_SIGNAL_RUN_LAST;
3454 run_type = G_SIGNAL_RUN_FIRST;
3459 if (fastpath && closure == NULL && node_copy.return_type == G_TYPE_NONE)
3462 /* Don't allow no-recurse emission as we might have to restart, which means
3463 we will run multiple handlers and thus must ref all arguments */
3464 if (closure != NULL && (node_copy.flags & (G_SIGNAL_NO_RECURSE)) != 0)
3470 GValue *return_accu, accu = G_VALUE_INIT;
3471 GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3472 GValue emission_return = G_VALUE_INIT;
3473 GType rtype = node_copy.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3474 gboolean static_scope = node_copy.return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3476 if (rtype == G_TYPE_NONE)
3478 else if (node_copy.accumulator)
3479 return_accu = &accu;
3481 return_accu = &emission_return;
3483 emission.instance = instance;
3484 emission.ihint.signal_id = signal_id;
3485 emission.ihint.detail = detail;
3486 emission.ihint.run_type = run_type | G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3487 emission.state = EMISSION_RUN;
3488 emission.chain_type = instance_type;
3489 emission_push (&emission);
3491 if (fastpath_handler)
3492 handler_ref (fastpath_handler);
3494 if (closure != NULL)
3496 TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3500 if (rtype != G_TYPE_NONE)
3501 g_value_init (&emission_return, rtype);
3503 if (node_copy.accumulator)
3504 g_value_init (&accu, rtype);
3507 * Coverity doesn’t understand the paired ref/unref here and seems
3508 * to ignore the ref, thus reports every call to g_signal_emit()
3509 * as causing a double-free. That’s incorrect, but I can’t get a
3510 * model file to work for avoiding the false positives, so instead
3511 * comment out the ref/unref when doing static analysis.
3513 #ifndef __COVERITY__
3514 g_object_ref (instance);
3516 _g_closure_invoke_va (closure,
3521 node_copy.param_types);
3522 accumulate (&emission.ihint, &emission_return, &accu, node_copy.accumulator);
3524 if (node_copy.accumulator)
3525 g_value_unset (&accu);
3530 emission.chain_type = G_TYPE_NONE;
3531 emission_pop (&emission);
3533 if (fastpath_handler)
3534 handler_unref_R (signal_id, instance, fastpath_handler);
3538 if (rtype != G_TYPE_NONE)
3540 gchar *error = NULL;
3541 for (i = 0; i < node_copy.n_params; i++)
3543 GType ptype = node_copy.param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3544 G_VALUE_COLLECT_SKIP (ptype, var_args);
3547 if (closure == NULL)
3548 g_value_init (&emission_return, rtype);
3550 G_VALUE_LCOPY (&emission_return,
3552 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3555 g_value_unset (&emission_return);
3558 g_critical ("%s: %s", G_STRLOC, error);
3560 /* we purposely leak the value here, it might not be
3561 * in a correct state if an error condition occurred
3566 TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3568 /* See comment above paired ref above */
3569 #ifndef __COVERITY__
3570 if (closure != NULL)
3571 g_object_unref (instance);
3580 instance_and_params = g_newa0 (GValue, node_copy.n_params + 1);
3581 param_values = instance_and_params + 1;
3583 for (i = 0; i < node_copy.n_params; i++)
3586 GType ptype = node_copy.param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3587 gboolean static_scope = node_copy.param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3589 G_VALUE_COLLECT_INIT (param_values + i, ptype,
3591 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3595 g_critical ("%s: %s", G_STRLOC, error);
3598 /* we purposely leak the value here, it might not be
3599 * in a correct state if an error condition occurred
3602 g_value_unset (param_values + i);
3608 g_value_init_from_instance (instance_and_params, instance);
3609 if (node_copy.return_type == G_TYPE_NONE)
3612 signal_emit_unlocked_R (&node_copy, detail, instance, NULL, instance_and_params);
3617 GValue return_value = G_VALUE_INIT;
3618 gchar *error = NULL;
3619 GType rtype = node_copy.return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3620 gboolean static_scope = node_copy.return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3622 g_value_init (&return_value, rtype);
3625 signal_emit_unlocked_R (&node_copy, detail, instance, &return_value, instance_and_params);
3628 G_VALUE_LCOPY (&return_value,
3630 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3633 g_value_unset (&return_value);
3636 g_critical ("%s: %s", G_STRLOC, error);
3639 /* we purposely leak the value here, it might not be
3640 * in a correct state if an error condition occurred
3644 for (i = 0; i < node_copy.n_params; i++)
3645 g_value_unset (param_values + i);
3646 g_value_unset (instance_and_params);
3653 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3654 * @signal_id: the signal id
3655 * @detail: the detail
3656 * @...: parameters to be passed to the signal, followed by a
3657 * location for the return value. If the return type of the signal
3658 * is %G_TYPE_NONE, the return value location can be omitted.
3660 * Emits a signal. Signal emission is done synchronously.
3661 * The method will only return control after all handlers are called or signal emission was stopped.
3663 * Note that g_signal_emit() resets the return value to the default
3664 * if no handlers are connected, in contrast to g_signal_emitv().
3667 g_signal_emit (gpointer instance,
3674 va_start (var_args, detail);
3675 g_signal_emit_valist (instance, signal_id, detail, var_args);
3680 * g_signal_emit_by_name:
3681 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3682 * @detailed_signal: a string of the form "signal-name::detail".
3683 * @...: parameters to be passed to the signal, followed by a
3684 * location for the return value. If the return type of the signal
3685 * is %G_TYPE_NONE, the return value location can be omitted. The
3686 * number of parameters to pass to this function is defined when creating the signal.
3688 * Emits a signal. Signal emission is done synchronously.
3689 * The method will only return control after all handlers are called or signal emission was stopped.
3691 * Note that g_signal_emit_by_name() resets the return value to the default
3692 * if no handlers are connected, in contrast to g_signal_emitv().
3695 g_signal_emit_by_name (gpointer instance,
3696 const gchar *detailed_signal,
3703 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3704 g_return_if_fail (detailed_signal != NULL);
3706 itype = G_TYPE_FROM_INSTANCE (instance);
3709 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3715 va_start (var_args, detailed_signal);
3716 if (signal_emit_valist_unlocked (instance, signal_id, detail, var_args))
3724 g_critical ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3725 G_STRLOC, detailed_signal, instance, g_type_name (itype));
3729 G_ALWAYS_INLINE static inline GValue *
3730 maybe_init_accumulator_unlocked (SignalNode *node,
3731 GValue *emission_return,
3732 GValue *accumulator_value)
3734 if (node->accumulator)
3736 if (accumulator_value->g_type)
3737 return accumulator_value;
3739 g_value_init (accumulator_value,
3740 node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3741 return accumulator_value;
3744 return emission_return;
3748 signal_emit_unlocked_R (SignalNode *node,
3751 GValue *emission_return,
3752 const GValue *instance_and_params)
3754 SignalAccumulator *accumulator;
3756 GClosure *class_closure;
3758 Handler *handler_list = NULL;
3759 GValue *return_accu, accu = G_VALUE_INIT;
3761 gulong max_sequential_handler_number;
3762 gboolean return_value_altered = FALSE;
3765 TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3767 /* We expect this function to be called with a stable SignalNode pointer
3768 * that cannot change location, so accessing its stable members should
3769 * always work even after a lock/unlock.
3771 signal_id = node->signal_id;
3772 n_params = node->n_params + 1;
3774 if (node->flags & G_SIGNAL_NO_RECURSE)
3776 Emission *emission_node = emission_find (signal_id, detail, instance);
3780 emission_node->state = EMISSION_RESTART;
3781 return return_value_altered;
3784 accumulator = node->accumulator;
3785 emission.instance = instance;
3786 emission.ihint.signal_id = node->signal_id;
3787 emission.ihint.detail = detail;
3788 emission.ihint.run_type = 0;
3790 emission.chain_type = G_TYPE_NONE;
3791 emission_push (&emission);
3792 class_closure = signal_lookup_closure (node, instance);
3797 handler_unref_R (signal_id, instance, handler_list);
3798 max_sequential_handler_number = g_handler_sequential_number;
3799 hlist = handler_list_lookup (signal_id, instance);
3800 handler_list = hlist ? hlist->handlers : NULL;
3802 handler_ref (handler_list);
3804 emission.ihint.run_type = G_SIGNAL_RUN_FIRST | G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3806 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3808 emission.state = EMISSION_RUN;
3810 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3812 return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
3813 g_closure_invoke (class_closure,
3816 instance_and_params,
3818 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3819 emission.state == EMISSION_RUN)
3820 emission.state = EMISSION_STOP;
3822 emission.chain_type = G_TYPE_NONE;
3823 return_value_altered = TRUE;
3825 if (emission.state == EMISSION_STOP)
3827 else if (emission.state == EMISSION_RESTART)
3831 if (node->emission_hooks)
3834 GHook *static_emission_hooks[3];
3835 size_t n_emission_hooks = 0;
3836 const gboolean may_recurse = TRUE;
3839 emission.state = EMISSION_HOOK;
3841 /* Quick check to determine whether any hooks match this emission,
3842 * before committing to the more complex work of calling those hooks.
3843 * We save a few of them into a static array, to try to avoid further
3846 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3849 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3851 if (!signal_hook->detail || signal_hook->detail == detail)
3853 if (n_emission_hooks < G_N_ELEMENTS (static_emission_hooks))
3855 static_emission_hooks[n_emission_hooks] =
3856 g_hook_ref (node->emission_hooks, hook);
3859 n_emission_hooks += 1;
3862 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3865 /* Re-iterate back through the matching hooks and copy them into
3866 * an array which won’t change when we unlock to call the
3867 * user-provided hook functions.
3868 * These functions may change hook configuration for this signal,
3869 * add / remove signal handlers, etc.
3871 if G_UNLIKELY (n_emission_hooks > 0)
3873 guint8 static_hook_returns[G_N_ELEMENTS (static_emission_hooks)];
3874 GHook **emission_hooks = NULL;
3875 guint8 *hook_returns = NULL;
3877 if G_LIKELY (n_emission_hooks <= G_N_ELEMENTS (static_emission_hooks))
3879 emission_hooks = static_emission_hooks;
3880 hook_returns = static_hook_returns;
3884 emission_hooks = g_newa (GHook *, n_emission_hooks);
3885 hook_returns = g_newa (guint8, n_emission_hooks);
3887 /* We can't just memcpy the ones we have in the static array,
3888 * to the alloca()'d one because otherwise we'd get an invalid
3889 * ID assertion during unref
3892 for (hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3894 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse))
3896 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3898 if (!signal_hook->detail || signal_hook->detail == detail)
3900 if (i < G_N_ELEMENTS (static_emission_hooks))
3902 emission_hooks[i] = g_steal_pointer (&static_emission_hooks[i]);
3903 g_assert (emission_hooks[i] == hook);
3907 emission_hooks[i] = g_hook_ref (node->emission_hooks, hook);
3914 g_assert (i == n_emission_hooks);
3919 for (i = 0; i < n_emission_hooks; ++i)
3921 GSignalEmissionHook hook_func;
3922 gboolean need_destroy;
3925 hook = emission_hooks[i];
3926 hook_func = (GSignalEmissionHook) hook->func;
3928 old_flags = g_atomic_int_or (&hook->flags, G_HOOK_FLAG_IN_CALL);
3929 need_destroy = !hook_func (&emission.ihint, n_params,
3930 instance_and_params, hook->data);
3932 if (!(old_flags & G_HOOK_FLAG_IN_CALL))
3934 g_atomic_int_compare_and_exchange (&hook->flags,
3935 old_flags | G_HOOK_FLAG_IN_CALL,
3939 hook_returns[i] = !!need_destroy;
3944 for (i = 0; i < n_emission_hooks; i++)
3946 hook = emission_hooks[i];
3948 g_hook_unref (node->emission_hooks, hook);
3950 if (hook_returns[i])
3951 g_hook_destroy_link (node->emission_hooks, hook);
3955 if (emission.state == EMISSION_RESTART)
3961 Handler *handler = handler_list;
3963 emission.state = EMISSION_RUN;
3964 handler_ref (handler);
3971 handler_unref_R (signal_id, instance, handler_list);
3972 handler_list = handler;
3975 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3976 handler->sequential_number < max_sequential_handler_number)
3979 return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
3980 g_closure_invoke (handler->closure,
3983 instance_and_params,
3985 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3986 emission.state == EMISSION_RUN)
3987 emission.state = EMISSION_STOP;
3989 return_value_altered = TRUE;
3991 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3994 tmp = handler->next;
3998 handler_unref_R (signal_id, instance, handler_list);
3999 handler_list = handler;
4004 if (emission.state == EMISSION_STOP)
4006 else if (emission.state == EMISSION_RESTART)
4010 emission.ihint.run_type &= ~G_SIGNAL_RUN_FIRST;
4011 emission.ihint.run_type |= G_SIGNAL_RUN_LAST;
4013 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
4015 emission.state = EMISSION_RUN;
4017 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
4019 return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
4020 g_closure_invoke (class_closure,
4023 instance_and_params,
4025 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
4026 emission.state == EMISSION_RUN)
4027 emission.state = EMISSION_STOP;
4029 emission.chain_type = G_TYPE_NONE;
4030 return_value_altered = TRUE;
4032 if (emission.state == EMISSION_STOP)
4034 else if (emission.state == EMISSION_RESTART)
4040 Handler *handler = handler_list;
4042 emission.state = EMISSION_RUN;
4043 handler_ref (handler);
4048 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
4049 handler->sequential_number < max_sequential_handler_number)
4052 return_accu = maybe_init_accumulator_unlocked (node, emission_return, &accu);
4053 g_closure_invoke (handler->closure,
4056 instance_and_params,
4058 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
4059 emission.state == EMISSION_RUN)
4060 emission.state = EMISSION_STOP;
4062 return_value_altered = TRUE;
4064 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
4067 tmp = handler->next;
4071 handler_unref_R (signal_id, instance, handler);
4076 if (emission.state == EMISSION_STOP)
4078 else if (emission.state == EMISSION_RESTART)
4084 emission.ihint.run_type &= ~G_SIGNAL_RUN_LAST;
4085 emission.ihint.run_type |= G_SIGNAL_RUN_CLEANUP;
4087 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
4089 gboolean need_unset = FALSE;
4091 emission.state = EMISSION_STOP;
4093 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
4095 if (node->return_type != G_TYPE_NONE && !accumulator)
4097 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
4100 g_closure_invoke (class_closure,
4101 node->return_type != G_TYPE_NONE ? &accu : NULL,
4103 instance_and_params,
4105 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
4106 emission.state == EMISSION_RUN)
4107 emission.state = EMISSION_STOP;
4109 g_value_unset (&accu);
4111 return_value_altered = TRUE;
4113 emission.chain_type = G_TYPE_NONE;
4115 if (emission.state == EMISSION_RESTART)
4120 handler_unref_R (signal_id, instance, handler_list);
4122 emission_pop (&emission);
4124 g_value_unset (&accu);
4126 TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
4128 return return_value_altered;
4132 add_invalid_closure_notify (Handler *handler,
4135 g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
4136 handler->has_invalid_closure_notify = 1;
4140 remove_invalid_closure_notify (Handler *handler,
4143 if (handler->has_invalid_closure_notify)
4145 g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
4146 handler->has_invalid_closure_notify = 0;
4151 invalid_closure_notify (gpointer instance,
4159 handler = handler_lookup (instance, 0, closure, &signal_id);
4160 /* See https://bugzilla.gnome.org/show_bug.cgi?id=730296 for discussion about this... */
4161 g_assert (handler != NULL);
4162 g_assert (handler->closure == closure);
4164 g_hash_table_remove (g_handlers, handler);
4165 handler->sequential_number = 0;
4166 handler->block_count = 1;
4167 handler_unref_R (signal_id, instance, handler);
4173 type_debug_name (GType type)
4177 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
4178 return name ? name : "<unknown>";
4185 * g_signal_accumulator_true_handled:
4186 * @ihint: standard #GSignalAccumulator parameter
4187 * @return_accu: standard #GSignalAccumulator parameter
4188 * @handler_return: standard #GSignalAccumulator parameter
4189 * @dummy: standard #GSignalAccumulator parameter
4191 * A predefined #GSignalAccumulator for signals that return a
4192 * boolean values. The behavior that this accumulator gives is
4193 * that a return of %TRUE stops the signal emission: no further
4194 * callbacks will be invoked, while a return of %FALSE allows
4195 * the emission to continue. The idea here is that a %TRUE return
4196 * indicates that the callback handled the signal, and no further
4197 * handling is needed.
4201 * Returns: standard #GSignalAccumulator result
4204 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
4205 GValue *return_accu,
4206 const GValue *handler_return,
4209 gboolean continue_emission;
4210 gboolean signal_handled;
4212 signal_handled = g_value_get_boolean (handler_return);
4213 g_value_set_boolean (return_accu, signal_handled);
4214 continue_emission = !signal_handled;
4216 return continue_emission;
4220 * g_signal_accumulator_first_wins:
4221 * @ihint: standard #GSignalAccumulator parameter
4222 * @return_accu: standard #GSignalAccumulator parameter
4223 * @handler_return: standard #GSignalAccumulator parameter
4224 * @dummy: standard #GSignalAccumulator parameter
4226 * A predefined #GSignalAccumulator for signals intended to be used as a
4227 * hook for application code to provide a particular value. Usually
4228 * only one such value is desired and multiple handlers for the same
4229 * signal don't make much sense (except for the case of the default
4230 * handler defined in the class structure, in which case you will
4231 * usually want the signal connection to override the class handler).
4233 * This accumulator will use the return value from the first signal
4234 * handler that is run as the return value for the signal and not run
4235 * any further handlers (ie: the first handler "wins").
4237 * Returns: standard #GSignalAccumulator result
4242 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
4243 GValue *return_accu,
4244 const GValue *handler_return,
4247 g_value_copy (handler_return, return_accu);
4252 * g_clear_signal_handler:
4253 * @handler_id_ptr: A pointer to a handler ID (of type #gulong) of the handler to be disconnected.
4254 * @instance: (type GObject.Object): The instance to remove the signal handler from.
4255 * This pointer may be %NULL or invalid, if the handler ID is zero.
4257 * Disconnects a handler from @instance so it will not be called during
4258 * any future or currently ongoing emissions of the signal it has been
4259 * connected to. The @handler_id_ptr is then set to zero, which is never a valid handler ID value (see g_signal_connect()).
4261 * If the handler ID is 0 then this function does nothing.
4263 * There is also a macro version of this function so that the code
4269 (g_clear_signal_handler) (gulong *handler_id_ptr,
4272 g_return_if_fail (handler_id_ptr != NULL);
4274 #ifndef g_clear_signal_handler
4275 #error g_clear_signal_handler() macro is not defined
4278 g_clear_signal_handler (handler_id_ptr, instance);