1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * this code is based on the original GtkSignal implementation
18 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
31 #include "gtype-private.h"
32 #include "gbsearcharray.h"
33 #include "gvaluecollector.h"
34 #include "gvaluetypes.h"
37 #include "gobject_trace.h"
42 * @short_description: A means for customization of object behaviour
43 * and a general purpose notification mechanism
46 * The basic concept of the signal system is that of the emission
47 * of a signal. Signals are introduced per-type and are identified
48 * through strings. Signals introduced for a parent type are available
49 * in derived types as well, so basically they are a per-type facility
52 * A signal emission mainly involves invocation of a certain set of
53 * callbacks in precisely defined manner. There are two main categories
54 * of such callbacks, per-object ones and user provided ones.
55 * (Although signals can deal with any kind of instantiatable type, I'm
56 * referring to those types as "object types" in the following, simply
57 * because that is the context most users will encounter signals in.)
58 * The per-object callbacks are most often referred to as "object method
59 * handler" or "default (signal) handler", while user provided callbacks are
60 * usually just called "signal handler".
62 * The object method handler is provided at signal creation time (this most
63 * frequently happens at the end of an object class' creation), while user
64 * provided handlers are frequently connected and disconnected to/from a
65 * certain signal on certain object instances.
67 * A signal emission consists of five stages, unless prematurely stopped:
69 * 1. Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
71 * 2. Invocation of normal user-provided signal handlers (where the @after
74 * 3. Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
76 * 4. Invocation of user provided signal handlers (where the @after flag is set)
78 * 5. Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
80 * The user-provided signal handlers are called in the order they were
83 * All handlers may prematurely stop a signal emission, and any number of
84 * handlers may be connected, disconnected, blocked or unblocked during
87 * There are certain criteria for skipping user handlers in stages 2 and 4
88 * of a signal emission.
90 * First, user handlers may be blocked. Blocked handlers are omitted during
91 * callback invocation, to return from the blocked state, a handler has to
92 * get unblocked exactly the same amount of times it has been blocked before.
94 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
95 * @detail argument passed in to g_signal_emit() has to match the detail
96 * argument of the signal handler currently subject to invocation.
97 * Specification of no detail argument for signal handlers (omission of the
98 * detail part of the signal specification upon connection) serves as a
99 * wildcard and matches any detail argument passed in to emission.
103 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
105 /* --- typedefs --- */
106 typedef struct _SignalNode SignalNode;
107 typedef struct _SignalKey SignalKey;
108 typedef struct _Emission Emission;
109 typedef struct _Handler Handler;
110 typedef struct _HandlerList HandlerList;
111 typedef struct _HandlerMatch HandlerMatch;
121 /* --- prototypes --- */
122 static inline guint signal_id_lookup (GQuark quark,
124 static void signal_destroy_R (SignalNode *signal_node);
125 static inline HandlerList* handler_list_ensure (guint signal_id,
127 static inline HandlerList* handler_list_lookup (guint signal_id,
129 static inline Handler* handler_new (gboolean after);
130 static void handler_insert (guint signal_id,
133 static Handler* handler_lookup (gpointer instance,
137 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
140 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
142 static HandlerMatch* handlers_find (gpointer instance,
143 GSignalMatchType mask,
149 gboolean one_and_only);
150 static inline void handler_ref (Handler *handler);
151 static inline void handler_unref_R (guint signal_id,
154 static gint handler_lists_cmp (gconstpointer node1,
155 gconstpointer node2);
156 static inline void emission_push (Emission **emission_list_p,
158 static inline void emission_pop (Emission **emission_list_p,
160 static inline Emission* emission_find (Emission *emission_list,
164 static gint class_closures_cmp (gconstpointer node1,
165 gconstpointer node2);
166 static gint signal_key_cmp (gconstpointer node1,
167 gconstpointer node2);
168 static gboolean signal_emit_unlocked_R (SignalNode *node,
171 GValue *return_value,
172 const GValue *instance_and_params);
173 static void add_invalid_closure_notify (Handler *handler,
175 static void remove_invalid_closure_notify (Handler *handler,
177 static void invalid_closure_notify (gpointer data,
179 static const gchar * type_debug_name (GType type);
180 static void node_check_deprecated (const SignalNode *node);
181 static void node_update_single_va_closure (SignalNode *node);
184 /* --- structures --- */
187 GSignalAccumulator func;
195 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
199 /* permanent portion */
205 /* reinitializable portion */
208 guint single_va_closure_is_valid : 1;
209 guint single_va_closure_is_after : 1;
210 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
211 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
212 GBSearchArray *class_closure_bsa;
213 SignalAccumulator *accumulator;
214 GSignalCMarshaller c_marshaller;
215 GSignalCVaMarshaller va_marshaller;
216 GHookList *emission_hooks;
218 GClosure *single_va_closure;
221 #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1) /* indicates single_va_closure is valid but empty */
234 GSignalInvocationHint ihint;
243 Handler *tail_before; /* normal signal handlers are appended here */
244 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
249 gulong sequential_number;
254 guint block_count : 16;
255 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
257 guint has_invalid_closure_notify : 1;
269 GType instance_type; /* 0 for default closure */
274 /* --- variables --- */
275 static GBSearchArray *g_signal_key_bsa = NULL;
276 static const GBSearchConfig g_signal_key_bconfig = {
279 G_BSEARCH_ARRAY_ALIGN_POWER2,
281 static GBSearchConfig g_signal_hlbsa_bconfig = {
282 sizeof (HandlerList),
286 static GBSearchConfig g_class_closure_bconfig = {
287 sizeof (ClassClosure),
291 static GHashTable *g_handler_list_bsa_ht = NULL;
292 static Emission *g_recursive_emissions = NULL;
293 static Emission *g_restart_emissions = NULL;
294 static gulong g_handler_sequential_number = 1;
295 G_LOCK_DEFINE_STATIC (g_signal_mutex);
296 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
297 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
300 /* --- signal nodes --- */
301 static guint g_n_signal_nodes = 0;
302 static SignalNode **g_signal_nodes = NULL;
304 static inline SignalNode*
305 LOOKUP_SIGNAL_NODE (guint signal_id)
307 if (signal_id < g_n_signal_nodes)
308 return g_signal_nodes[signal_id];
314 /* --- functions --- */
316 signal_id_lookup (GQuark quark,
319 GType *ifaces, type = itype;
325 /* try looking up signals for this type and its ancestors */
328 SignalKey *signal_key;
331 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
334 return signal_key->signal_id;
336 type = g_type_parent (type);
340 /* no luck, try interfaces it exports */
341 ifaces = g_type_interfaces (itype, &n_ifaces);
344 SignalKey *signal_key;
346 key.itype = ifaces[n_ifaces];
347 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
352 return signal_key->signal_id;
361 class_closures_cmp (gconstpointer node1,
364 const ClassClosure *c1 = node1, *c2 = node2;
366 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
370 handler_lists_cmp (gconstpointer node1,
373 const HandlerList *hlist1 = node1, *hlist2 = node2;
375 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
378 static inline HandlerList*
379 handler_list_ensure (guint signal_id,
382 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
385 key.signal_id = signal_id;
387 key.tail_before = NULL;
388 key.tail_after = NULL;
391 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
392 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
393 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
397 GBSearchArray *o = hlbsa;
399 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
401 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
403 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
406 static inline HandlerList*
407 handler_list_lookup (guint signal_id,
410 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
413 key.signal_id = signal_id;
415 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
419 handler_lookup (gpointer instance,
424 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
430 for (i = 0; i < hlbsa->n_nodes; i++)
432 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
435 for (handler = hlist->handlers; handler; handler = handler->next)
436 if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id))
439 *signal_id_p = hlist->signal_id;
449 static inline HandlerMatch*
450 handler_match_prepend (HandlerMatch *list,
456 node = g_slice_new (HandlerMatch);
457 node->handler = handler;
459 node->signal_id = signal_id;
460 handler_ref (handler);
464 static inline HandlerMatch*
465 handler_match_free1_R (HandlerMatch *node,
468 HandlerMatch *next = node->next;
470 handler_unref_R (node->signal_id, instance, node->handler);
471 g_slice_free (HandlerMatch, node);
477 handlers_find (gpointer instance,
478 GSignalMatchType mask,
484 gboolean one_and_only)
486 HandlerMatch *mlist = NULL;
488 if (mask & G_SIGNAL_MATCH_ID)
490 HandlerList *hlist = handler_list_lookup (signal_id, instance);
492 SignalNode *node = NULL;
494 if (mask & G_SIGNAL_MATCH_FUNC)
496 node = LOOKUP_SIGNAL_NODE (signal_id);
497 if (!node || !node->c_marshaller)
502 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
503 if (handler->sequential_number &&
504 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
505 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
506 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
507 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
508 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
509 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
510 ((GCClosure*) handler->closure)->callback == func)))
512 mlist = handler_match_prepend (mlist, handler, signal_id);
519 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
526 for (i = 0; i < hlbsa->n_nodes; i++)
528 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
529 SignalNode *node = NULL;
532 if (!(mask & G_SIGNAL_MATCH_FUNC))
534 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
535 if (!node->c_marshaller)
539 for (handler = hlist->handlers; handler; handler = handler->next)
540 if (handler->sequential_number &&
541 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
542 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
543 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
544 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
545 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
546 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
547 ((GCClosure*) handler->closure)->callback == func)))
549 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
560 static inline Handler*
561 handler_new (gboolean after)
563 Handler *handler = g_slice_new (Handler);
564 #ifndef G_DISABLE_CHECKS
565 if (g_handler_sequential_number < 1)
566 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
569 handler->sequential_number = g_handler_sequential_number++;
570 handler->prev = NULL;
571 handler->next = NULL;
573 handler->ref_count = 1;
574 handler->block_count = 0;
575 handler->after = after != FALSE;
576 handler->closure = NULL;
577 handler->has_invalid_closure_notify = 0;
583 handler_ref (Handler *handler)
585 g_return_if_fail (handler->ref_count > 0);
587 handler->ref_count++;
591 handler_unref_R (guint signal_id,
595 g_return_if_fail (handler->ref_count > 0);
597 handler->ref_count--;
599 if (G_UNLIKELY (handler->ref_count == 0))
601 HandlerList *hlist = NULL;
604 handler->next->prev = handler->prev;
605 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
606 handler->prev->next = handler->next;
609 hlist = handler_list_lookup (signal_id, instance);
610 hlist->handlers = handler->next;
615 /* check if we are removing the handler pointed to by tail_before */
616 if (!handler->after && (!handler->next || handler->next->after))
619 hlist = handler_list_lookup (signal_id, instance);
622 g_assert (hlist->tail_before == handler); /* paranoid */
623 hlist->tail_before = handler->prev;
627 /* check if we are removing the handler pointed to by tail_after */
631 hlist = handler_list_lookup (signal_id, instance);
634 g_assert (hlist->tail_after == handler); /* paranoid */
635 hlist->tail_after = handler->prev;
641 g_closure_unref (handler->closure);
643 g_slice_free (Handler, handler);
648 handler_insert (guint signal_id,
654 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
656 hlist = handler_list_ensure (signal_id, instance);
657 if (!hlist->handlers)
659 hlist->handlers = handler;
661 hlist->tail_before = handler;
663 else if (handler->after)
665 handler->prev = hlist->tail_after;
666 hlist->tail_after->next = handler;
670 if (hlist->tail_before)
672 handler->next = hlist->tail_before->next;
674 handler->next->prev = handler;
675 handler->prev = hlist->tail_before;
676 hlist->tail_before->next = handler;
678 else /* insert !after handler into a list of only after handlers */
680 handler->next = hlist->handlers;
682 handler->next->prev = handler;
683 hlist->handlers = handler;
685 hlist->tail_before = handler;
689 hlist->tail_after = handler;
693 node_update_single_va_closure (SignalNode *node)
695 GClosure *closure = NULL;
696 gboolean is_after = FALSE;
698 /* Fast path single-handler without boxing the arguments in GValues */
699 if (G_TYPE_IS_OBJECT (node->itype) &&
700 (node->flags & (G_SIGNAL_MUST_COLLECT)) == 0 &&
701 (node->emission_hooks == NULL || node->emission_hooks->hooks == NULL))
703 GSignalFlags run_type;
705 GBSearchArray *bsa = node->class_closure_bsa;
707 if (bsa == NULL || bsa->n_nodes == 0)
708 closure = SINGLE_VA_CLOSURE_EMPTY_MAGIC;
709 else if (bsa->n_nodes == 1)
711 /* Look for default class closure (can't support non-default as it
712 chains up using GValues */
713 cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
714 if (cc->instance_type == 0)
716 run_type = node->flags & (G_SIGNAL_RUN_FIRST|G_SIGNAL_RUN_LAST|G_SIGNAL_RUN_CLEANUP);
717 /* Only support *one* of run-first or run-last, not multiple or cleanup */
718 if (run_type == G_SIGNAL_RUN_FIRST ||
719 run_type == G_SIGNAL_RUN_LAST)
721 closure = cc->closure;
722 is_after = (run_type == G_SIGNAL_RUN_LAST);
728 node->single_va_closure_is_valid = TRUE;
729 node->single_va_closure = closure;
730 node->single_va_closure_is_after = is_after;
734 emission_push (Emission **emission_list_p,
737 emission->next = *emission_list_p;
738 *emission_list_p = emission;
742 emission_pop (Emission **emission_list_p,
745 Emission *node, *last = NULL;
747 for (node = *emission_list_p; node; last = node, node = last->next)
748 if (node == emission)
751 last->next = node->next;
753 *emission_list_p = node->next;
756 g_assert_not_reached ();
759 static inline Emission*
760 emission_find (Emission *emission_list,
767 for (emission = emission_list; emission; emission = emission->next)
768 if (emission->instance == instance &&
769 emission->ihint.signal_id == signal_id &&
770 emission->ihint.detail == detail)
775 static inline Emission*
776 emission_find_innermost (gpointer instance)
778 Emission *emission, *s = NULL, *c = NULL;
780 for (emission = g_restart_emissions; emission; emission = emission->next)
781 if (emission->instance == instance)
786 for (emission = g_recursive_emissions; emission; emission = emission->next)
787 if (emission->instance == instance)
797 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
801 signal_key_cmp (gconstpointer node1,
804 const SignalKey *key1 = node1, *key2 = node2;
806 if (key1->itype == key2->itype)
807 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
809 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
813 _g_signal_init (void)
816 if (!g_n_signal_nodes)
818 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
819 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
820 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
822 /* invalid (0) signal_id */
823 g_n_signal_nodes = 1;
824 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
825 g_signal_nodes[0] = NULL;
831 _g_signals_destroy (GType itype)
836 for (i = 1; i < g_n_signal_nodes; i++)
838 SignalNode *node = g_signal_nodes[i];
840 if (node->itype == itype)
843 g_warning (G_STRLOC ": signal \"%s\" of type '%s' already destroyed",
845 type_debug_name (node->itype));
847 signal_destroy_R (node);
854 * g_signal_stop_emission:
855 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
856 * @signal_id: the signal identifier, as returned by g_signal_lookup().
857 * @detail: the detail which the signal was emitted with.
859 * Stops a signal's current emission.
861 * This will prevent the default method from running, if the signal was
862 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
865 * Prints a warning if used on a signal which isn't being emitted.
868 g_signal_stop_emission (gpointer instance,
874 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
875 g_return_if_fail (signal_id > 0);
878 node = LOOKUP_SIGNAL_NODE (signal_id);
879 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
881 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
885 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
887 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
888 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
892 if (emission->state == EMISSION_HOOK)
893 g_warning (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
894 node->name, instance);
895 else if (emission->state == EMISSION_RUN)
896 emission->state = EMISSION_STOP;
899 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
900 node->name, instance);
903 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
908 signal_finalize_hook (GHookList *hook_list,
911 GDestroyNotify destroy = hook->destroy;
915 hook->destroy = NULL;
917 destroy (hook->data);
923 * g_signal_add_emission_hook:
924 * @signal_id: the signal identifier, as returned by g_signal_lookup().
925 * @detail: the detail on which to call the hook.
926 * @hook_func: a #GSignalEmissionHook function.
927 * @hook_data: user data for @hook_func.
928 * @data_destroy: a #GDestroyNotify for @hook_data.
930 * Adds an emission hook for a signal, which will get called for any emission
931 * of that signal, independent of the instance. This is possible only
932 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
934 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
937 g_signal_add_emission_hook (guint signal_id,
939 GSignalEmissionHook hook_func,
941 GDestroyNotify data_destroy)
943 static gulong seq_hook_id = 1;
946 SignalHook *signal_hook;
948 g_return_val_if_fail (signal_id > 0, 0);
949 g_return_val_if_fail (hook_func != NULL, 0);
952 node = LOOKUP_SIGNAL_NODE (signal_id);
953 if (!node || node->destroyed)
955 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
959 if (node->flags & G_SIGNAL_NO_HOOKS)
961 g_warning ("%s: signal id '%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
965 if (detail && !(node->flags & G_SIGNAL_DETAILED))
967 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
971 node->single_va_closure_is_valid = FALSE;
972 if (!node->emission_hooks)
974 node->emission_hooks = g_new (GHookList, 1);
975 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
976 node->emission_hooks->finalize_hook = signal_finalize_hook;
979 node_check_deprecated (node);
981 hook = g_hook_alloc (node->emission_hooks);
982 hook->data = hook_data;
983 hook->func = (gpointer) hook_func;
984 hook->destroy = data_destroy;
985 signal_hook = SIGNAL_HOOK (hook);
986 signal_hook->detail = detail;
987 node->emission_hooks->seq_id = seq_hook_id;
988 g_hook_append (node->emission_hooks, hook);
989 seq_hook_id = node->emission_hooks->seq_id;
993 return hook->hook_id;
997 * g_signal_remove_emission_hook:
998 * @signal_id: the id of the signal
999 * @hook_id: the id of the emission hook, as returned by
1000 * g_signal_add_emission_hook()
1002 * Deletes an emission hook.
1005 g_signal_remove_emission_hook (guint signal_id,
1010 g_return_if_fail (signal_id > 0);
1011 g_return_if_fail (hook_id > 0);
1014 node = LOOKUP_SIGNAL_NODE (signal_id);
1015 if (!node || node->destroyed)
1017 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1020 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
1021 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
1023 node->single_va_closure_is_valid = FALSE;
1030 signal_parse_name (const gchar *name,
1033 gboolean force_quark)
1035 const gchar *colon = strchr (name, ':');
1040 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1041 if (signal_id && detail_p)
1044 else if (colon[1] == ':')
1047 guint l = colon - name;
1051 memcpy (buffer, name, l);
1053 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1057 gchar *signal = g_new (gchar, l + 1);
1059 memcpy (signal, name, l);
1061 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1065 if (signal_id && detail_p)
1066 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1074 * g_signal_parse_name:
1075 * @detailed_signal: a string of the form "signal-name::detail".
1076 * @itype: The interface/instance type that introduced "signal-name".
1077 * @signal_id_p: (out): Location to store the signal id.
1078 * @detail_p: (out): Location to store the detail quark.
1079 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1081 * Internal function to parse a signal name into its @signal_id
1082 * and @detail quark.
1084 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1087 g_signal_parse_name (const gchar *detailed_signal,
1091 gboolean force_detail_quark)
1097 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1098 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1101 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1104 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1105 if (!node || node->destroyed ||
1106 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1110 *signal_id_p = signal_id;
1118 * g_signal_stop_emission_by_name:
1119 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
1120 * @detailed_signal: a string of the form "signal-name::detail".
1122 * Stops a signal's current emission.
1124 * This is just like g_signal_stop_emission() except it will look up the
1125 * signal id for you.
1128 g_signal_stop_emission_by_name (gpointer instance,
1129 const gchar *detailed_signal)
1135 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1136 g_return_if_fail (detailed_signal != NULL);
1139 itype = G_TYPE_FROM_INSTANCE (instance);
1140 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1143 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1145 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1146 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
1147 else if (!g_type_is_a (itype, node->itype))
1148 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1149 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1152 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1153 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1157 if (emission->state == EMISSION_HOOK)
1158 g_warning (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1159 node->name, instance);
1160 else if (emission->state == EMISSION_RUN)
1161 emission->state = EMISSION_STOP;
1164 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1165 node->name, instance);
1169 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1170 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1176 * @name: the signal's name.
1177 * @itype: the type that the signal operates on.
1179 * Given the name of the signal and the type of object it connects to, gets
1180 * the signal's identifying integer. Emitting the signal by number is
1181 * somewhat faster than using the name each time.
1183 * Also tries the ancestors of the given type.
1185 * See g_signal_new() for details on allowed signal names.
1187 * Returns: the signal's identifying number, or 0 if no signal was found.
1190 g_signal_lookup (const gchar *name,
1194 g_return_val_if_fail (name != NULL, 0);
1195 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1198 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1202 /* give elaborate warnings */
1203 if (!g_type_name (itype))
1204 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id '%"G_GSIZE_FORMAT"'",
1206 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1207 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type '%s'",
1208 name, g_type_name (itype));
1209 else if (!g_type_class_peek (itype))
1210 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type '%s'",
1211 name, g_type_name (itype));
1218 * g_signal_list_ids:
1219 * @itype: Instance or interface type.
1220 * @n_ids: Location to store the number of signal ids for @itype.
1222 * Lists the signals by id that a certain instance or interface type
1223 * created. Further information about the signals can be acquired through
1226 * Returns: (array length=n_ids): Newly allocated array of signal IDs.
1229 g_signal_list_ids (GType itype,
1237 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1238 g_return_val_if_fail (n_ids != NULL, NULL);
1241 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1242 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1243 result = g_array_new (FALSE, FALSE, sizeof (guint));
1245 for (i = 0; i < n_nodes; i++)
1246 if (keys[i].itype == itype)
1248 const gchar *name = g_quark_to_string (keys[i].quark);
1250 /* Signal names with "_" in them are aliases to the same
1251 * name with "-" instead of "_".
1253 if (!strchr (name, '_'))
1254 g_array_append_val (result, keys[i].signal_id);
1256 *n_ids = result->len;
1260 /* give elaborate warnings */
1261 if (!g_type_name (itype))
1262 g_warning (G_STRLOC ": unable to list signals for invalid type id '%"G_GSIZE_FORMAT"'",
1264 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1265 g_warning (G_STRLOC ": unable to list signals of non instantiatable type '%s'",
1266 g_type_name (itype));
1267 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1268 g_warning (G_STRLOC ": unable to list signals of unloaded type '%s'",
1269 g_type_name (itype));
1272 return (guint*) g_array_free (result, FALSE);
1277 * @signal_id: the signal's identifying number.
1279 * Given the signal's identifier, finds its name.
1281 * Two different signals may have the same name, if they have differing types.
1283 * Returns: the signal name, or %NULL if the signal number was invalid.
1286 g_signal_name (guint signal_id)
1292 node = LOOKUP_SIGNAL_NODE (signal_id);
1293 name = node ? node->name : NULL;
1296 return (char*) name;
1301 * @signal_id: The signal id of the signal to query information for.
1302 * @query: (out caller-allocates): A user provided structure that is
1303 * filled in with constant values upon success.
1305 * Queries the signal system for in-depth information about a
1306 * specific signal. This function will fill in a user-provided
1307 * structure to hold signal-specific information. If an invalid
1308 * signal id is passed in, the @signal_id member of the #GSignalQuery
1309 * is 0. All members filled into the #GSignalQuery structure should
1310 * be considered constant and have to be left untouched.
1313 g_signal_query (guint signal_id,
1314 GSignalQuery *query)
1318 g_return_if_fail (query != NULL);
1321 node = LOOKUP_SIGNAL_NODE (signal_id);
1322 if (!node || node->destroyed)
1323 query->signal_id = 0;
1326 query->signal_id = node->signal_id;
1327 query->signal_name = node->name;
1328 query->itype = node->itype;
1329 query->signal_flags = node->flags;
1330 query->return_type = node->return_type;
1331 query->n_params = node->n_params;
1332 query->param_types = node->param_types;
1339 * @signal_name: the name for the signal
1340 * @itype: the type this signal pertains to. It will also pertain to
1341 * types which are derived from this type.
1342 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1343 * the default handler is to be invoked. You should at least specify
1344 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1345 * @class_offset: The offset of the function pointer in the class structure
1346 * for this type. Used to invoke a class method generically. Pass 0 to
1347 * not associate a class method slot with this signal.
1348 * @accumulator: the accumulator for this signal; may be %NULL.
1349 * @accu_data: user data for the @accumulator.
1350 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1351 * values to signal emissions into C language callback invocations or %NULL.
1352 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1353 * without a return value.
1354 * @n_params: the number of parameter types to follow.
1355 * @...: a list of types, one for each parameter.
1357 * Creates a new signal. (This is usually done in the class initializer.)
1359 * A signal name consists of segments consisting of ASCII letters and
1360 * digits, separated by either the '-' or '_' character. The first
1361 * character of a signal name must be a letter. Names which violate these
1362 * rules lead to undefined behaviour of the GSignal system.
1364 * When registering a signal and looking up a signal, either separator can
1365 * be used, but they cannot be mixed.
1367 * If 0 is used for @class_offset subclasses cannot override the class handler
1368 * in their class_init method by doing super_class->signal_handler = my_signal_handler.
1369 * Instead they will have to use g_signal_override_class_handler().
1371 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1372 * the marshaller for this signal.
1374 * Returns: the signal id
1377 g_signal_new (const gchar *signal_name,
1379 GSignalFlags signal_flags,
1381 GSignalAccumulator accumulator,
1383 GSignalCMarshaller c_marshaller,
1391 g_return_val_if_fail (signal_name != NULL, 0);
1393 va_start (args, n_params);
1395 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1396 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1397 accumulator, accu_data, c_marshaller,
1398 return_type, n_params, args);
1406 * g_signal_new_class_handler:
1407 * @signal_name: the name for the signal
1408 * @itype: the type this signal pertains to. It will also pertain to
1409 * types which are derived from this type.
1410 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1411 * the default handler is to be invoked. You should at least specify
1412 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1413 * @class_handler: a #GCallback which acts as class implementation of
1414 * this signal. Used to invoke a class method generically. Pass %NULL to
1415 * not associate a class method with this signal.
1416 * @accumulator: the accumulator for this signal; may be %NULL.
1417 * @accu_data: user data for the @accumulator.
1418 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1419 * values to signal emissions into C language callback invocations or %NULL.
1420 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1421 * without a return value.
1422 * @n_params: the number of parameter types to follow.
1423 * @...: a list of types, one for each parameter.
1425 * Creates a new signal. (This is usually done in the class initializer.)
1427 * This is a variant of g_signal_new() that takes a C callback instead
1428 * off a class offset for the signal's class handler. This function
1429 * doesn't need a function pointer exposed in the class structure of
1430 * an object definition, instead the function pointer is passed
1431 * directly and can be overriden by derived classes with
1432 * g_signal_override_class_closure() or
1433 * g_signal_override_class_handler()and chained to with
1434 * g_signal_chain_from_overridden() or
1435 * g_signal_chain_from_overridden_handler().
1437 * See g_signal_new() for information about signal names.
1439 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1440 * the marshaller for this signal.
1442 * Returns: the signal id
1447 g_signal_new_class_handler (const gchar *signal_name,
1449 GSignalFlags signal_flags,
1450 GCallback class_handler,
1451 GSignalAccumulator accumulator,
1453 GSignalCMarshaller c_marshaller,
1461 g_return_val_if_fail (signal_name != NULL, 0);
1463 va_start (args, n_params);
1465 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1466 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1467 accumulator, accu_data, c_marshaller,
1468 return_type, n_params, args);
1475 static inline ClassClosure*
1476 signal_find_class_closure (SignalNode *node,
1479 GBSearchArray *bsa = node->class_closure_bsa;
1486 /* cc->instance_type is 0 for default closure */
1488 key.instance_type = itype;
1489 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1490 while (!cc && key.instance_type)
1492 key.instance_type = g_type_parent (key.instance_type);
1493 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1501 static inline GClosure*
1502 signal_lookup_closure (SignalNode *node,
1503 GTypeInstance *instance)
1507 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1509 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1510 if (cc && cc->instance_type == 0) /* check for default closure */
1513 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1514 return cc ? cc->closure : NULL;
1518 signal_add_class_closure (SignalNode *node,
1524 node->single_va_closure_is_valid = FALSE;
1526 if (!node->class_closure_bsa)
1527 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1528 key.instance_type = itype;
1529 key.closure = g_closure_ref (closure);
1530 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1531 &g_class_closure_bconfig,
1533 g_closure_sink (closure);
1534 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1536 g_closure_set_marshal (closure, node->c_marshaller);
1537 if (node->va_marshaller)
1538 _g_closure_set_va_marshal (closure, node->va_marshaller);
1544 * @signal_name: the name for the signal
1545 * @itype: the type this signal pertains to. It will also pertain to
1546 * types which are derived from this type
1547 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1548 * the default handler is to be invoked. You should at least specify
1549 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1550 * @class_closure: (allow-none): The closure to invoke on signal emission;
1552 * @accumulator: (allow-none): the accumulator for this signal; may be %NULL
1553 * @accu_data: user data for the @accumulator
1554 * @c_marshaller: (allow-none): the function to translate arrays of
1555 * parameter values to signal emissions into C language callback
1556 * invocations or %NULL
1557 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1558 * without a return value
1559 * @n_params: the length of @param_types
1560 * @param_types: (array length=n_params): an array of types, one for
1563 * Creates a new signal. (This is usually done in the class initializer.)
1565 * See g_signal_new() for details on allowed signal names.
1567 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1568 * the marshaller for this signal.
1570 * Returns: the signal id
1573 g_signal_newv (const gchar *signal_name,
1575 GSignalFlags signal_flags,
1576 GClosure *class_closure,
1577 GSignalAccumulator accumulator,
1579 GSignalCMarshaller c_marshaller,
1587 GSignalCMarshaller builtin_c_marshaller;
1588 GSignalCVaMarshaller va_marshaller;
1590 g_return_val_if_fail (signal_name != NULL, 0);
1591 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1593 g_return_val_if_fail (param_types != NULL, 0);
1594 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1595 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1596 g_return_val_if_fail (accumulator == NULL, 0);
1598 g_return_val_if_fail (accu_data == NULL, 0);
1600 name = g_strdup (signal_name);
1601 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1605 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1606 node = LOOKUP_SIGNAL_NODE (signal_id);
1607 if (node && !node->destroyed)
1609 g_warning (G_STRLOC ": signal \"%s\" already exists in the '%s' %s",
1611 type_debug_name (node->itype),
1612 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1617 if (node && node->itype != itype)
1619 g_warning (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'",
1621 type_debug_name (itype),
1622 type_debug_name (node->itype));
1627 for (i = 0; i < n_params; i++)
1628 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1630 g_warning (G_STRLOC ": parameter %d of type '%s' for signal \"%s::%s\" is not a value type",
1631 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1636 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1638 g_warning (G_STRLOC ": return value of type '%s' for signal \"%s::%s\" is not a value type",
1639 type_debug_name (return_type), type_debug_name (itype), name);
1644 if (return_type != G_TYPE_NONE &&
1645 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1647 g_warning (G_STRLOC ": signal \"%s::%s\" has return type '%s' and is only G_SIGNAL_RUN_FIRST",
1648 type_debug_name (itype), name, type_debug_name (return_type));
1654 /* setup permanent portion of signal node */
1659 signal_id = g_n_signal_nodes++;
1660 node = g_new (SignalNode, 1);
1661 node->signal_id = signal_id;
1662 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1663 g_signal_nodes[signal_id] = node;
1664 node->itype = itype;
1667 key.quark = g_quark_from_string (node->name);
1668 key.signal_id = signal_id;
1669 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1670 g_strdelimit (name, "_", '-');
1671 node->name = g_intern_string (name);
1672 key.quark = g_quark_from_string (name);
1673 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1675 TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1677 node->destroyed = FALSE;
1679 /* setup reinitializable portion */
1680 node->single_va_closure_is_valid = FALSE;
1681 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1682 node->n_params = n_params;
1683 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1684 node->return_type = return_type;
1685 node->class_closure_bsa = NULL;
1688 node->accumulator = g_new (SignalAccumulator, 1);
1689 node->accumulator->func = accumulator;
1690 node->accumulator->data = accu_data;
1693 node->accumulator = NULL;
1695 builtin_c_marshaller = NULL;
1696 va_marshaller = NULL;
1698 /* Pick up built-in va marshallers for standard types, and
1699 instead of generic marshaller if no marshaller specified */
1700 if (n_params == 0 && return_type == G_TYPE_NONE)
1702 builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1703 va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1705 else if (n_params == 1 && return_type == G_TYPE_NONE)
1707 #define ADD_CHECK(__type__) \
1708 else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \
1710 builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \
1711 va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \
1734 if (c_marshaller == NULL)
1736 if (builtin_c_marshaller)
1737 c_marshaller = builtin_c_marshaller;
1740 c_marshaller = g_cclosure_marshal_generic;
1741 va_marshaller = g_cclosure_marshal_generic_va;
1745 node->c_marshaller = c_marshaller;
1746 node->va_marshaller = va_marshaller;
1747 node->emission_hooks = NULL;
1749 signal_add_class_closure (node, 0, class_closure);
1759 g_signal_set_va_marshaller (guint signal_id,
1760 GType instance_type,
1761 GSignalCVaMarshaller va_marshaller)
1765 g_return_if_fail (signal_id > 0);
1766 g_return_if_fail (va_marshaller != NULL);
1769 node = LOOKUP_SIGNAL_NODE (signal_id);
1772 node->va_marshaller = va_marshaller;
1773 if (node->class_closure_bsa)
1775 ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1776 if (cc->closure->marshal == node->c_marshaller)
1777 _g_closure_set_va_marshal (cc->closure, va_marshaller);
1780 node->single_va_closure_is_valid = FALSE;
1788 * g_signal_new_valist:
1789 * @signal_name: the name for the signal
1790 * @itype: the type this signal pertains to. It will also pertain to
1791 * types which are derived from this type.
1792 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1793 * the default handler is to be invoked. You should at least specify
1794 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1795 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1796 * @accumulator: the accumulator for this signal; may be %NULL.
1797 * @accu_data: user data for the @accumulator.
1798 * @c_marshaller: (allow-none): the function to translate arrays of parameter
1799 * values to signal emissions into C language callback invocations or %NULL.
1800 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1801 * without a return value.
1802 * @n_params: the number of parameter types in @args.
1803 * @args: va_list of #GType, one for each parameter.
1805 * Creates a new signal. (This is usually done in the class initializer.)
1807 * See g_signal_new() for details on allowed signal names.
1809 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1810 * the marshaller for this signal.
1812 * Returns: the signal id
1815 g_signal_new_valist (const gchar *signal_name,
1817 GSignalFlags signal_flags,
1818 GClosure *class_closure,
1819 GSignalAccumulator accumulator,
1821 GSignalCMarshaller c_marshaller,
1832 param_types = g_new (GType, n_params);
1834 for (i = 0; i < n_params; i++)
1835 param_types[i] = va_arg (args, GType);
1840 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1841 class_closure, accumulator, accu_data, c_marshaller,
1842 return_type, n_params, param_types);
1843 g_free (param_types);
1849 signal_destroy_R (SignalNode *signal_node)
1851 SignalNode node = *signal_node;
1853 signal_node->destroyed = TRUE;
1855 /* reentrancy caution, zero out real contents first */
1856 signal_node->single_va_closure_is_valid = FALSE;
1857 signal_node->n_params = 0;
1858 signal_node->param_types = NULL;
1859 signal_node->return_type = 0;
1860 signal_node->class_closure_bsa = NULL;
1861 signal_node->accumulator = NULL;
1862 signal_node->c_marshaller = NULL;
1863 signal_node->va_marshaller = NULL;
1864 signal_node->emission_hooks = NULL;
1866 #ifdef G_ENABLE_DEBUG
1867 /* check current emissions */
1871 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1872 emission; emission = emission->next)
1873 if (emission->ihint.signal_id == node.signal_id)
1874 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')",
1875 node.name, emission->instance);
1879 /* free contents that need to
1882 g_free (node.param_types);
1883 if (node.class_closure_bsa)
1887 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1889 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1891 g_closure_unref (cc->closure);
1893 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1895 g_free (node.accumulator);
1896 if (node.emission_hooks)
1898 g_hook_list_clear (node.emission_hooks);
1899 g_free (node.emission_hooks);
1905 * g_signal_override_class_closure:
1906 * @signal_id: the signal id
1907 * @instance_type: the instance type on which to override the class closure
1909 * @class_closure: the closure.
1911 * Overrides the class closure (i.e. the default handler) for the given signal
1912 * for emissions on instances of @instance_type. @instance_type must be derived
1913 * from the type to which the signal belongs.
1915 * See g_signal_chain_from_overridden() and
1916 * g_signal_chain_from_overridden_handler() for how to chain up to the
1917 * parent class closure from inside the overridden one.
1920 g_signal_override_class_closure (guint signal_id,
1921 GType instance_type,
1922 GClosure *class_closure)
1926 g_return_if_fail (signal_id > 0);
1927 g_return_if_fail (class_closure != NULL);
1930 node = LOOKUP_SIGNAL_NODE (signal_id);
1931 node_check_deprecated (node);
1932 if (!g_type_is_a (instance_type, node->itype))
1933 g_warning ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1936 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1938 if (cc && cc->instance_type == instance_type)
1939 g_warning ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1941 signal_add_class_closure (node, instance_type, class_closure);
1947 * g_signal_override_class_handler:
1948 * @signal_name: the name for the signal
1949 * @instance_type: the instance type on which to override the class handler
1951 * @class_handler: the handler.
1953 * Overrides the class closure (i.e. the default handler) for the
1954 * given signal for emissions on instances of @instance_type with
1955 * callback @class_handler. @instance_type must be derived from the
1956 * type to which the signal belongs.
1958 * See g_signal_chain_from_overridden() and
1959 * g_signal_chain_from_overridden_handler() for how to chain up to the
1960 * parent class closure from inside the overridden one.
1965 g_signal_override_class_handler (const gchar *signal_name,
1966 GType instance_type,
1967 GCallback class_handler)
1971 g_return_if_fail (signal_name != NULL);
1972 g_return_if_fail (instance_type != G_TYPE_NONE);
1973 g_return_if_fail (class_handler != NULL);
1975 signal_id = g_signal_lookup (signal_name, instance_type);
1978 g_signal_override_class_closure (signal_id, instance_type,
1979 g_cclosure_new (class_handler, NULL, NULL));
1981 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1982 G_STRLOC, signal_name, instance_type);
1987 * g_signal_chain_from_overridden:
1988 * @instance_and_params: (array) the argument list of the signal emission.
1989 * The first element in the array is a #GValue for the instance the signal
1990 * is being emitted on. The rest are any arguments to be passed to the signal.
1991 * @return_value: Location for the return value.
1993 * Calls the original class closure of a signal. This function should only
1994 * be called from an overridden class closure; see
1995 * g_signal_override_class_closure() and
1996 * g_signal_override_class_handler().
1999 g_signal_chain_from_overridden (const GValue *instance_and_params,
2000 GValue *return_value)
2002 GType chain_type = 0, restore_type = 0;
2003 Emission *emission = NULL;
2004 GClosure *closure = NULL;
2008 g_return_if_fail (instance_and_params != NULL);
2009 instance = g_value_peek_pointer (instance_and_params);
2010 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2013 emission = emission_find_innermost (instance);
2016 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2018 g_assert (node != NULL); /* paranoid */
2020 /* we should probably do the same parameter checks as g_signal_emit() here.
2022 if (emission->chain_type != G_TYPE_NONE)
2024 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2026 g_assert (cc != NULL); /* closure currently in call stack */
2028 n_params = node->n_params;
2029 restore_type = cc->instance_type;
2030 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2031 if (cc && cc->instance_type != restore_type)
2033 closure = cc->closure;
2034 chain_type = cc->instance_type;
2038 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2041 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2045 emission->chain_type = chain_type;
2047 g_closure_invoke (closure,
2050 instance_and_params,
2053 emission->chain_type = restore_type;
2059 * g_signal_chain_from_overridden_handler: (skip)
2060 * @instance: the instance the signal is being emitted on.
2061 * @...: parameters to be passed to the parent class closure, followed by a
2062 * location for the return value. If the return type of the signal
2063 * is #G_TYPE_NONE, the return value location can be omitted.
2065 * Calls the original class closure of a signal. This function should
2066 * only be called from an overridden class closure; see
2067 * g_signal_override_class_closure() and
2068 * g_signal_override_class_handler().
2073 g_signal_chain_from_overridden_handler (gpointer instance,
2076 GType chain_type = 0, restore_type = 0;
2077 Emission *emission = NULL;
2078 GClosure *closure = NULL;
2082 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2085 emission = emission_find_innermost (instance);
2088 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2090 g_assert (node != NULL); /* paranoid */
2092 /* we should probably do the same parameter checks as g_signal_emit() here.
2094 if (emission->chain_type != G_TYPE_NONE)
2096 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2098 g_assert (cc != NULL); /* closure currently in call stack */
2100 n_params = node->n_params;
2101 restore_type = cc->instance_type;
2102 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2103 if (cc && cc->instance_type != restore_type)
2105 closure = cc->closure;
2106 chain_type = cc->instance_type;
2110 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2113 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2117 GValue *instance_and_params;
2118 GType signal_return_type;
2119 GValue *param_values;
2123 va_start (var_args, instance);
2125 signal_return_type = node->return_type;
2126 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
2127 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
2128 param_values = instance_and_params + 1;
2130 for (i = 0; i < node->n_params; i++)
2133 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2134 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2137 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2139 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2143 g_warning ("%s: %s", G_STRLOC, error);
2146 /* we purposely leak the value here, it might not be
2147 * in a sane state if an error condition occoured
2150 g_value_unset (param_values + i);
2159 instance_and_params->g_type = 0;
2160 g_value_init_from_instance (instance_and_params, instance);
2163 emission->chain_type = chain_type;
2166 if (signal_return_type == G_TYPE_NONE)
2168 g_closure_invoke (closure,
2171 instance_and_params,
2176 GValue return_value = G_VALUE_INIT;
2177 gchar *error = NULL;
2178 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2179 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2181 g_value_init (&return_value, rtype);
2183 g_closure_invoke (closure,
2186 instance_and_params,
2189 G_VALUE_LCOPY (&return_value,
2191 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2195 g_value_unset (&return_value);
2199 g_warning ("%s: %s", G_STRLOC, error);
2202 /* we purposely leak the value here, it might not be
2203 * in a sane state if an error condition occurred
2208 for (i = 0; i < n_params; i++)
2209 g_value_unset (param_values + i);
2210 g_value_unset (instance_and_params);
2215 emission->chain_type = restore_type;
2221 * g_signal_get_invocation_hint:
2222 * @instance: (type GObject.Object): the instance to query
2224 * Returns the invocation hint of the innermost signal emission of instance.
2226 * Returns: (transfer none): the invocation hint of the innermost signal emission.
2228 GSignalInvocationHint*
2229 g_signal_get_invocation_hint (gpointer instance)
2231 Emission *emission = NULL;
2233 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2236 emission = emission_find_innermost (instance);
2239 return emission ? &emission->ihint : NULL;
2243 * g_signal_connect_closure_by_id:
2244 * @instance: (type GObject.Object): the instance to connect to.
2245 * @signal_id: the id of the signal.
2246 * @detail: the detail.
2247 * @closure: the closure to connect.
2248 * @after: whether the handler should be called before or after the
2249 * default handler of the signal.
2251 * Connects a closure to a signal for a particular object.
2253 * Returns: the handler id (always greater than 0 for successful connections)
2256 g_signal_connect_closure_by_id (gpointer instance,
2263 gulong handler_seq_no = 0;
2265 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2266 g_return_val_if_fail (signal_id > 0, 0);
2267 g_return_val_if_fail (closure != NULL, 0);
2270 node = LOOKUP_SIGNAL_NODE (signal_id);
2273 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2274 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2275 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2276 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2279 Handler *handler = handler_new (after);
2281 handler_seq_no = handler->sequential_number;
2282 handler->detail = detail;
2283 handler->closure = g_closure_ref (closure);
2284 g_closure_sink (closure);
2285 add_invalid_closure_notify (handler, instance);
2286 handler_insert (signal_id, instance, handler);
2287 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2289 g_closure_set_marshal (closure, node->c_marshaller);
2290 if (node->va_marshaller)
2291 _g_closure_set_va_marshal (closure, node->va_marshaller);
2296 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2299 return handler_seq_no;
2303 * g_signal_connect_closure:
2304 * @instance: (type GObject.Object): the instance to connect to.
2305 * @detailed_signal: a string of the form "signal-name::detail".
2306 * @closure: the closure to connect.
2307 * @after: whether the handler should be called before or after the
2308 * default handler of the signal.
2310 * Connects a closure to a signal for a particular object.
2312 * Returns: the handler id (always greater than 0 for successful connections)
2315 g_signal_connect_closure (gpointer instance,
2316 const gchar *detailed_signal,
2321 gulong handler_seq_no = 0;
2325 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2326 g_return_val_if_fail (detailed_signal != NULL, 0);
2327 g_return_val_if_fail (closure != NULL, 0);
2330 itype = G_TYPE_FROM_INSTANCE (instance);
2331 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2334 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2336 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2337 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2338 else if (!g_type_is_a (itype, node->itype))
2339 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2340 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2343 Handler *handler = handler_new (after);
2345 handler_seq_no = handler->sequential_number;
2346 handler->detail = detail;
2347 handler->closure = g_closure_ref (closure);
2348 g_closure_sink (closure);
2349 add_invalid_closure_notify (handler, instance);
2350 handler_insert (signal_id, instance, handler);
2351 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2353 g_closure_set_marshal (handler->closure, node->c_marshaller);
2354 if (node->va_marshaller)
2355 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2360 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2361 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2364 return handler_seq_no;
2368 node_check_deprecated (const SignalNode *node)
2370 static const gchar * g_enable_diagnostic = NULL;
2372 if (G_UNLIKELY (!g_enable_diagnostic))
2374 g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2375 if (!g_enable_diagnostic)
2376 g_enable_diagnostic = "0";
2379 if (g_enable_diagnostic[0] == '1')
2381 if (node->flags & G_SIGNAL_DEPRECATED)
2383 g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2384 "anymore. It will be removed in a future version.",
2385 type_debug_name (node->itype), node->name);
2391 * g_signal_connect_data:
2392 * @instance: (type GObject.Object): the instance to connect to.
2393 * @detailed_signal: a string of the form "signal-name::detail".
2394 * @c_handler: the #GCallback to connect.
2395 * @data: data to pass to @c_handler calls.
2396 * @destroy_data: a #GClosureNotify for @data.
2397 * @connect_flags: a combination of #GConnectFlags.
2399 * Connects a #GCallback function to a signal for a particular object. Similar
2400 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2401 * which will be called when the signal handler is disconnected and no longer
2402 * used. Specify @connect_flags if you need `..._after()` or
2403 * `..._swapped()` variants of this function.
2405 * Returns: the handler id (always greater than 0 for successful connections)
2408 g_signal_connect_data (gpointer instance,
2409 const gchar *detailed_signal,
2410 GCallback c_handler,
2412 GClosureNotify destroy_data,
2413 GConnectFlags connect_flags)
2416 gulong handler_seq_no = 0;
2419 gboolean swapped, after;
2421 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2422 g_return_val_if_fail (detailed_signal != NULL, 0);
2423 g_return_val_if_fail (c_handler != NULL, 0);
2425 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2426 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2429 itype = G_TYPE_FROM_INSTANCE (instance);
2430 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2433 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2435 node_check_deprecated (node);
2437 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2438 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2439 else if (!g_type_is_a (itype, node->itype))
2440 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2441 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2444 Handler *handler = handler_new (after);
2446 handler_seq_no = handler->sequential_number;
2447 handler->detail = detail;
2448 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2449 g_closure_sink (handler->closure);
2450 handler_insert (signal_id, instance, handler);
2451 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2453 g_closure_set_marshal (handler->closure, node->c_marshaller);
2454 if (node->va_marshaller)
2455 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2460 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2461 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2464 return handler_seq_no;
2468 * g_signal_handler_block:
2469 * @instance: (type GObject.Object): The instance to block the signal handler of.
2470 * @handler_id: Handler id of the handler to be blocked.
2472 * Blocks a handler of an instance so it will not be called during any
2473 * signal emissions unless it is unblocked again. Thus "blocking" a
2474 * signal handler means to temporarily deactive it, a signal handler
2475 * has to be unblocked exactly the same amount of times it has been
2476 * blocked before to become active again.
2478 * The @handler_id has to be a valid signal handler id, connected to a
2479 * signal of @instance.
2482 g_signal_handler_block (gpointer instance,
2487 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2488 g_return_if_fail (handler_id > 0);
2491 handler = handler_lookup (instance, handler_id, NULL, NULL);
2494 #ifndef G_DISABLE_CHECKS
2495 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2496 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2498 handler->block_count += 1;
2501 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2506 * g_signal_handler_unblock:
2507 * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2508 * @handler_id: Handler id of the handler to be unblocked.
2510 * Undoes the effect of a previous g_signal_handler_block() call. A
2511 * blocked handler is skipped during signal emissions and will not be
2512 * invoked, unblocking it (for exactly the amount of times it has been
2513 * blocked before) reverts its "blocked" state, so the handler will be
2514 * recognized by the signal system and is called upon future or
2515 * currently ongoing signal emissions (since the order in which
2516 * handlers are called during signal emissions is deterministic,
2517 * whether the unblocked handler in question is called as part of a
2518 * currently ongoing emission depends on how far that emission has
2521 * The @handler_id has to be a valid id of a signal handler that is
2522 * connected to a signal of @instance and is currently blocked.
2525 g_signal_handler_unblock (gpointer instance,
2530 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2531 g_return_if_fail (handler_id > 0);
2534 handler = handler_lookup (instance, handler_id, NULL, NULL);
2537 if (handler->block_count)
2538 handler->block_count -= 1;
2540 g_warning (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2543 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2548 * g_signal_handler_disconnect:
2549 * @instance: (type GObject.Object): The instance to remove the signal handler from.
2550 * @handler_id: Handler id of the handler to be disconnected.
2552 * Disconnects a handler from an instance so it will not be called during
2553 * any future or currently ongoing emissions of the signal it has been
2554 * connected to. The @handler_id becomes invalid and may be reused.
2556 * The @handler_id has to be a valid signal handler id, connected to a
2557 * signal of @instance.
2560 g_signal_handler_disconnect (gpointer instance,
2566 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2567 g_return_if_fail (handler_id > 0);
2570 handler = handler_lookup (instance, handler_id, NULL, &signal_id);
2573 handler->sequential_number = 0;
2574 handler->block_count = 1;
2575 remove_invalid_closure_notify (handler, instance);
2576 handler_unref_R (signal_id, instance, handler);
2579 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2584 * g_signal_handler_is_connected:
2585 * @instance: (type GObject.Object): The instance where a signal handler is sought.
2586 * @handler_id: the handler id.
2588 * Returns whether @handler_id is the id of a handler connected to @instance.
2590 * Returns: whether @handler_id identifies a handler connected to @instance.
2593 g_signal_handler_is_connected (gpointer instance,
2599 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2602 handler = handler_lookup (instance, handler_id, NULL, NULL);
2603 connected = handler != NULL;
2610 * g_signal_handlers_destroy:
2611 * @instance: (type GObject.Object): The instance whose signal handlers are destroyed
2613 * Destroy all signal handlers of a type instance. This function is
2614 * an implementation detail of the #GObject dispose implementation,
2615 * and should not be used outside of the type system.
2618 g_signal_handlers_destroy (gpointer instance)
2620 GBSearchArray *hlbsa;
2622 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2625 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2630 /* reentrancy caution, delete instance trace first */
2631 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2633 for (i = 0; i < hlbsa->n_nodes; i++)
2635 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2636 Handler *handler = hlist->handlers;
2640 Handler *tmp = handler;
2642 handler = tmp->next;
2643 tmp->block_count = 1;
2644 /* cruel unlink, this works because _all_ handlers vanish */
2647 if (tmp->sequential_number)
2649 remove_invalid_closure_notify (tmp, instance);
2650 tmp->sequential_number = 0;
2651 handler_unref_R (0, NULL, tmp);
2655 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2661 * g_signal_handler_find:
2662 * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2663 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2664 * and/or @data the handler has to match.
2665 * @signal_id: Signal the handler has to be connected to.
2666 * @detail: Signal detail the handler has to be connected to.
2667 * @closure: (allow-none): The closure the handler will invoke.
2668 * @func: The C closure callback of the handler (useless for non-C closures).
2669 * @data: The closure data of the handler's closure.
2671 * Finds the first signal handler that matches certain selection criteria.
2672 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2673 * flags, and the criteria values are passed as arguments.
2674 * The match @mask has to be non-0 for successful matches.
2675 * If no handler was found, 0 is returned.
2677 * Returns: A valid non-0 signal handler id for a successful match.
2680 g_signal_handler_find (gpointer instance,
2681 GSignalMatchType mask,
2688 gulong handler_seq_no = 0;
2690 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2691 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2693 if (mask & G_SIGNAL_MATCH_MASK)
2695 HandlerMatch *mlist;
2698 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2701 handler_seq_no = mlist->handler->sequential_number;
2702 handler_match_free1_R (mlist, instance);
2707 return handler_seq_no;
2711 signal_handlers_foreach_matched_R (gpointer instance,
2712 GSignalMatchType mask,
2718 void (*callback) (gpointer instance,
2719 gulong handler_seq_no))
2721 HandlerMatch *mlist;
2722 guint n_handlers = 0;
2724 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2728 if (mlist->handler->sequential_number)
2731 callback (instance, mlist->handler->sequential_number);
2734 mlist = handler_match_free1_R (mlist, instance);
2741 * g_signal_handlers_block_matched:
2742 * @instance: (type GObject.Object): The instance to block handlers from.
2743 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2744 * and/or @data the handlers have to match.
2745 * @signal_id: Signal the handlers have to be connected to.
2746 * @detail: Signal detail the handlers have to be connected to.
2747 * @closure: (allow-none): The closure the handlers will invoke.
2748 * @func: The C closure callback of the handlers (useless for non-C closures).
2749 * @data: The closure data of the handlers' closures.
2751 * Blocks all handlers on an instance that match a certain selection criteria.
2752 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2753 * flags, and the criteria values are passed as arguments.
2754 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2755 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2756 * If no handlers were found, 0 is returned, the number of blocked handlers
2759 * Returns: The number of handlers that matched.
2762 g_signal_handlers_block_matched (gpointer instance,
2763 GSignalMatchType mask,
2770 guint n_handlers = 0;
2772 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2773 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2775 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2778 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2779 closure, func, data,
2780 g_signal_handler_block);
2788 * g_signal_handlers_unblock_matched:
2789 * @instance: (type GObject.Object): The instance to unblock handlers from.
2790 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2791 * and/or @data the handlers have to match.
2792 * @signal_id: Signal the handlers have to be connected to.
2793 * @detail: Signal detail the handlers have to be connected to.
2794 * @closure: (allow-none): The closure the handlers will invoke.
2795 * @func: The C closure callback of the handlers (useless for non-C closures).
2796 * @data: The closure data of the handlers' closures.
2798 * Unblocks all handlers on an instance that match a certain selection
2799 * criteria. The criteria mask is passed as an OR-ed combination of
2800 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2801 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2802 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2803 * If no handlers were found, 0 is returned, the number of unblocked handlers
2804 * otherwise. The match criteria should not apply to any handlers that are
2805 * not currently blocked.
2807 * Returns: The number of handlers that matched.
2810 g_signal_handlers_unblock_matched (gpointer instance,
2811 GSignalMatchType mask,
2818 guint n_handlers = 0;
2820 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2821 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2823 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2826 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2827 closure, func, data,
2828 g_signal_handler_unblock);
2836 * g_signal_handlers_disconnect_matched:
2837 * @instance: (type GObject.Object): The instance to remove handlers from.
2838 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2839 * and/or @data the handlers have to match.
2840 * @signal_id: Signal the handlers have to be connected to.
2841 * @detail: Signal detail the handlers have to be connected to.
2842 * @closure: (allow-none): The closure the handlers will invoke.
2843 * @func: The C closure callback of the handlers (useless for non-C closures).
2844 * @data: The closure data of the handlers' closures.
2846 * Disconnects all handlers on an instance that match a certain
2847 * selection criteria. The criteria mask is passed as an OR-ed
2848 * combination of #GSignalMatchType flags, and the criteria values are
2849 * passed as arguments. Passing at least one of the
2850 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2851 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2852 * matches. If no handlers were found, 0 is returned, the number of
2853 * disconnected handlers otherwise.
2855 * Returns: The number of handlers that matched.
2858 g_signal_handlers_disconnect_matched (gpointer instance,
2859 GSignalMatchType mask,
2866 guint n_handlers = 0;
2868 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2869 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2871 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2874 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2875 closure, func, data,
2876 g_signal_handler_disconnect);
2884 * g_signal_has_handler_pending:
2885 * @instance: (type GObject.Object): the object whose signal handlers are sought.
2886 * @signal_id: the signal id.
2887 * @detail: the detail.
2888 * @may_be_blocked: whether blocked handlers should count as match.
2890 * Returns whether there are any handlers connected to @instance for the
2891 * given signal id and detail.
2893 * One example of when you might use this is when the arguments to the
2894 * signal are difficult to compute. A class implementor may opt to not
2895 * emit the signal if no one is attached anyway, thus saving the cost
2896 * of building the arguments.
2898 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2902 g_signal_has_handler_pending (gpointer instance,
2905 gboolean may_be_blocked)
2907 HandlerMatch *mlist;
2908 gboolean has_pending;
2910 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2911 g_return_val_if_fail (signal_id > 0, FALSE);
2916 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2918 if (!(node->flags & G_SIGNAL_DETAILED))
2920 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2925 mlist = handlers_find (instance,
2926 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2927 signal_id, detail, NULL, NULL, NULL, TRUE);
2931 handler_match_free1_R (mlist, instance);
2934 has_pending = FALSE;
2942 * @instance_and_params: (array): argument list for the signal emission.
2943 * The first element in the array is a #GValue for the instance the signal
2944 * is being emitted on. The rest are any arguments to be passed to the signal.
2945 * @signal_id: the signal id
2946 * @detail: the detail
2947 * @return_value: Location to store the return value of the signal emission.
2951 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2952 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2955 g_signal_emitv (const GValue *instance_and_params,
2958 GValue *return_value)
2962 #ifdef G_ENABLE_DEBUG
2963 const GValue *param_values;
2967 g_return_if_fail (instance_and_params != NULL);
2968 instance = g_value_peek_pointer (instance_and_params);
2969 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2970 g_return_if_fail (signal_id > 0);
2972 #ifdef G_ENABLE_DEBUG
2973 param_values = instance_and_params + 1;
2977 node = LOOKUP_SIGNAL_NODE (signal_id);
2978 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2980 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2984 #ifdef G_ENABLE_DEBUG
2985 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2987 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2991 for (i = 0; i < node->n_params; i++)
2992 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2994 g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
2996 type_debug_name (node->param_types[i]),
2999 G_VALUE_TYPE_NAME (param_values + i));
3003 if (node->return_type != G_TYPE_NONE)
3007 g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3009 type_debug_name (node->return_type),
3014 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3016 g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3018 type_debug_name (node->return_type),
3020 G_VALUE_TYPE_NAME (return_value));
3026 return_value = NULL;
3027 #endif /* G_ENABLE_DEBUG */
3029 /* optimize NOP emissions */
3030 if (!node->single_va_closure_is_valid)
3031 node_update_single_va_closure (node);
3033 if (node->single_va_closure != NULL &&
3034 (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3035 _g_closure_is_void (node->single_va_closure, instance)))
3037 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3038 if (hlist == NULL || hlist->handlers == NULL)
3040 /* nothing to do to emit this signal */
3042 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3048 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
3051 static inline gboolean
3052 accumulate (GSignalInvocationHint *ihint,
3053 GValue *return_accu,
3054 GValue *handler_return,
3055 SignalAccumulator *accumulator)
3057 gboolean continue_emission;
3062 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3063 g_value_reset (handler_return);
3065 return continue_emission;
3069 * g_signal_emit_valist: (skip)
3070 * @instance: the instance the signal is being emitted on.
3071 * @signal_id: the signal id
3072 * @detail: the detail
3073 * @var_args: a list of parameters to be passed to the signal, followed by a
3074 * location for the return value. If the return type of the signal
3075 * is #G_TYPE_NONE, the return value location can be omitted.
3079 * Note that g_signal_emit_valist() resets the return value to the default
3080 * if no handlers are connected, in contrast to g_signal_emitv().
3083 g_signal_emit_valist (gpointer instance,
3088 GValue *instance_and_params;
3089 GType signal_return_type;
3090 GValue *param_values;
3094 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3095 g_return_if_fail (signal_id > 0);
3098 node = LOOKUP_SIGNAL_NODE (signal_id);
3099 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3101 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3105 #ifndef G_DISABLE_CHECKS
3106 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3108 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3112 #endif /* !G_DISABLE_CHECKS */
3114 if (!node->single_va_closure_is_valid)
3115 node_update_single_va_closure (node);
3117 if (node->single_va_closure != NULL)
3119 HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3120 Handler *fastpath_handler = NULL;
3122 GClosure *closure = NULL;
3123 gboolean fastpath = TRUE;
3124 GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3126 if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3127 !_g_closure_is_void (node->single_va_closure, instance))
3129 if (_g_closure_supports_invoke_va (node->single_va_closure))
3131 closure = node->single_va_closure;
3132 if (node->single_va_closure_is_after)
3133 run_type = G_SIGNAL_RUN_LAST;
3135 run_type = G_SIGNAL_RUN_FIRST;
3141 for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3143 if (!l->block_count &&
3144 (!l->detail || l->detail == detail))
3146 if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3153 fastpath_handler = l;
3154 closure = l->closure;
3156 run_type = G_SIGNAL_RUN_LAST;
3158 run_type = G_SIGNAL_RUN_FIRST;
3163 if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3169 /* Don't allow no-recurse emission as we might have to restart, which means
3170 we will run multiple handlers and thus must ref all arguments */
3171 if (closure != NULL && (node->flags & (G_SIGNAL_NO_RECURSE)) != 0)
3176 SignalAccumulator *accumulator;
3178 GValue *return_accu, accu = G_VALUE_INIT;
3180 GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3181 GValue emission_return = G_VALUE_INIT;
3182 GType rtype = node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3183 gboolean static_scope = node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3185 signal_id = node->signal_id;
3186 accumulator = node->accumulator;
3187 if (rtype == G_TYPE_NONE)
3189 else if (accumulator)
3190 return_accu = &accu;
3192 return_accu = &emission_return;
3194 emission.instance = instance;
3195 emission.ihint.signal_id = signal_id;
3196 emission.ihint.detail = detail;
3197 emission.ihint.run_type = run_type;
3198 emission.state = EMISSION_RUN;
3199 emission.chain_type = instance_type;
3200 emission_push (&g_recursive_emissions, &emission);
3202 if (fastpath_handler)
3203 handler_ref (fastpath_handler);
3207 TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3209 if (rtype != G_TYPE_NONE)
3210 g_value_init (&emission_return, rtype);
3213 g_value_init (&accu, rtype);
3215 if (closure != NULL)
3217 g_object_ref (instance);
3218 _g_closure_invoke_va (closure,
3224 accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3229 emission.chain_type = G_TYPE_NONE;
3230 emission_pop (&g_recursive_emissions, &emission);
3232 if (fastpath_handler)
3233 handler_unref_R (signal_id, instance, fastpath_handler);
3238 g_value_unset (&accu);
3240 if (rtype != G_TYPE_NONE)
3242 gchar *error = NULL;
3243 for (i = 0; i < node->n_params; i++)
3245 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3246 G_VALUE_COLLECT_SKIP (ptype, var_args);
3249 G_VALUE_LCOPY (&emission_return,
3251 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3254 g_value_unset (&emission_return);
3257 g_warning ("%s: %s", G_STRLOC, error);
3259 /* we purposely leak the value here, it might not be
3260 * in a sane state if an error condition occurred
3265 TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3267 if (closure != NULL)
3268 g_object_unref (instance);
3275 n_params = node->n_params;
3276 signal_return_type = node->return_type;
3277 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
3278 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
3279 param_values = instance_and_params + 1;
3281 for (i = 0; i < node->n_params; i++)
3284 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3285 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3287 G_VALUE_COLLECT_INIT (param_values + i, ptype,
3289 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3293 g_warning ("%s: %s", G_STRLOC, error);
3296 /* we purposely leak the value here, it might not be
3297 * in a sane state if an error condition occoured
3300 g_value_unset (param_values + i);
3306 instance_and_params->g_type = 0;
3307 g_value_init_from_instance (instance_and_params, instance);
3308 if (signal_return_type == G_TYPE_NONE)
3309 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
3312 GValue return_value = G_VALUE_INIT;
3313 gchar *error = NULL;
3314 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3315 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3317 g_value_init (&return_value, rtype);
3319 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3321 G_VALUE_LCOPY (&return_value,
3323 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3326 g_value_unset (&return_value);
3329 g_warning ("%s: %s", G_STRLOC, error);
3332 /* we purposely leak the value here, it might not be
3333 * in a sane state if an error condition occurred
3337 for (i = 0; i < n_params; i++)
3338 g_value_unset (param_values + i);
3339 g_value_unset (instance_and_params);
3344 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3345 * @signal_id: the signal id
3346 * @detail: the detail
3347 * @...: parameters to be passed to the signal, followed by a
3348 * location for the return value. If the return type of the signal
3349 * is #G_TYPE_NONE, the return value location can be omitted.
3353 * Note that g_signal_emit() resets the return value to the default
3354 * if no handlers are connected, in contrast to g_signal_emitv().
3357 g_signal_emit (gpointer instance,
3364 va_start (var_args, detail);
3365 g_signal_emit_valist (instance, signal_id, detail, var_args);
3370 * g_signal_emit_by_name:
3371 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3372 * @detailed_signal: a string of the form "signal-name::detail".
3373 * @...: parameters to be passed to the signal, followed by a
3374 * location for the return value. If the return type of the signal
3375 * is #G_TYPE_NONE, the return value location can be omitted.
3379 * Note that g_signal_emit_by_name() resets the return value to the default
3380 * if no handlers are connected, in contrast to g_signal_emitv().
3383 g_signal_emit_by_name (gpointer instance,
3384 const gchar *detailed_signal,
3391 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3392 g_return_if_fail (detailed_signal != NULL);
3394 itype = G_TYPE_FROM_INSTANCE (instance);
3397 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3404 va_start (var_args, detailed_signal);
3405 g_signal_emit_valist (instance, signal_id, detail, var_args);
3409 g_warning ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3410 G_STRLOC, detailed_signal, instance, g_type_name (itype));
3414 signal_emit_unlocked_R (SignalNode *node,
3417 GValue *emission_return,
3418 const GValue *instance_and_params)
3420 SignalAccumulator *accumulator;
3422 GClosure *class_closure;
3424 Handler *handler_list = NULL;
3425 GValue *return_accu, accu = G_VALUE_INIT;
3427 gulong max_sequential_handler_number;
3428 gboolean return_value_altered = FALSE;
3430 TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3433 signal_id = node->signal_id;
3435 if (node->flags & G_SIGNAL_NO_RECURSE)
3437 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3441 node->state = EMISSION_RESTART;
3443 return return_value_altered;
3446 accumulator = node->accumulator;
3450 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3451 return_accu = &accu;
3455 return_accu = emission_return;
3456 emission.instance = instance;
3457 emission.ihint.signal_id = node->signal_id;
3458 emission.ihint.detail = detail;
3459 emission.ihint.run_type = 0;
3461 emission.chain_type = G_TYPE_NONE;
3462 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3463 class_closure = signal_lookup_closure (node, instance);
3468 handler_unref_R (signal_id, instance, handler_list);
3469 max_sequential_handler_number = g_handler_sequential_number;
3470 hlist = handler_list_lookup (signal_id, instance);
3471 handler_list = hlist ? hlist->handlers : NULL;
3473 handler_ref (handler_list);
3475 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3477 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3479 emission.state = EMISSION_RUN;
3481 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3483 g_closure_invoke (class_closure,
3486 instance_and_params,
3488 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3489 emission.state == EMISSION_RUN)
3490 emission.state = EMISSION_STOP;
3492 emission.chain_type = G_TYPE_NONE;
3493 return_value_altered = TRUE;
3495 if (emission.state == EMISSION_STOP)
3497 else if (emission.state == EMISSION_RESTART)
3501 if (node->emission_hooks)
3503 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3506 emission.state = EMISSION_HOOK;
3507 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3510 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3512 if (!signal_hook->detail || signal_hook->detail == detail)
3514 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3516 was_in_call = G_HOOK_IN_CALL (hook);
3517 hook->flags |= G_HOOK_FLAG_IN_CALL;
3519 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3522 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3524 g_hook_destroy_link (node->emission_hooks, hook);
3526 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3529 if (emission.state == EMISSION_RESTART)
3535 Handler *handler = handler_list;
3537 emission.state = EMISSION_RUN;
3538 handler_ref (handler);
3545 handler_unref_R (signal_id, instance, handler_list);
3546 handler_list = handler;
3549 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3550 handler->sequential_number < max_sequential_handler_number)
3553 g_closure_invoke (handler->closure,
3556 instance_and_params,
3558 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3559 emission.state == EMISSION_RUN)
3560 emission.state = EMISSION_STOP;
3562 return_value_altered = TRUE;
3564 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3567 tmp = handler->next;
3571 handler_unref_R (signal_id, instance, handler_list);
3572 handler_list = handler;
3577 if (emission.state == EMISSION_STOP)
3579 else if (emission.state == EMISSION_RESTART)
3583 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3585 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3587 emission.state = EMISSION_RUN;
3589 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3591 g_closure_invoke (class_closure,
3594 instance_and_params,
3596 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3597 emission.state == EMISSION_RUN)
3598 emission.state = EMISSION_STOP;
3600 emission.chain_type = G_TYPE_NONE;
3601 return_value_altered = TRUE;
3603 if (emission.state == EMISSION_STOP)
3605 else if (emission.state == EMISSION_RESTART)
3611 Handler *handler = handler_list;
3613 emission.state = EMISSION_RUN;
3614 handler_ref (handler);
3619 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3620 handler->sequential_number < max_sequential_handler_number)
3623 g_closure_invoke (handler->closure,
3626 instance_and_params,
3628 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3629 emission.state == EMISSION_RUN)
3630 emission.state = EMISSION_STOP;
3632 return_value_altered = TRUE;
3634 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3637 tmp = handler->next;
3641 handler_unref_R (signal_id, instance, handler);
3646 if (emission.state == EMISSION_STOP)
3648 else if (emission.state == EMISSION_RESTART)
3654 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3656 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3658 gboolean need_unset = FALSE;
3660 emission.state = EMISSION_STOP;
3662 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3664 if (node->return_type != G_TYPE_NONE && !accumulator)
3666 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3669 g_closure_invoke (class_closure,
3670 node->return_type != G_TYPE_NONE ? &accu : NULL,
3672 instance_and_params,
3675 g_value_unset (&accu);
3677 emission.chain_type = G_TYPE_NONE;
3679 if (emission.state == EMISSION_RESTART)
3684 handler_unref_R (signal_id, instance, handler_list);
3686 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3689 g_value_unset (&accu);
3691 TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3693 return return_value_altered;
3697 add_invalid_closure_notify (Handler *handler,
3700 g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3701 handler->has_invalid_closure_notify = 1;
3705 remove_invalid_closure_notify (Handler *handler,
3708 if (handler->has_invalid_closure_notify)
3710 g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3711 handler->has_invalid_closure_notify = 0;
3716 invalid_closure_notify (gpointer instance,
3724 handler = handler_lookup (instance, 0, closure, &signal_id);
3725 g_assert (handler->closure == closure);
3727 handler->sequential_number = 0;
3728 handler->block_count = 1;
3729 handler_unref_R (signal_id, instance, handler);
3735 type_debug_name (GType type)
3739 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3740 return name ? name : "<unknown>";
3747 * g_signal_accumulator_true_handled:
3748 * @ihint: standard #GSignalAccumulator parameter
3749 * @return_accu: standard #GSignalAccumulator parameter
3750 * @handler_return: standard #GSignalAccumulator parameter
3751 * @dummy: standard #GSignalAccumulator parameter
3753 * A predefined #GSignalAccumulator for signals that return a
3754 * boolean values. The behavior that this accumulator gives is
3755 * that a return of %TRUE stops the signal emission: no further
3756 * callbacks will be invoked, while a return of %FALSE allows
3757 * the emission to continue. The idea here is that a %TRUE return
3758 * indicates that the callback handled the signal, and no further
3759 * handling is needed.
3763 * Returns: standard #GSignalAccumulator result
3766 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3767 GValue *return_accu,
3768 const GValue *handler_return,
3771 gboolean continue_emission;
3772 gboolean signal_handled;
3774 signal_handled = g_value_get_boolean (handler_return);
3775 g_value_set_boolean (return_accu, signal_handled);
3776 continue_emission = !signal_handled;
3778 return continue_emission;
3782 * g_signal_accumulator_first_wins:
3783 * @ihint: standard #GSignalAccumulator parameter
3784 * @return_accu: standard #GSignalAccumulator parameter
3785 * @handler_return: standard #GSignalAccumulator parameter
3786 * @dummy: standard #GSignalAccumulator parameter
3788 * A predefined #GSignalAccumulator for signals intended to be used as a
3789 * hook for application code to provide a particular value. Usually
3790 * only one such value is desired and multiple handlers for the same
3791 * signal don't make much sense (except for the case of the default
3792 * handler defined in the class structure, in which case you will
3793 * usually want the signal connection to override the class handler).
3795 * This accumulator will use the return value from the first signal
3796 * handler that is run as the return value for the signal and not run
3797 * any further handlers (ie: the first handler "wins").
3799 * Returns: standard #GSignalAccumulator result
3804 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
3805 GValue *return_accu,
3806 const GValue *handler_return,
3809 g_value_copy (handler_return, return_accu);