1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
33 #include "gbsearcharray.h"
34 #include "gvaluecollector.h"
35 #include "gvaluetypes.h"
39 #include "gobject_trace.h"
40 #include "gobjectalias.h"
45 * @short_description: A means for customization of object behaviour
46 * and a general purpose notification mechanism
49 * The basic concept of the signal system is that of the
50 * <emphasis>emission</emphasis> of a signal. Signals are introduced
51 * per-type and are identified through strings. Signals introduced
52 * for a parent type are available in derived types as well, so
53 * basically they are a per-type facility that is inherited. A signal
54 * emission mainly involves invocation of a certain set of callbacks
55 * in precisely defined manner. There are two main categories of such
56 * callbacks, per-object
57 * <footnote><para>Although signals can deal with any kind of instantiatable
58 * type, i'm referring to those types as "object types" in the following,
59 * simply because that is the context most users will encounter signals in.
61 * ones and user provided ones.
62 * The per-object callbacks are most often referred to as "object method
63 * handler" or "default (signal) handler", while user provided callbacks are
64 * usually just called "signal handler".
65 * The object method handler is provided at signal creation time (this most
66 * frequently happens at the end of an object class' creation), while user
67 * provided handlers are frequently connected and disconnected to/from a certain
68 * signal on certain object instances.
70 * A signal emission consists of five stages, unless prematurely stopped:
72 * <varlistentry><term></term><listitem><para>
73 * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
74 * </para></listitem></varlistentry>
75 * <varlistentry><term></term><listitem><para>
76 * 2 - Invocation of normal user-provided signal handlers (<emphasis>after</emphasis> flag %FALSE)
77 * </para></listitem></varlistentry>
78 * <varlistentry><term></term><listitem><para>
79 * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
80 * </para></listitem></varlistentry>
81 * <varlistentry><term></term><listitem><para>
82 * 4 - Invocation of user provided signal handlers, connected with an <emphasis>after</emphasis> flag of %TRUE
83 * </para></listitem></varlistentry>
84 * <varlistentry><term></term><listitem><para>
85 * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
86 * </para></listitem></varlistentry>
88 * The user-provided signal handlers are called in the order they were
90 * All handlers may prematurely stop a signal emission, and any number of
91 * handlers may be connected, disconnected, blocked or unblocked during
93 * There are certain criteria for skipping user handlers in stages 2 and 4
94 * of a signal emission.
95 * First, user handlers may be <emphasis>blocked</emphasis>, blocked handlers are omitted
96 * during callback invocation, to return from the "blocked" state, a
97 * handler has to get unblocked exactly the same amount of times
98 * it has been blocked before.
99 * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
100 * "detail" argument passed in to g_signal_emit() has to match the detail
101 * argument of the signal handler currently subject to invocation.
102 * Specification of no detail argument for signal handlers (omission of the
103 * detail part of the signal specification upon connection) serves as a
104 * wildcard and matches any detail argument passed in to emission.
108 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
109 #ifdef G_ENABLE_DEBUG
110 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
111 static volatile gpointer g_trace_instance_signals = NULL;
112 static volatile gpointer g_trap_instance_signals = NULL;
113 #endif /* G_ENABLE_DEBUG */
116 /* --- typedefs --- */
117 typedef struct _SignalNode SignalNode;
118 typedef struct _SignalKey SignalKey;
119 typedef struct _Emission Emission;
120 typedef struct _Handler Handler;
121 typedef struct _HandlerList HandlerList;
122 typedef struct _HandlerMatch HandlerMatch;
132 /* --- prototypes --- */
133 static inline guint signal_id_lookup (GQuark quark,
135 static void signal_destroy_R (SignalNode *signal_node);
136 static inline HandlerList* handler_list_ensure (guint signal_id,
138 static inline HandlerList* handler_list_lookup (guint signal_id,
140 static inline Handler* handler_new (gboolean after);
141 static void handler_insert (guint signal_id,
144 static Handler* handler_lookup (gpointer instance,
147 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
150 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
152 static HandlerMatch* handlers_find (gpointer instance,
153 GSignalMatchType mask,
159 gboolean one_and_only);
160 static inline void handler_ref (Handler *handler);
161 static inline void handler_unref_R (guint signal_id,
164 static gint handler_lists_cmp (gconstpointer node1,
165 gconstpointer node2);
166 static inline void emission_push (Emission **emission_list_p,
168 static inline void emission_pop (Emission **emission_list_p,
170 static inline Emission* emission_find (Emission *emission_list,
174 static gint class_closures_cmp (gconstpointer node1,
175 gconstpointer node2);
176 static gint signal_key_cmp (gconstpointer node1,
177 gconstpointer node2);
178 static gboolean signal_emit_unlocked_R (SignalNode *node,
181 GValue *return_value,
182 const GValue *instance_and_params);
183 static const gchar * type_debug_name (GType type);
186 /* --- structures --- */
189 GSignalAccumulator func;
197 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
201 /* permanent portion */
207 /* reinitializable portion */
208 guint test_class_offset : 12;
211 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
212 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
213 GBSearchArray *class_closure_bsa;
214 SignalAccumulator *accumulator;
215 GSignalCMarshaller c_marshaller;
216 GHookList *emission_hooks;
218 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
219 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
232 GSignalInvocationHint ihint;
241 Handler *tail_before; /* normal signal handlers are appended here */
242 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
247 gulong sequential_number;
252 guint block_count : 16;
253 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
266 GType instance_type; /* 0 for default closure */
271 /* --- variables --- */
272 static GBSearchArray *g_signal_key_bsa = NULL;
273 static const GBSearchConfig g_signal_key_bconfig = {
276 G_BSEARCH_ARRAY_ALIGN_POWER2,
278 static GBSearchConfig g_signal_hlbsa_bconfig = {
279 sizeof (HandlerList),
283 static GBSearchConfig g_class_closure_bconfig = {
284 sizeof (ClassClosure),
288 static GHashTable *g_handler_list_bsa_ht = NULL;
289 static Emission *g_recursive_emissions = NULL;
290 static Emission *g_restart_emissions = NULL;
291 static gulong g_handler_sequential_number = 1;
292 G_LOCK_DEFINE_STATIC (g_signal_mutex);
293 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
294 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
297 /* --- signal nodes --- */
298 static guint g_n_signal_nodes = 0;
299 static SignalNode **g_signal_nodes = NULL;
301 static inline SignalNode*
302 LOOKUP_SIGNAL_NODE (register guint signal_id)
304 if (signal_id < g_n_signal_nodes)
305 return g_signal_nodes[signal_id];
311 /* --- functions --- */
313 signal_id_lookup (GQuark quark,
316 GType *ifaces, type = itype;
322 /* try looking up signals for this type and its ancestors */
325 SignalKey *signal_key;
328 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
331 return signal_key->signal_id;
333 type = g_type_parent (type);
337 /* no luck, try interfaces it exports */
338 ifaces = g_type_interfaces (itype, &n_ifaces);
341 SignalKey *signal_key;
343 key.itype = ifaces[n_ifaces];
344 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
349 return signal_key->signal_id;
358 class_closures_cmp (gconstpointer node1,
361 const ClassClosure *c1 = node1, *c2 = node2;
363 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
367 handler_lists_cmp (gconstpointer node1,
370 const HandlerList *hlist1 = node1, *hlist2 = node2;
372 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
375 static inline HandlerList*
376 handler_list_ensure (guint signal_id,
379 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
382 key.signal_id = signal_id;
384 key.tail_before = NULL;
385 key.tail_after = NULL;
388 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
389 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
390 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
394 GBSearchArray *o = hlbsa;
396 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
398 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
400 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
403 static inline HandlerList*
404 handler_list_lookup (guint signal_id,
407 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
410 key.signal_id = signal_id;
412 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
416 handler_lookup (gpointer instance,
420 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
426 for (i = 0; i < hlbsa->n_nodes; i++)
428 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
431 for (handler = hlist->handlers; handler; handler = handler->next)
432 if (handler->sequential_number == handler_id)
435 *signal_id_p = hlist->signal_id;
445 static inline HandlerMatch*
446 handler_match_prepend (HandlerMatch *list,
452 node = g_slice_new (HandlerMatch);
453 node->handler = handler;
455 node->signal_id = signal_id;
456 handler_ref (handler);
460 static inline HandlerMatch*
461 handler_match_free1_R (HandlerMatch *node,
464 HandlerMatch *next = node->next;
466 handler_unref_R (node->signal_id, instance, node->handler);
467 g_slice_free (HandlerMatch, node);
473 handlers_find (gpointer instance,
474 GSignalMatchType mask,
480 gboolean one_and_only)
482 HandlerMatch *mlist = NULL;
484 if (mask & G_SIGNAL_MATCH_ID)
486 HandlerList *hlist = handler_list_lookup (signal_id, instance);
488 SignalNode *node = NULL;
490 if (mask & G_SIGNAL_MATCH_FUNC)
492 node = LOOKUP_SIGNAL_NODE (signal_id);
493 if (!node || !node->c_marshaller)
498 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
499 if (handler->sequential_number &&
500 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
501 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
502 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
503 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
504 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
505 handler->closure->meta_marshal == 0 &&
506 ((GCClosure*) handler->closure)->callback == func)))
508 mlist = handler_match_prepend (mlist, handler, signal_id);
515 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
522 for (i = 0; i < hlbsa->n_nodes; i++)
524 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
525 SignalNode *node = NULL;
528 if (!(mask & G_SIGNAL_MATCH_FUNC))
530 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
531 if (!node->c_marshaller)
535 for (handler = hlist->handlers; handler; handler = handler->next)
536 if (handler->sequential_number &&
537 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
538 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
539 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
540 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
541 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
542 handler->closure->meta_marshal == 0 &&
543 ((GCClosure*) handler->closure)->callback == func)))
545 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
556 static inline Handler*
557 handler_new (gboolean after)
559 Handler *handler = g_slice_new (Handler);
560 #ifndef G_DISABLE_CHECKS
561 if (g_handler_sequential_number < 1)
562 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
565 handler->sequential_number = g_handler_sequential_number++;
566 handler->prev = NULL;
567 handler->next = NULL;
569 handler->ref_count = 1;
570 handler->block_count = 0;
571 handler->after = after != FALSE;
572 handler->closure = NULL;
578 handler_ref (Handler *handler)
580 g_return_if_fail (handler->ref_count > 0);
582 g_atomic_int_inc ((int *)&handler->ref_count);
586 handler_unref_R (guint signal_id,
592 g_return_if_fail (handler->ref_count > 0);
594 is_zero = g_atomic_int_dec_and_test ((int *)&handler->ref_count);
596 if (G_UNLIKELY (is_zero))
598 HandlerList *hlist = NULL;
601 handler->next->prev = handler->prev;
602 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
603 handler->prev->next = handler->next;
606 hlist = handler_list_lookup (signal_id, instance);
607 hlist->handlers = handler->next;
612 /* check if we are removing the handler pointed to by tail_before */
613 if (!handler->after && (!handler->next || handler->next->after))
616 hlist = handler_list_lookup (signal_id, instance);
619 g_assert (hlist->tail_before == handler); /* paranoid */
620 hlist->tail_before = handler->prev;
624 /* check if we are removing the handler pointed to by tail_after */
628 hlist = handler_list_lookup (signal_id, instance);
631 g_assert (hlist->tail_after == handler); /* paranoid */
632 hlist->tail_after = handler->prev;
638 g_closure_unref (handler->closure);
640 g_slice_free (Handler, handler);
645 handler_insert (guint signal_id,
651 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
653 hlist = handler_list_ensure (signal_id, instance);
654 if (!hlist->handlers)
656 hlist->handlers = handler;
658 hlist->tail_before = handler;
660 else if (handler->after)
662 handler->prev = hlist->tail_after;
663 hlist->tail_after->next = handler;
667 if (hlist->tail_before)
669 handler->next = hlist->tail_before->next;
671 handler->next->prev = handler;
672 handler->prev = hlist->tail_before;
673 hlist->tail_before->next = handler;
675 else /* insert !after handler into a list of only after handlers */
677 handler->next = hlist->handlers;
679 handler->next->prev = handler;
680 hlist->handlers = handler;
682 hlist->tail_before = handler;
686 hlist->tail_after = handler;
690 emission_push (Emission **emission_list_p,
693 emission->next = *emission_list_p;
694 *emission_list_p = emission;
698 emission_pop (Emission **emission_list_p,
701 Emission *node, *last = NULL;
703 for (node = *emission_list_p; node; last = node, node = last->next)
704 if (node == emission)
707 last->next = node->next;
709 *emission_list_p = node->next;
712 g_assert_not_reached ();
715 static inline Emission*
716 emission_find (Emission *emission_list,
723 for (emission = emission_list; emission; emission = emission->next)
724 if (emission->instance == instance &&
725 emission->ihint.signal_id == signal_id &&
726 emission->ihint.detail == detail)
731 static inline Emission*
732 emission_find_innermost (gpointer instance)
734 Emission *emission, *s = NULL, *c = NULL;
736 for (emission = g_restart_emissions; emission; emission = emission->next)
737 if (emission->instance == instance)
742 for (emission = g_recursive_emissions; emission; emission = emission->next)
743 if (emission->instance == instance)
753 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
757 signal_key_cmp (gconstpointer node1,
760 const SignalKey *key1 = node1, *key2 = node2;
762 if (key1->itype == key2->itype)
763 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
765 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
772 if (!g_n_signal_nodes)
774 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
775 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
776 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
778 /* invalid (0) signal_id */
779 g_n_signal_nodes = 1;
780 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
781 g_signal_nodes[0] = NULL;
787 _g_signals_destroy (GType itype)
792 for (i = 1; i < g_n_signal_nodes; i++)
794 SignalNode *node = g_signal_nodes[i];
796 if (node->itype == itype)
799 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
801 type_debug_name (node->itype));
803 signal_destroy_R (node);
810 * g_signal_stop_emission:
811 * @instance: the object whose signal handlers you wish to stop.
812 * @signal_id: the signal identifier, as returned by g_signal_lookup().
813 * @detail: the detail which the signal was emitted with.
815 * Stops a signal's current emission.
817 * This will prevent the default method from running, if the signal was
818 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
821 * Prints a warning if used on a signal which isn't being emitted.
824 g_signal_stop_emission (gpointer instance,
830 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
831 g_return_if_fail (signal_id > 0);
834 node = LOOKUP_SIGNAL_NODE (signal_id);
835 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
837 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
841 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
843 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
844 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
848 if (emission->state == EMISSION_HOOK)
849 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
850 node->name, instance);
851 else if (emission->state == EMISSION_RUN)
852 emission->state = EMISSION_STOP;
855 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
856 node->name, instance);
859 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
864 signal_finalize_hook (GHookList *hook_list,
867 GDestroyNotify destroy = hook->destroy;
871 hook->destroy = NULL;
873 destroy (hook->data);
879 * g_signal_add_emission_hook:
880 * @signal_id: the signal identifier, as returned by g_signal_lookup().
881 * @detail: the detail on which to call the hook.
882 * @hook_func: a #GSignalEmissionHook function.
883 * @hook_data: user data for @hook_func.
884 * @data_destroy: a #GDestroyNotify for @hook_data.
886 * Adds an emission hook for a signal, which will get called for any emission
887 * of that signal, independent of the instance. This is possible only
888 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
890 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
893 g_signal_add_emission_hook (guint signal_id,
895 GSignalEmissionHook hook_func,
897 GDestroyNotify data_destroy)
899 static gulong seq_hook_id = 1;
902 SignalHook *signal_hook;
904 g_return_val_if_fail (signal_id > 0, 0);
905 g_return_val_if_fail (hook_func != NULL, 0);
908 node = LOOKUP_SIGNAL_NODE (signal_id);
909 if (!node || node->destroyed)
911 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
915 if (node->flags & G_SIGNAL_NO_HOOKS)
917 g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
921 if (detail && !(node->flags & G_SIGNAL_DETAILED))
923 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
927 if (!node->emission_hooks)
929 node->emission_hooks = g_new (GHookList, 1);
930 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
931 node->emission_hooks->finalize_hook = signal_finalize_hook;
933 hook = g_hook_alloc (node->emission_hooks);
934 hook->data = hook_data;
935 hook->func = (gpointer) hook_func;
936 hook->destroy = data_destroy;
937 signal_hook = SIGNAL_HOOK (hook);
938 signal_hook->detail = detail;
939 node->emission_hooks->seq_id = seq_hook_id;
940 g_hook_append (node->emission_hooks, hook);
941 seq_hook_id = node->emission_hooks->seq_id;
944 return hook->hook_id;
948 * g_signal_remove_emission_hook:
949 * @signal_id: the id of the signal
950 * @hook_id: the id of the emission hook, as returned by
951 * g_signal_add_emission_hook()
953 * Deletes an emission hook.
956 g_signal_remove_emission_hook (guint signal_id,
961 g_return_if_fail (signal_id > 0);
962 g_return_if_fail (hook_id > 0);
965 node = LOOKUP_SIGNAL_NODE (signal_id);
966 if (!node || node->destroyed)
967 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
968 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
969 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
974 signal_parse_name (const gchar *name,
977 gboolean force_quark)
979 const gchar *colon = strchr (name, ':');
984 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
985 if (signal_id && detail_p)
988 else if (colon[1] == ':')
991 guint l = colon - name;
995 memcpy (buffer, name, l);
997 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1001 gchar *signal = g_new (gchar, l + 1);
1003 memcpy (signal, name, l);
1005 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1009 if (signal_id && detail_p)
1010 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1018 * g_signal_parse_name:
1019 * @detailed_signal: a string of the form "signal-name::detail".
1020 * @itype: The interface/instance type that introduced "signal-name".
1021 * @signal_id_p: Location to store the signal id.
1022 * @detail_p: Location to store the detail quark.
1023 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1025 * Internal function to parse a signal name into its @signal_id
1026 * and @detail quark.
1028 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1031 g_signal_parse_name (const gchar *detailed_signal,
1035 gboolean force_detail_quark)
1041 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1042 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1045 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1048 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1049 if (!node || node->destroyed ||
1050 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1054 *signal_id_p = signal_id;
1062 * g_signal_stop_emission_by_name:
1063 * @instance: the object whose signal handlers you wish to stop.
1064 * @detailed_signal: a string of the form "signal-name::detail".
1066 * Stops a signal's current emission.
1068 * This is just like g_signal_stop_emission() except it will look up the
1069 * signal id for you.
1072 g_signal_stop_emission_by_name (gpointer instance,
1073 const gchar *detailed_signal)
1079 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1080 g_return_if_fail (detailed_signal != NULL);
1083 itype = G_TYPE_FROM_INSTANCE (instance);
1084 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1087 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1089 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1090 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1091 else if (!g_type_is_a (itype, node->itype))
1092 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1095 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1096 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
1100 if (emission->state == EMISSION_HOOK)
1101 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
1102 node->name, instance);
1103 else if (emission->state == EMISSION_RUN)
1104 emission->state = EMISSION_STOP;
1107 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
1108 node->name, instance);
1112 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1118 * @name: the signal's name.
1119 * @itype: the type that the signal operates on.
1121 * Given the name of the signal and the type of object it connects to, gets
1122 * the signal's identifying integer. Emitting the signal by number is
1123 * somewhat faster than using the name each time.
1125 * Also tries the ancestors of the given type.
1127 * See g_signal_new() for details on allowed signal names.
1129 * Returns: the signal's identifying number, or 0 if no signal was found.
1132 g_signal_lookup (const gchar *name,
1136 g_return_val_if_fail (name != NULL, 0);
1137 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1140 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1144 /* give elaborate warnings */
1145 if (!g_type_name (itype))
1146 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'",
1148 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1149 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1150 name, g_type_name (itype));
1151 else if (!g_type_class_peek (itype))
1152 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1153 name, g_type_name (itype));
1160 * g_signal_list_ids:
1161 * @itype: Instance or interface type.
1162 * @n_ids: Location to store the number of signal ids for @itype.
1164 * Lists the signals by id that a certain instance or interface type
1165 * created. Further information about the signals can be acquired through
1168 * Returns: Newly allocated array of signal IDs.
1171 g_signal_list_ids (GType itype,
1179 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1180 g_return_val_if_fail (n_ids != NULL, NULL);
1183 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1184 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1185 result = g_array_new (FALSE, FALSE, sizeof (guint));
1187 for (i = 0; i < n_nodes; i++)
1188 if (keys[i].itype == itype)
1190 const gchar *name = g_quark_to_string (keys[i].quark);
1192 /* Signal names with "_" in them are aliases to the same
1193 * name with "-" instead of "_".
1195 if (!strchr (name, '_'))
1196 g_array_append_val (result, keys[i].signal_id);
1198 *n_ids = result->len;
1202 /* give elaborate warnings */
1203 if (!g_type_name (itype))
1204 g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'",
1206 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1207 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1208 g_type_name (itype));
1209 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1210 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1211 g_type_name (itype));
1214 return (guint*) g_array_free (result, FALSE);
1219 * @signal_id: the signal's identifying number.
1221 * Given the signal's identifier, finds its name.
1223 * Two different signals may have the same name, if they have differing types.
1225 * Returns: the signal name, or %NULL if the signal number was invalid.
1227 G_CONST_RETURN gchar*
1228 g_signal_name (guint signal_id)
1234 node = LOOKUP_SIGNAL_NODE (signal_id);
1235 name = node ? node->name : NULL;
1238 return (char*) name;
1243 * @signal_id: The signal id of the signal to query information for.
1244 * @query: A user provided structure that is filled in with constant
1245 * values upon success.
1247 * Queries the signal system for in-depth information about a
1248 * specific signal. This function will fill in a user-provided
1249 * structure to hold signal-specific information. If an invalid
1250 * signal id is passed in, the @signal_id member of the #GSignalQuery
1251 * is 0. All members filled into the #GSignalQuery structure should
1252 * be considered constant and have to be left untouched.
1255 g_signal_query (guint signal_id,
1256 GSignalQuery *query)
1260 g_return_if_fail (query != NULL);
1263 node = LOOKUP_SIGNAL_NODE (signal_id);
1264 if (!node || node->destroyed)
1265 query->signal_id = 0;
1268 query->signal_id = node->signal_id;
1269 query->signal_name = node->name;
1270 query->itype = node->itype;
1271 query->signal_flags = node->flags;
1272 query->return_type = node->return_type;
1273 query->n_params = node->n_params;
1274 query->param_types = node->param_types;
1281 * @signal_name: the name for the signal
1282 * @itype: the type this signal pertains to. It will also pertain to
1283 * types which are derived from this type.
1284 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1285 * the default handler is to be invoked. You should at least specify
1286 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1287 * @class_offset: The offset of the function pointer in the class structure
1288 * for this type. Used to invoke a class method generically. Pass 0 to
1289 * not associate a class method slot with this signal.
1290 * @accumulator: the accumulator for this signal; may be %NULL.
1291 * @accu_data: user data for the @accumulator.
1292 * @c_marshaller: the function to translate arrays of parameter values to
1293 * signal emissions into C language callback invocations.
1294 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1295 * without a return value.
1296 * @n_params: the number of parameter types to follow.
1297 * @...: a list of types, one for each parameter.
1299 * Creates a new signal. (This is usually done in the class initializer.)
1301 * A signal name consists of segments consisting of ASCII letters and
1302 * digits, separated by either the '-' or '_' character. The first
1303 * character of a signal name must be a letter. Names which violate these
1304 * rules lead to undefined behaviour of the GSignal system.
1306 * When registering a signal and looking up a signal, either separator can
1307 * be used, but they cannot be mixed.
1309 * If 0 is used for @class_offset subclasses cannot override the class handler
1310 * in their <code>class_init</code> method by doing
1311 * <code>super_class->signal_handler = my_signal_handler</code>. Instead they
1312 * will have to use g_signal_override_class_handler().
1314 * Returns: the signal id
1317 g_signal_new (const gchar *signal_name,
1319 GSignalFlags signal_flags,
1321 GSignalAccumulator accumulator,
1323 GSignalCMarshaller c_marshaller,
1331 g_return_val_if_fail (signal_name != NULL, 0);
1333 va_start (args, n_params);
1335 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1336 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1337 accumulator, accu_data, c_marshaller,
1338 return_type, n_params, args);
1342 /* optimize NOP emissions with NULL class handlers */
1343 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1344 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1349 node = LOOKUP_SIGNAL_NODE (signal_id);
1350 node->test_class_offset = class_offset;
1358 * g_signal_new_class_handler:
1359 * @signal_name: the name for the signal
1360 * @itype: the type this signal pertains to. It will also pertain to
1361 * types which are derived from this type.
1362 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1363 * the default handler is to be invoked. You should at least specify
1364 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1365 * @class_handler: a #GCallback which acts as class implementation of
1366 * this signal. Used to invoke a class method generically. Pass %NULL to
1367 * not associate a class method with this signal.
1368 * @accumulator: the accumulator for this signal; may be %NULL.
1369 * @accu_data: user data for the @accumulator.
1370 * @c_marshaller: the function to translate arrays of parameter values to
1371 * signal emissions into C language callback invocations.
1372 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1373 * without a return value.
1374 * @n_params: the number of parameter types to follow.
1375 * @...: a list of types, one for each parameter.
1377 * Creates a new signal. (This is usually done in the class initializer.)
1379 * This is a variant of g_signal_new() that takes a C callback instead
1380 * off a class offset for the signal's class handler. This function
1381 * doesn't need a function pointer exposed in the class structure of
1382 * an object definition, instead the function pointer is passed
1383 * directly and can be overriden by derived classes with
1384 * g_signal_override_class_closure() or
1385 * g_signal_override_class_handler()and chained to with
1386 * g_signal_chain_from_overridden() or
1387 * g_signal_chain_from_overridden_handler().
1389 * See g_signal_new() for information about signal names.
1391 * Returns: the signal id
1396 g_signal_new_class_handler (const gchar *signal_name,
1398 GSignalFlags signal_flags,
1399 GCallback class_handler,
1400 GSignalAccumulator accumulator,
1402 GSignalCMarshaller c_marshaller,
1410 g_return_val_if_fail (signal_name != NULL, 0);
1412 va_start (args, n_params);
1414 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1415 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1416 accumulator, accu_data, c_marshaller,
1417 return_type, n_params, args);
1424 static inline ClassClosure*
1425 signal_find_class_closure (SignalNode *node,
1428 GBSearchArray *bsa = node->class_closure_bsa;
1435 /* cc->instance_type is 0 for default closure */
1437 key.instance_type = itype;
1438 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1439 while (!cc && key.instance_type)
1441 key.instance_type = g_type_parent (key.instance_type);
1442 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1450 static inline GClosure*
1451 signal_lookup_closure (SignalNode *node,
1452 GTypeInstance *instance)
1456 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1458 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1459 if (cc && cc->instance_type == 0) /* check for default closure */
1462 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1463 return cc ? cc->closure : NULL;
1467 signal_add_class_closure (SignalNode *node,
1473 /* can't optimize NOP emissions with overridden class closures */
1474 node->test_class_offset = 0;
1476 if (!node->class_closure_bsa)
1477 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1478 key.instance_type = itype;
1479 key.closure = g_closure_ref (closure);
1480 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1481 &g_class_closure_bconfig,
1483 g_closure_sink (closure);
1484 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1485 g_closure_set_marshal (closure, node->c_marshaller);
1490 * @signal_name: the name for the signal
1491 * @itype: the type this signal pertains to. It will also pertain to
1492 * types which are derived from this type
1493 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1494 * the default handler is to be invoked. You should at least specify
1495 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1496 * @class_closure: The closure to invoke on signal emission; may be %NULL
1497 * @accumulator: the accumulator for this signal; may be %NULL
1498 * @accu_data: user data for the @accumulator
1499 * @c_marshaller: the function to translate arrays of parameter values to
1500 * signal emissions into C language callback invocations
1501 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1502 * without a return value
1503 * @n_params: the length of @param_types
1504 * @param_types: an array of types, one for each parameter
1506 * Creates a new signal. (This is usually done in the class initializer.)
1508 * See g_signal_new() for details on allowed signal names.
1510 * Returns: the signal id
1513 g_signal_newv (const gchar *signal_name,
1515 GSignalFlags signal_flags,
1516 GClosure *class_closure,
1517 GSignalAccumulator accumulator,
1519 GSignalCMarshaller c_marshaller,
1528 g_return_val_if_fail (signal_name != NULL, 0);
1529 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1531 g_return_val_if_fail (param_types != NULL, 0);
1532 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1533 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1534 g_return_val_if_fail (accumulator == NULL, 0);
1536 g_return_val_if_fail (accu_data == NULL, 0);
1538 name = g_strdup (signal_name);
1539 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1543 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1544 node = LOOKUP_SIGNAL_NODE (signal_id);
1545 if (node && !node->destroyed)
1547 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1549 type_debug_name (node->itype),
1550 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1555 if (node && node->itype != itype)
1557 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1559 type_debug_name (itype),
1560 type_debug_name (node->itype));
1565 for (i = 0; i < n_params; i++)
1566 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1568 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1569 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1574 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1576 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1577 type_debug_name (return_type), type_debug_name (itype), name);
1582 if (return_type != G_TYPE_NONE &&
1583 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1585 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1586 type_debug_name (itype), name, type_debug_name (return_type));
1592 /* setup permanent portion of signal node */
1597 signal_id = g_n_signal_nodes++;
1598 node = g_new (SignalNode, 1);
1599 node->signal_id = signal_id;
1600 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1601 g_signal_nodes[signal_id] = node;
1602 node->itype = itype;
1605 key.quark = g_quark_from_string (node->name);
1606 key.signal_id = signal_id;
1607 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1608 g_strdelimit (name, "_", '-');
1609 node->name = g_intern_string (name);
1610 key.quark = g_quark_from_string (name);
1611 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1613 TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1615 node->destroyed = FALSE;
1616 node->test_class_offset = 0;
1618 /* setup reinitializable portion */
1619 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1620 node->n_params = n_params;
1621 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1622 node->return_type = return_type;
1623 node->class_closure_bsa = NULL;
1626 node->accumulator = g_new (SignalAccumulator, 1);
1627 node->accumulator->func = accumulator;
1628 node->accumulator->data = accu_data;
1631 node->accumulator = NULL;
1632 node->c_marshaller = c_marshaller;
1633 node->emission_hooks = NULL;
1635 signal_add_class_closure (node, 0, class_closure);
1636 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1638 /* optimize NOP emissions */
1639 node->test_class_offset = TEST_CLASS_MAGIC;
1649 * g_signal_new_valist:
1650 * @signal_name: the name for the signal
1651 * @itype: the type this signal pertains to. It will also pertain to
1652 * types which are derived from this type.
1653 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1654 * the default handler is to be invoked. You should at least specify
1655 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1656 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1657 * @accumulator: the accumulator for this signal; may be %NULL.
1658 * @accu_data: user data for the @accumulator.
1659 * @c_marshaller: the function to translate arrays of parameter values to
1660 * signal emissions into C language callback invocations.
1661 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1662 * without a return value.
1663 * @n_params: the number of parameter types in @args.
1664 * @args: va_list of #GType, one for each parameter.
1666 * Creates a new signal. (This is usually done in the class initializer.)
1668 * See g_signal_new() for details on allowed signal names.
1670 * Returns: the signal id
1673 g_signal_new_valist (const gchar *signal_name,
1675 GSignalFlags signal_flags,
1676 GClosure *class_closure,
1677 GSignalAccumulator accumulator,
1679 GSignalCMarshaller c_marshaller,
1690 param_types = g_new (GType, n_params);
1692 for (i = 0; i < n_params; i++)
1693 param_types[i] = va_arg (args, GType);
1698 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1699 class_closure, accumulator, accu_data, c_marshaller,
1700 return_type, n_params, param_types);
1701 g_free (param_types);
1707 signal_destroy_R (SignalNode *signal_node)
1709 SignalNode node = *signal_node;
1711 signal_node->destroyed = TRUE;
1713 /* reentrancy caution, zero out real contents first */
1714 signal_node->test_class_offset = 0;
1715 signal_node->n_params = 0;
1716 signal_node->param_types = NULL;
1717 signal_node->return_type = 0;
1718 signal_node->class_closure_bsa = NULL;
1719 signal_node->accumulator = NULL;
1720 signal_node->c_marshaller = NULL;
1721 signal_node->emission_hooks = NULL;
1723 #ifdef G_ENABLE_DEBUG
1724 /* check current emissions */
1728 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1729 emission; emission = emission->next)
1730 if (emission->ihint.signal_id == node.signal_id)
1731 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1732 node.name, emission->instance);
1736 /* free contents that need to
1739 g_free (node.param_types);
1740 if (node.class_closure_bsa)
1744 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1746 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1748 g_closure_unref (cc->closure);
1750 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1752 g_free (node.accumulator);
1753 if (node.emission_hooks)
1755 g_hook_list_clear (node.emission_hooks);
1756 g_free (node.emission_hooks);
1762 * g_signal_override_class_closure:
1763 * @signal_id: the signal id
1764 * @instance_type: the instance type on which to override the class closure
1766 * @class_closure: the closure.
1768 * Overrides the class closure (i.e. the default handler) for the given signal
1769 * for emissions on instances of @instance_type. @instance_type must be derived
1770 * from the type to which the signal belongs.
1772 * See g_signal_chain_from_overridden() and
1773 * g_signal_chain_from_overridden_handler() for how to chain up to the
1774 * parent class closure from inside the overridden one.
1777 g_signal_override_class_closure (guint signal_id,
1778 GType instance_type,
1779 GClosure *class_closure)
1783 g_return_if_fail (signal_id > 0);
1784 g_return_if_fail (class_closure != NULL);
1787 node = LOOKUP_SIGNAL_NODE (signal_id);
1788 if (!g_type_is_a (instance_type, node->itype))
1789 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1792 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1794 if (cc && cc->instance_type == instance_type)
1795 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1797 signal_add_class_closure (node, instance_type, class_closure);
1803 * g_signal_override_class_handler:
1804 * @signal_name: the name for the signal
1805 * @instance_type: the instance type on which to override the class handler
1807 * @class_handler: the handler.
1809 * Overrides the class closure (i.e. the default handler) for the
1810 * given signal for emissions on instances of @instance_type with
1811 * callabck @class_handler. @instance_type must be derived from the
1812 * type to which the signal belongs.
1814 * See g_signal_chain_from_overridden() and
1815 * g_signal_chain_from_overridden_handler() for how to chain up to the
1816 * parent class closure from inside the overridden one.
1821 g_signal_override_class_handler (const gchar *signal_name,
1822 GType instance_type,
1823 GCallback class_handler)
1827 g_return_if_fail (signal_name != NULL);
1828 g_return_if_fail (instance_type != G_TYPE_NONE);
1829 g_return_if_fail (class_handler != NULL);
1831 signal_id = g_signal_lookup (signal_name, instance_type);
1834 g_signal_override_class_closure (signal_id, instance_type,
1835 g_cclosure_new (class_handler, NULL, NULL));
1837 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1838 G_STRLOC, signal_name, instance_type);
1843 * g_signal_chain_from_overridden:
1844 * @instance_and_params: the argument list of the signal emission. The first
1845 * element in the array is a #GValue for the instance the signal is being
1846 * emitted on. The rest are any arguments to be passed to the signal.
1847 * @return_value: Location for the return value.
1849 * Calls the original class closure of a signal. This function should only
1850 * be called from an overridden class closure; see
1851 * g_signal_override_class_closure() and
1852 * g_signal_override_class_handler().
1855 g_signal_chain_from_overridden (const GValue *instance_and_params,
1856 GValue *return_value)
1858 GType chain_type = 0, restore_type = 0;
1859 Emission *emission = NULL;
1860 GClosure *closure = NULL;
1864 g_return_if_fail (instance_and_params != NULL);
1865 instance = g_value_peek_pointer (instance_and_params);
1866 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1869 emission = emission_find_innermost (instance);
1872 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1874 g_assert (node != NULL); /* paranoid */
1876 /* we should probably do the same parameter checks as g_signal_emit() here.
1878 if (emission->chain_type != G_TYPE_NONE)
1880 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1882 g_assert (cc != NULL); /* closure currently in call stack */
1884 n_params = node->n_params;
1885 restore_type = cc->instance_type;
1886 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1887 if (cc && cc->instance_type != restore_type)
1889 closure = cc->closure;
1890 chain_type = cc->instance_type;
1894 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1897 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1901 emission->chain_type = chain_type;
1903 g_closure_invoke (closure,
1906 instance_and_params,
1909 emission->chain_type = restore_type;
1915 * g_signal_chain_from_overridden_handler:
1916 * @instance: the instance the signal is being emitted on.
1917 * @...: parameters to be passed to the parent class closure, followed by a
1918 * location for the return value. If the return type of the signal
1919 * is #G_TYPE_NONE, the return value location can be omitted.
1921 * Calls the original class closure of a signal. This function should
1922 * only be called from an overridden class closure; see
1923 * g_signal_override_class_closure() and
1924 * g_signal_override_class_handler().
1929 g_signal_chain_from_overridden_handler (gpointer instance,
1932 GType chain_type = 0, restore_type = 0;
1933 Emission *emission = NULL;
1934 GClosure *closure = NULL;
1938 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1941 emission = emission_find_innermost (instance);
1944 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1946 g_assert (node != NULL); /* paranoid */
1948 /* we should probably do the same parameter checks as g_signal_emit() here.
1950 if (emission->chain_type != G_TYPE_NONE)
1952 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1954 g_assert (cc != NULL); /* closure currently in call stack */
1956 n_params = node->n_params;
1957 restore_type = cc->instance_type;
1958 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1959 if (cc && cc->instance_type != restore_type)
1961 closure = cc->closure;
1962 chain_type = cc->instance_type;
1966 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1969 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1973 GValue *instance_and_params;
1974 GType signal_return_type;
1975 GValue *param_values;
1979 va_start (var_args, instance);
1981 signal_return_type = node->return_type;
1982 instance_and_params = g_slice_alloc0 (sizeof (GValue) * (n_params + 1));
1983 param_values = instance_and_params + 1;
1985 for (i = 0; i < node->n_params; i++)
1988 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1989 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1992 G_VALUE_COLLECT_INIT (param_values + i, ptype,
1994 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1998 g_warning ("%s: %s", G_STRLOC, error);
2001 /* we purposely leak the value here, it might not be
2002 * in a sane state if an error condition occoured
2005 g_value_unset (param_values + i);
2007 g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params);
2015 instance_and_params->g_type = 0;
2016 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2017 g_value_set_instance (instance_and_params, instance);
2020 emission->chain_type = chain_type;
2023 if (signal_return_type == G_TYPE_NONE)
2025 g_closure_invoke (closure,
2028 instance_and_params,
2033 GValue return_value = { 0, };
2034 gchar *error = NULL;
2035 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2036 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2038 g_value_init (&return_value, rtype);
2040 g_closure_invoke (closure,
2043 instance_and_params,
2046 G_VALUE_LCOPY (&return_value,
2048 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2052 g_value_unset (&return_value);
2056 g_warning ("%s: %s", G_STRLOC, error);
2059 /* we purposely leak the value here, it might not be
2060 * in a sane state if an error condition occured
2065 for (i = 0; i < n_params; i++)
2066 g_value_unset (param_values + i);
2067 g_value_unset (instance_and_params);
2068 g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params);
2073 emission->chain_type = restore_type;
2079 * g_signal_get_invocation_hint:
2080 * @instance: the instance to query
2082 * Returns the invocation hint of the innermost signal emission of instance.
2084 * Returns: the invocation hint of the innermost signal emission.
2086 GSignalInvocationHint*
2087 g_signal_get_invocation_hint (gpointer instance)
2089 Emission *emission = NULL;
2091 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2094 emission = emission_find_innermost (instance);
2097 return emission ? &emission->ihint : NULL;
2101 * g_signal_connect_closure_by_id:
2102 * @instance: the instance to connect to.
2103 * @signal_id: the id of the signal.
2104 * @detail: the detail.
2105 * @closure: the closure to connect.
2106 * @after: whether the handler should be called before or after the
2107 * default handler of the signal.
2109 * Connects a closure to a signal for a particular object.
2111 * Returns: the handler id
2114 g_signal_connect_closure_by_id (gpointer instance,
2121 gulong handler_seq_no = 0;
2123 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2124 g_return_val_if_fail (signal_id > 0, 0);
2125 g_return_val_if_fail (closure != NULL, 0);
2128 node = LOOKUP_SIGNAL_NODE (signal_id);
2131 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2132 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2133 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2134 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2137 Handler *handler = handler_new (after);
2139 handler_seq_no = handler->sequential_number;
2140 handler->detail = detail;
2141 handler->closure = g_closure_ref (closure);
2142 g_closure_sink (closure);
2143 handler_insert (signal_id, instance, handler);
2144 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2145 g_closure_set_marshal (closure, node->c_marshaller);
2149 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2152 return handler_seq_no;
2156 * g_signal_connect_closure:
2157 * @instance: the instance to connect to.
2158 * @detailed_signal: a string of the form "signal-name::detail".
2159 * @closure: the closure to connect.
2160 * @after: whether the handler should be called before or after the
2161 * default handler of the signal.
2163 * Connects a closure to a signal for a particular object.
2165 * Returns: the handler id
2168 g_signal_connect_closure (gpointer instance,
2169 const gchar *detailed_signal,
2174 gulong handler_seq_no = 0;
2178 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2179 g_return_val_if_fail (detailed_signal != NULL, 0);
2180 g_return_val_if_fail (closure != NULL, 0);
2183 itype = G_TYPE_FROM_INSTANCE (instance);
2184 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2187 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2189 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2190 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2191 else if (!g_type_is_a (itype, node->itype))
2192 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2195 Handler *handler = handler_new (after);
2197 handler_seq_no = handler->sequential_number;
2198 handler->detail = detail;
2199 handler->closure = g_closure_ref (closure);
2200 g_closure_sink (closure);
2201 handler_insert (signal_id, instance, handler);
2202 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2203 g_closure_set_marshal (handler->closure, node->c_marshaller);
2207 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2210 return handler_seq_no;
2214 * g_signal_connect_data:
2215 * @instance: the instance to connect to.
2216 * @detailed_signal: a string of the form "signal-name::detail".
2217 * @c_handler: the #GCallback to connect.
2218 * @data: data to pass to @c_handler calls.
2219 * @destroy_data: a #GClosureNotify for @data.
2220 * @connect_flags: a combination of #GConnectFlags.
2222 * Connects a #GCallback function to a signal for a particular object. Similar
2223 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2224 * which will be called when the signal handler is disconnected and no longer
2225 * used. Specify @connect_flags if you need <literal>..._after()</literal> or
2226 * <literal>..._swapped()</literal> variants of this function.
2228 * Returns: the handler id
2231 g_signal_connect_data (gpointer instance,
2232 const gchar *detailed_signal,
2233 GCallback c_handler,
2235 GClosureNotify destroy_data,
2236 GConnectFlags connect_flags)
2239 gulong handler_seq_no = 0;
2242 gboolean swapped, after;
2244 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2245 g_return_val_if_fail (detailed_signal != NULL, 0);
2246 g_return_val_if_fail (c_handler != NULL, 0);
2248 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2249 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2252 itype = G_TYPE_FROM_INSTANCE (instance);
2253 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2256 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2258 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2259 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2260 else if (!g_type_is_a (itype, node->itype))
2261 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2264 Handler *handler = handler_new (after);
2266 handler_seq_no = handler->sequential_number;
2267 handler->detail = detail;
2268 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2269 g_closure_sink (handler->closure);
2270 handler_insert (signal_id, instance, handler);
2271 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2272 g_closure_set_marshal (handler->closure, node->c_marshaller);
2276 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2279 return handler_seq_no;
2283 * g_signal_handler_block:
2284 * @instance: The instance to block the signal handler of.
2285 * @handler_id: Handler id of the handler to be blocked.
2287 * Blocks a handler of an instance so it will not be called during any
2288 * signal emissions unless it is unblocked again. Thus "blocking" a
2289 * signal handler means to temporarily deactive it, a signal handler
2290 * has to be unblocked exactly the same amount of times it has been
2291 * blocked before to become active again.
2293 * The @handler_id has to be a valid signal handler id, connected to a
2294 * signal of @instance.
2297 g_signal_handler_block (gpointer instance,
2302 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2303 g_return_if_fail (handler_id > 0);
2306 handler = handler_lookup (instance, handler_id, NULL);
2309 #ifndef G_DISABLE_CHECKS
2310 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2311 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2313 handler->block_count += 1;
2316 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2321 * g_signal_handler_unblock:
2322 * @instance: The instance to unblock the signal handler of.
2323 * @handler_id: Handler id of the handler to be unblocked.
2325 * Undoes the effect of a previous g_signal_handler_block() call. A
2326 * blocked handler is skipped during signal emissions and will not be
2327 * invoked, unblocking it (for exactly the amount of times it has been
2328 * blocked before) reverts its "blocked" state, so the handler will be
2329 * recognized by the signal system and is called upon future or
2330 * currently ongoing signal emissions (since the order in which
2331 * handlers are called during signal emissions is deterministic,
2332 * whether the unblocked handler in question is called as part of a
2333 * currently ongoing emission depends on how far that emission has
2336 * The @handler_id has to be a valid id of a signal handler that is
2337 * connected to a signal of @instance and is currently blocked.
2340 g_signal_handler_unblock (gpointer instance,
2345 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2346 g_return_if_fail (handler_id > 0);
2349 handler = handler_lookup (instance, handler_id, NULL);
2352 if (handler->block_count)
2353 handler->block_count -= 1;
2355 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2358 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2363 * g_signal_handler_disconnect:
2364 * @instance: The instance to remove the signal handler from.
2365 * @handler_id: Handler id of the handler to be disconnected.
2367 * Disconnects a handler from an instance so it will not be called during
2368 * any future or currently ongoing emissions of the signal it has been
2369 * connected to. The @handler_id becomes invalid and may be reused.
2371 * The @handler_id has to be a valid signal handler id, connected to a
2372 * signal of @instance.
2375 g_signal_handler_disconnect (gpointer instance,
2381 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2382 g_return_if_fail (handler_id > 0);
2385 handler = handler_lookup (instance, handler_id, &signal_id);
2388 handler->sequential_number = 0;
2389 handler->block_count = 1;
2390 handler_unref_R (signal_id, instance, handler);
2393 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2398 * g_signal_handler_is_connected:
2399 * @instance: The instance where a signal handler is sought.
2400 * @handler_id: the handler id.
2402 * Returns whether @handler_id is the id of a handler connected to @instance.
2404 * Returns: whether @handler_id identifies a handler connected to @instance.
2407 g_signal_handler_is_connected (gpointer instance,
2413 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2416 handler = handler_lookup (instance, handler_id, NULL);
2417 connected = handler != NULL;
2424 g_signal_handlers_destroy (gpointer instance)
2426 GBSearchArray *hlbsa;
2428 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2431 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2436 /* reentrancy caution, delete instance trace first */
2437 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2439 for (i = 0; i < hlbsa->n_nodes; i++)
2441 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2442 Handler *handler = hlist->handlers;
2446 Handler *tmp = handler;
2448 handler = tmp->next;
2449 tmp->block_count = 1;
2450 /* cruel unlink, this works because _all_ handlers vanish */
2453 if (tmp->sequential_number)
2455 tmp->sequential_number = 0;
2456 handler_unref_R (0, NULL, tmp);
2460 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2466 * g_signal_handler_find:
2467 * @instance: The instance owning the signal handler to be found.
2468 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2469 * and/or @data the handler has to match.
2470 * @signal_id: Signal the handler has to be connected to.
2471 * @detail: Signal detail the handler has to be connected to.
2472 * @closure: The closure the handler will invoke.
2473 * @func: The C closure callback of the handler (useless for non-C closures).
2474 * @data: The closure data of the handler's closure.
2476 * Finds the first signal handler that matches certain selection criteria.
2477 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2478 * flags, and the criteria values are passed as arguments.
2479 * The match @mask has to be non-0 for successful matches.
2480 * If no handler was found, 0 is returned.
2482 * Returns: A valid non-0 signal handler id for a successful match.
2485 g_signal_handler_find (gpointer instance,
2486 GSignalMatchType mask,
2493 gulong handler_seq_no = 0;
2495 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2496 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2498 if (mask & G_SIGNAL_MATCH_MASK)
2500 HandlerMatch *mlist;
2503 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2506 handler_seq_no = mlist->handler->sequential_number;
2507 handler_match_free1_R (mlist, instance);
2512 return handler_seq_no;
2516 signal_handlers_foreach_matched_R (gpointer instance,
2517 GSignalMatchType mask,
2523 void (*callback) (gpointer instance,
2524 gulong handler_seq_no))
2526 HandlerMatch *mlist;
2527 guint n_handlers = 0;
2529 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2533 if (mlist->handler->sequential_number)
2536 callback (instance, mlist->handler->sequential_number);
2539 mlist = handler_match_free1_R (mlist, instance);
2546 * g_signal_handlers_block_matched:
2547 * @instance: The instance to block handlers from.
2548 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2549 * and/or @data the handlers have to match.
2550 * @signal_id: Signal the handlers have to be connected to.
2551 * @detail: Signal detail the handlers have to be connected to.
2552 * @closure: The closure the handlers will invoke.
2553 * @func: The C closure callback of the handlers (useless for non-C closures).
2554 * @data: The closure data of the handlers' closures.
2556 * Blocks all handlers on an instance that match a certain selection criteria.
2557 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2558 * flags, and the criteria values are passed as arguments.
2559 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2560 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2561 * If no handlers were found, 0 is returned, the number of blocked handlers
2564 * Returns: The number of handlers that matched.
2567 g_signal_handlers_block_matched (gpointer instance,
2568 GSignalMatchType mask,
2575 guint n_handlers = 0;
2577 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2578 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2580 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2583 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2584 closure, func, data,
2585 g_signal_handler_block);
2593 * g_signal_handlers_unblock_matched:
2594 * @instance: The instance to unblock handlers from.
2595 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2596 * and/or @data the handlers have to match.
2597 * @signal_id: Signal the handlers have to be connected to.
2598 * @detail: Signal detail the handlers have to be connected to.
2599 * @closure: The closure the handlers will invoke.
2600 * @func: The C closure callback of the handlers (useless for non-C closures).
2601 * @data: The closure data of the handlers' closures.
2603 * Unblocks all handlers on an instance that match a certain selection
2604 * criteria. The criteria mask is passed as an OR-ed combination of
2605 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2606 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2607 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2608 * If no handlers were found, 0 is returned, the number of unblocked handlers
2609 * otherwise. The match criteria should not apply to any handlers that are
2610 * not currently blocked.
2612 * Returns: The number of handlers that matched.
2615 g_signal_handlers_unblock_matched (gpointer instance,
2616 GSignalMatchType mask,
2623 guint n_handlers = 0;
2625 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2626 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2628 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2631 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2632 closure, func, data,
2633 g_signal_handler_unblock);
2641 * g_signal_handlers_disconnect_matched:
2642 * @instance: The instance to remove handlers from.
2643 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2644 * and/or @data the handlers have to match.
2645 * @signal_id: Signal the handlers have to be connected to.
2646 * @detail: Signal detail the handlers have to be connected to.
2647 * @closure: The closure the handlers will invoke.
2648 * @func: The C closure callback of the handlers (useless for non-C closures).
2649 * @data: The closure data of the handlers' closures.
2651 * Disconnects all handlers on an instance that match a certain
2652 * selection criteria. The criteria mask is passed as an OR-ed
2653 * combination of #GSignalMatchType flags, and the criteria values are
2654 * passed as arguments. Passing at least one of the
2655 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2656 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2657 * matches. If no handlers were found, 0 is returned, the number of
2658 * disconnected handlers otherwise.
2660 * Returns: The number of handlers that matched.
2663 g_signal_handlers_disconnect_matched (gpointer instance,
2664 GSignalMatchType mask,
2671 guint n_handlers = 0;
2673 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2674 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2676 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2679 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2680 closure, func, data,
2681 g_signal_handler_disconnect);
2689 * g_signal_has_handler_pending:
2690 * @instance: the object whose signal handlers are sought.
2691 * @signal_id: the signal id.
2692 * @detail: the detail.
2693 * @may_be_blocked: whether blocked handlers should count as match.
2695 * Returns whether there are any handlers connected to @instance for the
2696 * given signal id and detail.
2698 * One example of when you might use this is when the arguments to the
2699 * signal are difficult to compute. A class implementor may opt to not
2700 * emit the signal if no one is attached anyway, thus saving the cost
2701 * of building the arguments.
2703 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2707 g_signal_has_handler_pending (gpointer instance,
2710 gboolean may_be_blocked)
2712 HandlerMatch *mlist;
2713 gboolean has_pending;
2715 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2716 g_return_val_if_fail (signal_id > 0, FALSE);
2721 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2723 if (!(node->flags & G_SIGNAL_DETAILED))
2725 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2730 mlist = handlers_find (instance,
2731 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2732 signal_id, detail, NULL, NULL, NULL, TRUE);
2736 handler_match_free1_R (mlist, instance);
2739 has_pending = FALSE;
2745 static inline gboolean
2746 signal_check_skip_emission (SignalNode *node,
2752 /* are we able to check for NULL class handlers? */
2753 if (!node->test_class_offset)
2756 /* are there emission hooks pending? */
2757 if (node->emission_hooks && node->emission_hooks->hooks)
2760 /* is there a non-NULL class handler? */
2761 if (node->test_class_offset != TEST_CLASS_MAGIC)
2763 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2765 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2769 /* are signals being debugged? */
2770 #ifdef G_ENABLE_DEBUG
2771 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2773 #endif /* G_ENABLE_DEBUG */
2775 /* is this a no-recurse signal already in emission? */
2776 if (node->flags & G_SIGNAL_NO_RECURSE &&
2777 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2780 /* do we have pending handlers? */
2781 hlist = handler_list_lookup (node->signal_id, instance);
2782 if (hlist && hlist->handlers)
2785 /* none of the above, no emission required */
2791 * @instance_and_params: argument list for the signal emission. The first
2792 * element in the array is a #GValue for the instance the signal is
2793 * being emitted on. The rest are any arguments to be passed to the
2795 * @signal_id: the signal id
2796 * @detail: the detail
2797 * @return_value: Location to store the return value of the signal emission.
2801 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2802 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2805 g_signal_emitv (const GValue *instance_and_params,
2808 GValue *return_value)
2812 #ifdef G_ENABLE_DEBUG
2813 const GValue *param_values;
2817 g_return_if_fail (instance_and_params != NULL);
2818 instance = g_value_peek_pointer (instance_and_params);
2819 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2820 g_return_if_fail (signal_id > 0);
2822 #ifdef G_ENABLE_DEBUG
2823 param_values = instance_and_params + 1;
2827 node = LOOKUP_SIGNAL_NODE (signal_id);
2828 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2830 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2834 #ifdef G_ENABLE_DEBUG
2835 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2837 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2841 for (i = 0; i < node->n_params; i++)
2842 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2844 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2846 type_debug_name (node->param_types[i]),
2849 G_VALUE_TYPE_NAME (param_values + i));
2853 if (node->return_type != G_TYPE_NONE)
2857 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2859 type_debug_name (node->return_type),
2864 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2866 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2868 type_debug_name (node->return_type),
2870 G_VALUE_TYPE_NAME (return_value));
2876 return_value = NULL;
2877 #endif /* G_ENABLE_DEBUG */
2879 /* optimize NOP emissions */
2880 if (signal_check_skip_emission (node, instance, detail))
2882 /* nothing to do to emit this signal */
2884 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2889 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2893 * g_signal_emit_valist:
2894 * @instance: the instance the signal is being emitted on.
2895 * @signal_id: the signal id
2896 * @detail: the detail
2897 * @var_args: a list of parameters to be passed to the signal, followed by a
2898 * location for the return value. If the return type of the signal
2899 * is #G_TYPE_NONE, the return value location can be omitted.
2903 * Note that g_signal_emit_valist() resets the return value to the default
2904 * if no handlers are connected, in contrast to g_signal_emitv().
2907 g_signal_emit_valist (gpointer instance,
2912 GValue *instance_and_params;
2913 GType signal_return_type;
2914 GValue *param_values;
2918 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2919 g_return_if_fail (signal_id > 0);
2922 node = LOOKUP_SIGNAL_NODE (signal_id);
2923 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2925 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2929 #ifndef G_DISABLE_CHECKS
2930 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2932 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2936 #endif /* !G_DISABLE_CHECKS */
2938 /* optimize NOP emissions */
2939 if (signal_check_skip_emission (node, instance, detail))
2941 /* nothing to do to emit this signal */
2943 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2947 n_params = node->n_params;
2948 signal_return_type = node->return_type;
2949 instance_and_params = g_slice_alloc0 (sizeof (GValue) * (n_params + 1));
2950 param_values = instance_and_params + 1;
2952 for (i = 0; i < node->n_params; i++)
2955 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2956 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2959 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2961 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2965 g_warning ("%s: %s", G_STRLOC, error);
2968 /* we purposely leak the value here, it might not be
2969 * in a sane state if an error condition occoured
2972 g_value_unset (param_values + i);
2974 g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params);
2980 instance_and_params->g_type = 0;
2981 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2982 g_value_set_instance (instance_and_params, instance);
2983 if (signal_return_type == G_TYPE_NONE)
2984 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2987 GValue return_value = { 0, };
2988 gchar *error = NULL;
2989 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2990 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2992 g_value_init (&return_value, rtype);
2994 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2996 G_VALUE_LCOPY (&return_value,
2998 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3001 g_value_unset (&return_value);
3004 g_warning ("%s: %s", G_STRLOC, error);
3007 /* we purposely leak the value here, it might not be
3008 * in a sane state if an error condition occured
3012 for (i = 0; i < n_params; i++)
3013 g_value_unset (param_values + i);
3014 g_value_unset (instance_and_params);
3015 g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params);
3020 * @instance: the instance the signal is being emitted on.
3021 * @signal_id: the signal id
3022 * @detail: the detail
3023 * @...: parameters to be passed to the signal, followed by a
3024 * location for the return value. If the return type of the signal
3025 * is #G_TYPE_NONE, the return value location can be omitted.
3029 * Note that g_signal_emit() resets the return value to the default
3030 * if no handlers are connected, in contrast to g_signal_emitv().
3033 g_signal_emit (gpointer instance,
3040 va_start (var_args, detail);
3041 g_signal_emit_valist (instance, signal_id, detail, var_args);
3046 * g_signal_emit_by_name:
3047 * @instance: the instance the signal is being emitted on.
3048 * @detailed_signal: a string of the form "signal-name::detail".
3049 * @...: parameters to be passed to the signal, followed by a
3050 * location for the return value. If the return type of the signal
3051 * is #G_TYPE_NONE, the return value location can be omitted.
3055 * Note that g_signal_emit_by_name() resets the return value to the default
3056 * if no handlers are connected, in contrast to g_signal_emitv().
3059 g_signal_emit_by_name (gpointer instance,
3060 const gchar *detailed_signal,
3066 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3067 g_return_if_fail (detailed_signal != NULL);
3070 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
3077 va_start (var_args, detailed_signal);
3078 g_signal_emit_valist (instance, signal_id, detail, var_args);
3082 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
3085 static inline gboolean
3086 accumulate (GSignalInvocationHint *ihint,
3087 GValue *return_accu,
3088 GValue *handler_return,
3089 SignalAccumulator *accumulator)
3091 gboolean continue_emission;
3096 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3097 g_value_reset (handler_return);
3099 return continue_emission;
3103 signal_emit_unlocked_R (SignalNode *node,
3106 GValue *emission_return,
3107 const GValue *instance_and_params)
3109 SignalAccumulator *accumulator;
3111 GClosure *class_closure;
3113 Handler *handler_list = NULL;
3114 GValue *return_accu, accu = { 0, };
3116 gulong max_sequential_handler_number;
3117 gboolean return_value_altered = FALSE;
3119 #ifdef G_ENABLE_DEBUG
3120 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
3122 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
3123 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
3126 if (g_trap_instance_signals == instance)
3129 #endif /* G_ENABLE_DEBUG */
3131 TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3134 signal_id = node->signal_id;
3135 if (node->flags & G_SIGNAL_NO_RECURSE)
3137 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3141 node->state = EMISSION_RESTART;
3143 return return_value_altered;
3146 accumulator = node->accumulator;
3150 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3151 return_accu = &accu;
3155 return_accu = emission_return;
3156 emission.instance = instance;
3157 emission.ihint.signal_id = node->signal_id;
3158 emission.ihint.detail = detail;
3159 emission.ihint.run_type = 0;
3161 emission.chain_type = G_TYPE_NONE;
3162 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3163 class_closure = signal_lookup_closure (node, instance);
3168 handler_unref_R (signal_id, instance, handler_list);
3169 max_sequential_handler_number = g_handler_sequential_number;
3170 hlist = handler_list_lookup (signal_id, instance);
3171 handler_list = hlist ? hlist->handlers : NULL;
3173 handler_ref (handler_list);
3175 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3177 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3179 emission.state = EMISSION_RUN;
3181 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3183 g_closure_invoke (class_closure,
3186 instance_and_params,
3188 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3189 emission.state == EMISSION_RUN)
3190 emission.state = EMISSION_STOP;
3192 emission.chain_type = G_TYPE_NONE;
3193 return_value_altered = TRUE;
3195 if (emission.state == EMISSION_STOP)
3197 else if (emission.state == EMISSION_RESTART)
3201 if (node->emission_hooks)
3203 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3206 emission.state = EMISSION_HOOK;
3207 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3210 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3212 if (!signal_hook->detail || signal_hook->detail == detail)
3214 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3216 was_in_call = G_HOOK_IN_CALL (hook);
3217 hook->flags |= G_HOOK_FLAG_IN_CALL;
3219 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3222 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3224 g_hook_destroy_link (node->emission_hooks, hook);
3226 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3229 if (emission.state == EMISSION_RESTART)
3235 Handler *handler = handler_list;
3237 emission.state = EMISSION_RUN;
3238 handler_ref (handler);
3245 handler_unref_R (signal_id, instance, handler_list);
3246 handler_list = handler;
3249 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3250 handler->sequential_number < max_sequential_handler_number)
3253 g_closure_invoke (handler->closure,
3256 instance_and_params,
3258 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3259 emission.state == EMISSION_RUN)
3260 emission.state = EMISSION_STOP;
3262 return_value_altered = TRUE;
3264 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3267 tmp = handler->next;
3271 handler_unref_R (signal_id, instance, handler_list);
3272 handler_list = handler;
3277 if (emission.state == EMISSION_STOP)
3279 else if (emission.state == EMISSION_RESTART)
3283 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3285 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3287 emission.state = EMISSION_RUN;
3289 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3291 g_closure_invoke (class_closure,
3294 instance_and_params,
3296 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3297 emission.state == EMISSION_RUN)
3298 emission.state = EMISSION_STOP;
3300 emission.chain_type = G_TYPE_NONE;
3301 return_value_altered = TRUE;
3303 if (emission.state == EMISSION_STOP)
3305 else if (emission.state == EMISSION_RESTART)
3311 Handler *handler = handler_list;
3313 emission.state = EMISSION_RUN;
3314 handler_ref (handler);
3319 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3320 handler->sequential_number < max_sequential_handler_number)
3323 g_closure_invoke (handler->closure,
3326 instance_and_params,
3328 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3329 emission.state == EMISSION_RUN)
3330 emission.state = EMISSION_STOP;
3332 return_value_altered = TRUE;
3334 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3337 tmp = handler->next;
3341 handler_unref_R (signal_id, instance, handler);
3346 if (emission.state == EMISSION_STOP)
3348 else if (emission.state == EMISSION_RESTART)
3354 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3356 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3358 gboolean need_unset = FALSE;
3360 emission.state = EMISSION_STOP;
3362 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3364 if (node->return_type != G_TYPE_NONE && !accumulator)
3366 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3369 g_closure_invoke (class_closure,
3370 node->return_type != G_TYPE_NONE ? &accu : NULL,
3372 instance_and_params,
3375 g_value_unset (&accu);
3377 emission.chain_type = G_TYPE_NONE;
3379 if (emission.state == EMISSION_RESTART)
3384 handler_unref_R (signal_id, instance, handler_list);
3386 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3389 g_value_unset (&accu);
3391 TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3393 return return_value_altered;
3397 type_debug_name (GType type)
3401 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3402 return name ? name : "<unknown>";
3409 * g_signal_accumulator_true_handled:
3410 * @ihint: standard #GSignalAccumulator parameter
3411 * @return_accu: standard #GSignalAccumulator parameter
3412 * @handler_return: standard #GSignalAccumulator parameter
3413 * @dummy: standard #GSignalAccumulator parameter
3415 * A predefined #GSignalAccumulator for signals that return a
3416 * boolean values. The behavior that this accumulator gives is
3417 * that a return of %TRUE stops the signal emission: no further
3418 * callbacks will be invoked, while a return of %FALSE allows
3419 * the emission to coninue. The idea here is that a %TRUE return
3420 * indicates that the callback <emphasis>handled</emphasis> the signal,
3421 * and no further handling is needed.
3425 * Returns: standard #GSignalAccumulator result
3428 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3429 GValue *return_accu,
3430 const GValue *handler_return,
3433 gboolean continue_emission;
3434 gboolean signal_handled;
3436 signal_handled = g_value_get_boolean (handler_return);
3437 g_value_set_boolean (return_accu, signal_handled);
3438 continue_emission = !signal_handled;
3440 return continue_emission;
3443 /* --- compile standard marshallers --- */
3444 #include "gmarshal.c"
3446 #define __G_SIGNAL_C__
3447 #include "gobjectaliasdef.c"