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.
101 * ## Memory management of signal handlers # {#signal-memory-management}
103 * If you are connecting handlers to signals and using a #GObject instance as
104 * your signal handler user data, you should remember to pair calls to
105 * g_signal_connect() with calls to g_signal_handler_disconnect() or
106 * g_signal_handlers_disconnect_by_func(). While signal handlers are
107 * automatically disconnected when the object emitting the signal is finalised,
108 * they are not automatically disconnected when the signal handler user data is
109 * destroyed. If this user data is a #GObject instance, using it from a
110 * signal handler after it has been finalised is an error.
112 * There are two strategies for managing such user data. The first is to
113 * disconnect the signal handler (using g_signal_handler_disconnect() or
114 * g_signal_handlers_disconnect_by_func()) when the user data (object) is
115 * finalised; this has to be implemented manually. For non-threaded programs,
116 * g_signal_connect_object() can be used to implement this automatically.
117 * Currently, however, it is unsafe to use in threaded programs.
119 * The second is to hold a strong reference on the user data until after the
120 * signal is disconnected for other reasons. This can be implemented
121 * automatically using g_signal_connect_data().
123 * The first approach is recommended, as the second approach can result in
124 * effective memory leaks of the user data if the signal handler is never
125 * disconnected for some reason.
129 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
131 /* --- typedefs --- */
132 typedef struct _SignalNode SignalNode
;
133 typedef struct _SignalKey SignalKey
;
134 typedef struct _Emission Emission
;
135 typedef struct _Handler Handler
;
136 typedef struct _HandlerList HandlerList
;
137 typedef struct _HandlerMatch HandlerMatch
;
147 /* --- prototypes --- */
148 static inline guint
signal_id_lookup (GQuark quark
,
150 static void signal_destroy_R (SignalNode
*signal_node
);
151 static inline HandlerList
* handler_list_ensure (guint signal_id
,
153 static inline HandlerList
* handler_list_lookup (guint signal_id
,
155 static inline Handler
* handler_new (guint signal_id
,
158 static void handler_insert (guint signal_id
,
161 static Handler
* handler_lookup (gpointer instance
,
165 static inline HandlerMatch
* handler_match_prepend (HandlerMatch
*list
,
168 static inline HandlerMatch
* handler_match_free1_R (HandlerMatch
*node
,
170 static HandlerMatch
* handlers_find (gpointer instance
,
171 GSignalMatchType mask
,
177 gboolean one_and_only
);
178 static inline void handler_ref (Handler
*handler
);
179 static inline void handler_unref_R (guint signal_id
,
182 static gint
handler_lists_cmp (gconstpointer node1
,
183 gconstpointer node2
);
184 static inline void emission_push (Emission
*emission
);
185 static inline void emission_pop (Emission
*emission
);
186 static inline Emission
* emission_find (guint signal_id
,
189 static gint
class_closures_cmp (gconstpointer node1
,
190 gconstpointer node2
);
191 static gint
signal_key_cmp (gconstpointer node1
,
192 gconstpointer node2
);
193 static gboolean
signal_emit_unlocked_R (SignalNode
*node
,
196 GValue
*return_value
,
197 const GValue
*instance_and_params
);
198 static void add_invalid_closure_notify (Handler
*handler
,
200 static void remove_invalid_closure_notify (Handler
*handler
,
202 static void invalid_closure_notify (gpointer data
,
204 static const gchar
* type_debug_name (GType type
);
205 static void node_check_deprecated (const SignalNode
*node
);
206 static void node_update_single_va_closure (SignalNode
*node
);
209 /* --- structures --- */
212 GSignalAccumulator func
;
220 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
224 /* permanent portion */
230 /* reinitializable portion */
233 guint single_va_closure_is_valid
: 1;
234 guint single_va_closure_is_after
: 1;
235 GType
*param_types
; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
236 GType return_type
; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
237 GBSearchArray
*class_closure_bsa
;
238 SignalAccumulator
*accumulator
;
239 GSignalCMarshaller c_marshaller
;
240 GSignalCVaMarshaller va_marshaller
;
241 GHookList
*emission_hooks
;
243 GClosure
*single_va_closure
;
246 #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1) /* indicates single_va_closure is valid but empty */
259 GSignalInvocationHint ihint
;
268 Handler
*tail_before
; /* normal signal handlers are appended here */
269 Handler
*tail_after
; /* CONNECT_AFTER handlers are appended here */
274 gulong sequential_number
;
280 guint block_count
: 16;
281 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
283 guint has_invalid_closure_notify
: 1;
296 GType instance_type
; /* 0 for default closure */
301 /* --- variables --- */
302 static GBSearchArray
*g_signal_key_bsa
= NULL
;
303 static const GBSearchConfig g_signal_key_bconfig
= {
306 G_BSEARCH_ARRAY_ALIGN_POWER2
,
308 static GBSearchConfig g_signal_hlbsa_bconfig
= {
309 sizeof (HandlerList
),
313 static GBSearchConfig g_class_closure_bconfig
= {
314 sizeof (ClassClosure
),
318 static GHashTable
*g_handler_list_bsa_ht
= NULL
;
319 static Emission
*g_emissions
= NULL
;
320 static gulong g_handler_sequential_number
= 1;
321 static GHashTable
*g_handlers
= NULL
;
323 G_LOCK_DEFINE_STATIC (g_signal_mutex
);
324 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
325 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
328 /* --- signal nodes --- */
329 static guint g_n_signal_nodes
= 0;
330 static SignalNode
**g_signal_nodes
= NULL
;
332 static inline SignalNode
*
333 LOOKUP_SIGNAL_NODE (guint signal_id
)
335 if (signal_id
< g_n_signal_nodes
)
336 return g_signal_nodes
[signal_id
];
342 /* --- functions --- */
344 signal_id_lookup (GQuark quark
,
347 GType
*ifaces
, type
= itype
;
353 /* try looking up signals for this type and its ancestors */
356 SignalKey
*signal_key
;
359 signal_key
= g_bsearch_array_lookup (g_signal_key_bsa
, &g_signal_key_bconfig
, &key
);
362 return signal_key
->signal_id
;
364 type
= g_type_parent (type
);
368 /* no luck, try interfaces it exports */
369 ifaces
= g_type_interfaces (itype
, &n_ifaces
);
372 SignalKey
*signal_key
;
374 key
.itype
= ifaces
[n_ifaces
];
375 signal_key
= g_bsearch_array_lookup (g_signal_key_bsa
, &g_signal_key_bconfig
, &key
);
380 return signal_key
->signal_id
;
389 class_closures_cmp (gconstpointer node1
,
392 const ClassClosure
*c1
= node1
, *c2
= node2
;
394 return G_BSEARCH_ARRAY_CMP (c1
->instance_type
, c2
->instance_type
);
398 handler_lists_cmp (gconstpointer node1
,
401 const HandlerList
*hlist1
= node1
, *hlist2
= node2
;
403 return G_BSEARCH_ARRAY_CMP (hlist1
->signal_id
, hlist2
->signal_id
);
406 static inline HandlerList
*
407 handler_list_ensure (guint signal_id
,
410 GBSearchArray
*hlbsa
= g_hash_table_lookup (g_handler_list_bsa_ht
, instance
);
413 key
.signal_id
= signal_id
;
415 key
.tail_before
= NULL
;
416 key
.tail_after
= NULL
;
419 hlbsa
= g_bsearch_array_create (&g_signal_hlbsa_bconfig
);
420 hlbsa
= g_bsearch_array_insert (hlbsa
, &g_signal_hlbsa_bconfig
, &key
);
421 g_hash_table_insert (g_handler_list_bsa_ht
, instance
, hlbsa
);
425 GBSearchArray
*o
= hlbsa
;
427 hlbsa
= g_bsearch_array_insert (o
, &g_signal_hlbsa_bconfig
, &key
);
429 g_hash_table_insert (g_handler_list_bsa_ht
, instance
, hlbsa
);
431 return g_bsearch_array_lookup (hlbsa
, &g_signal_hlbsa_bconfig
, &key
);
434 static inline HandlerList
*
435 handler_list_lookup (guint signal_id
,
438 GBSearchArray
*hlbsa
= g_hash_table_lookup (g_handler_list_bsa_ht
, instance
);
441 key
.signal_id
= signal_id
;
443 return hlbsa
? g_bsearch_array_lookup (hlbsa
, &g_signal_hlbsa_bconfig
, &key
) : NULL
;
447 handler_hash (gconstpointer key
)
449 return (guint
)((Handler
*)key
)->sequential_number
;
453 handler_equal (gconstpointer a
, gconstpointer b
)
455 Handler
*ha
= (Handler
*)a
;
456 Handler
*hb
= (Handler
*)b
;
457 return (ha
->sequential_number
== hb
->sequential_number
) &&
458 (ha
->instance
== hb
->instance
);
462 handler_lookup (gpointer instance
,
467 GBSearchArray
*hlbsa
;
472 key
.sequential_number
= handler_id
;
473 key
.instance
= instance
;
474 return g_hash_table_lookup (g_handlers
, &key
);
478 hlbsa
= g_hash_table_lookup (g_handler_list_bsa_ht
, instance
);
484 for (i
= 0; i
< hlbsa
->n_nodes
; i
++)
486 HandlerList
*hlist
= g_bsearch_array_get_nth (hlbsa
, &g_signal_hlbsa_bconfig
, i
);
489 for (handler
= hlist
->handlers
; handler
; handler
= handler
->next
)
490 if (closure
? (handler
->closure
== closure
) : (handler
->sequential_number
== handler_id
))
493 *signal_id_p
= hlist
->signal_id
;
503 static inline HandlerMatch
*
504 handler_match_prepend (HandlerMatch
*list
,
510 node
= g_slice_new (HandlerMatch
);
511 node
->handler
= handler
;
513 node
->signal_id
= signal_id
;
514 handler_ref (handler
);
518 static inline HandlerMatch
*
519 handler_match_free1_R (HandlerMatch
*node
,
522 HandlerMatch
*next
= node
->next
;
524 handler_unref_R (node
->signal_id
, instance
, node
->handler
);
525 g_slice_free (HandlerMatch
, node
);
531 handlers_find (gpointer instance
,
532 GSignalMatchType mask
,
538 gboolean one_and_only
)
540 HandlerMatch
*mlist
= NULL
;
542 if (mask
& G_SIGNAL_MATCH_ID
)
544 HandlerList
*hlist
= handler_list_lookup (signal_id
, instance
);
546 SignalNode
*node
= NULL
;
548 if (mask
& G_SIGNAL_MATCH_FUNC
)
550 node
= LOOKUP_SIGNAL_NODE (signal_id
);
551 if (!node
|| !node
->c_marshaller
)
556 for (handler
= hlist
? hlist
->handlers
: NULL
; handler
; handler
= handler
->next
)
557 if (handler
->sequential_number
&&
558 ((mask
& G_SIGNAL_MATCH_DETAIL
) || handler
->detail
== detail
) &&
559 ((mask
& G_SIGNAL_MATCH_CLOSURE
) || handler
->closure
== closure
) &&
560 ((mask
& G_SIGNAL_MATCH_DATA
) || handler
->closure
->data
== data
) &&
561 ((mask
& G_SIGNAL_MATCH_UNBLOCKED
) || handler
->block_count
== 0) &&
562 ((mask
& G_SIGNAL_MATCH_FUNC
) || (handler
->closure
->marshal
== node
->c_marshaller
&&
563 G_REAL_CLOSURE (handler
->closure
)->meta_marshal
== NULL
&&
564 ((GCClosure
*) handler
->closure
)->callback
== func
)))
566 mlist
= handler_match_prepend (mlist
, handler
, signal_id
);
573 GBSearchArray
*hlbsa
= g_hash_table_lookup (g_handler_list_bsa_ht
, instance
);
580 for (i
= 0; i
< hlbsa
->n_nodes
; i
++)
582 HandlerList
*hlist
= g_bsearch_array_get_nth (hlbsa
, &g_signal_hlbsa_bconfig
, i
);
583 SignalNode
*node
= NULL
;
586 if (!(mask
& G_SIGNAL_MATCH_FUNC
))
588 node
= LOOKUP_SIGNAL_NODE (hlist
->signal_id
);
589 if (!node
->c_marshaller
)
593 for (handler
= hlist
->handlers
; handler
; handler
= handler
->next
)
594 if (handler
->sequential_number
&&
595 ((mask
& G_SIGNAL_MATCH_DETAIL
) || handler
->detail
== detail
) &&
596 ((mask
& G_SIGNAL_MATCH_CLOSURE
) || handler
->closure
== closure
) &&
597 ((mask
& G_SIGNAL_MATCH_DATA
) || handler
->closure
->data
== data
) &&
598 ((mask
& G_SIGNAL_MATCH_UNBLOCKED
) || handler
->block_count
== 0) &&
599 ((mask
& G_SIGNAL_MATCH_FUNC
) || (handler
->closure
->marshal
== node
->c_marshaller
&&
600 G_REAL_CLOSURE (handler
->closure
)->meta_marshal
== NULL
&&
601 ((GCClosure
*) handler
->closure
)->callback
== func
)))
603 mlist
= handler_match_prepend (mlist
, handler
, hlist
->signal_id
);
614 static inline Handler
*
615 handler_new (guint signal_id
, gpointer instance
, gboolean after
)
617 Handler
*handler
= g_slice_new (Handler
);
618 #ifndef G_DISABLE_CHECKS
619 if (g_handler_sequential_number
< 1)
620 g_error (G_STRLOC
": handler id overflow, %s", REPORT_BUG
);
623 handler
->sequential_number
= g_handler_sequential_number
++;
624 handler
->prev
= NULL
;
625 handler
->next
= NULL
;
627 handler
->signal_id
= signal_id
;
628 handler
->instance
= instance
;
629 handler
->ref_count
= 1;
630 handler
->block_count
= 0;
631 handler
->after
= after
!= FALSE
;
632 handler
->closure
= NULL
;
633 handler
->has_invalid_closure_notify
= 0;
635 g_hash_table_add (g_handlers
, handler
);
641 handler_ref (Handler
*handler
)
643 g_return_if_fail (handler
->ref_count
> 0);
645 handler
->ref_count
++;
649 handler_unref_R (guint signal_id
,
653 g_return_if_fail (handler
->ref_count
> 0);
655 handler
->ref_count
--;
657 if (G_UNLIKELY (handler
->ref_count
== 0))
659 HandlerList
*hlist
= NULL
;
662 handler
->next
->prev
= handler
->prev
;
663 if (handler
->prev
) /* watch out for g_signal_handlers_destroy()! */
664 handler
->prev
->next
= handler
->next
;
667 hlist
= handler_list_lookup (signal_id
, instance
);
668 g_assert (hlist
!= NULL
);
669 hlist
->handlers
= handler
->next
;
674 /* check if we are removing the handler pointed to by tail_before */
675 if (!handler
->after
&& (!handler
->next
|| handler
->next
->after
))
678 hlist
= handler_list_lookup (signal_id
, instance
);
681 g_assert (hlist
->tail_before
== handler
); /* paranoid */
682 hlist
->tail_before
= handler
->prev
;
686 /* check if we are removing the handler pointed to by tail_after */
690 hlist
= handler_list_lookup (signal_id
, instance
);
693 g_assert (hlist
->tail_after
== handler
); /* paranoid */
694 hlist
->tail_after
= handler
->prev
;
700 g_closure_unref (handler
->closure
);
702 g_slice_free (Handler
, handler
);
707 handler_insert (guint signal_id
,
713 g_assert (handler
->prev
== NULL
&& handler
->next
== NULL
); /* paranoid */
715 hlist
= handler_list_ensure (signal_id
, instance
);
716 if (!hlist
->handlers
)
718 hlist
->handlers
= handler
;
720 hlist
->tail_before
= handler
;
722 else if (handler
->after
)
724 handler
->prev
= hlist
->tail_after
;
725 hlist
->tail_after
->next
= handler
;
729 if (hlist
->tail_before
)
731 handler
->next
= hlist
->tail_before
->next
;
733 handler
->next
->prev
= handler
;
734 handler
->prev
= hlist
->tail_before
;
735 hlist
->tail_before
->next
= handler
;
737 else /* insert !after handler into a list of only after handlers */
739 handler
->next
= hlist
->handlers
;
741 handler
->next
->prev
= handler
;
742 hlist
->handlers
= handler
;
744 hlist
->tail_before
= handler
;
748 hlist
->tail_after
= handler
;
752 node_update_single_va_closure (SignalNode
*node
)
754 GClosure
*closure
= NULL
;
755 gboolean is_after
= FALSE
;
757 /* Fast path single-handler without boxing the arguments in GValues */
758 if (G_TYPE_IS_OBJECT (node
->itype
) &&
759 (node
->flags
& (G_SIGNAL_MUST_COLLECT
)) == 0 &&
760 (node
->emission_hooks
== NULL
|| node
->emission_hooks
->hooks
== NULL
))
762 GSignalFlags run_type
;
764 GBSearchArray
*bsa
= node
->class_closure_bsa
;
766 if (bsa
== NULL
|| bsa
->n_nodes
== 0)
767 closure
= SINGLE_VA_CLOSURE_EMPTY_MAGIC
;
768 else if (bsa
->n_nodes
== 1)
770 /* Look for default class closure (can't support non-default as it
771 chains up using GValues */
772 cc
= g_bsearch_array_get_nth (bsa
, &g_class_closure_bconfig
, 0);
773 if (cc
->instance_type
== 0)
775 run_type
= node
->flags
& (G_SIGNAL_RUN_FIRST
|G_SIGNAL_RUN_LAST
|G_SIGNAL_RUN_CLEANUP
);
776 /* Only support *one* of run-first or run-last, not multiple or cleanup */
777 if (run_type
== G_SIGNAL_RUN_FIRST
||
778 run_type
== G_SIGNAL_RUN_LAST
)
780 closure
= cc
->closure
;
781 is_after
= (run_type
== G_SIGNAL_RUN_LAST
);
787 node
->single_va_closure_is_valid
= TRUE
;
788 node
->single_va_closure
= closure
;
789 node
->single_va_closure_is_after
= is_after
;
793 emission_push (Emission
*emission
)
795 emission
->next
= g_emissions
;
796 g_emissions
= emission
;
800 emission_pop (Emission
*emission
)
802 Emission
*node
, *last
= NULL
;
804 for (node
= g_emissions
; node
; last
= node
, node
= last
->next
)
805 if (node
== emission
)
808 last
->next
= node
->next
;
810 g_emissions
= node
->next
;
813 g_assert_not_reached ();
816 static inline Emission
*
817 emission_find (guint signal_id
,
823 for (emission
= g_emissions
; emission
; emission
= emission
->next
)
824 if (emission
->instance
== instance
&&
825 emission
->ihint
.signal_id
== signal_id
&&
826 emission
->ihint
.detail
== detail
)
831 static inline Emission
*
832 emission_find_innermost (gpointer instance
)
836 for (emission
= g_emissions
; emission
; emission
= emission
->next
)
837 if (emission
->instance
== instance
)
844 signal_key_cmp (gconstpointer node1
,
847 const SignalKey
*key1
= node1
, *key2
= node2
;
849 if (key1
->itype
== key2
->itype
)
850 return G_BSEARCH_ARRAY_CMP (key1
->quark
, key2
->quark
);
852 return G_BSEARCH_ARRAY_CMP (key1
->itype
, key2
->itype
);
856 _g_signal_init (void)
859 if (!g_n_signal_nodes
)
861 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
862 g_handler_list_bsa_ht
= g_hash_table_new (g_direct_hash
, NULL
);
863 g_signal_key_bsa
= g_bsearch_array_create (&g_signal_key_bconfig
);
865 /* invalid (0) signal_id */
866 g_n_signal_nodes
= 1;
867 g_signal_nodes
= g_renew (SignalNode
*, g_signal_nodes
, g_n_signal_nodes
);
868 g_signal_nodes
[0] = NULL
;
869 g_handlers
= g_hash_table_new (handler_hash
, handler_equal
);
875 _g_signals_destroy (GType itype
)
880 for (i
= 1; i
< g_n_signal_nodes
; i
++)
882 SignalNode
*node
= g_signal_nodes
[i
];
884 if (node
->itype
== itype
)
887 g_warning (G_STRLOC
": signal \"%s\" of type '%s' already destroyed",
889 type_debug_name (node
->itype
));
891 signal_destroy_R (node
);
898 * g_signal_stop_emission:
899 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
900 * @signal_id: the signal identifier, as returned by g_signal_lookup().
901 * @detail: the detail which the signal was emitted with.
903 * Stops a signal's current emission.
905 * This will prevent the default method from running, if the signal was
906 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
909 * Prints a warning if used on a signal which isn't being emitted.
912 g_signal_stop_emission (gpointer instance
,
918 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
919 g_return_if_fail (signal_id
> 0);
922 node
= LOOKUP_SIGNAL_NODE (signal_id
);
923 if (node
&& detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
925 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC
, signal_id
, detail
);
929 if (node
&& g_type_is_a (G_TYPE_FROM_INSTANCE (instance
), node
->itype
))
931 Emission
*emission
= emission_find (signal_id
, detail
, instance
);
935 if (emission
->state
== EMISSION_HOOK
)
936 g_warning (G_STRLOC
": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
937 node
->name
, instance
);
938 else if (emission
->state
== EMISSION_RUN
)
939 emission
->state
= EMISSION_STOP
;
942 g_warning (G_STRLOC
": no emission of signal \"%s\" to stop for instance '%p'",
943 node
->name
, instance
);
946 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC
, signal_id
, instance
);
951 signal_finalize_hook (GHookList
*hook_list
,
954 GDestroyNotify destroy
= hook
->destroy
;
958 hook
->destroy
= NULL
;
960 destroy (hook
->data
);
966 * g_signal_add_emission_hook:
967 * @signal_id: the signal identifier, as returned by g_signal_lookup().
968 * @detail: the detail on which to call the hook.
969 * @hook_func: a #GSignalEmissionHook function.
970 * @hook_data: user data for @hook_func.
971 * @data_destroy: a #GDestroyNotify for @hook_data.
973 * Adds an emission hook for a signal, which will get called for any emission
974 * of that signal, independent of the instance. This is possible only
975 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
977 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
980 g_signal_add_emission_hook (guint signal_id
,
982 GSignalEmissionHook hook_func
,
984 GDestroyNotify data_destroy
)
986 static gulong seq_hook_id
= 1;
989 SignalHook
*signal_hook
;
991 g_return_val_if_fail (signal_id
> 0, 0);
992 g_return_val_if_fail (hook_func
!= NULL
, 0);
995 node
= LOOKUP_SIGNAL_NODE (signal_id
);
996 if (!node
|| node
->destroyed
)
998 g_warning ("%s: invalid signal id '%u'", G_STRLOC
, signal_id
);
1002 if (node
->flags
& G_SIGNAL_NO_HOOKS
)
1004 g_warning ("%s: signal id '%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC
, signal_id
);
1008 if (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
1010 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC
, signal_id
, detail
);
1014 node
->single_va_closure_is_valid
= FALSE
;
1015 if (!node
->emission_hooks
)
1017 node
->emission_hooks
= g_new (GHookList
, 1);
1018 g_hook_list_init (node
->emission_hooks
, sizeof (SignalHook
));
1019 node
->emission_hooks
->finalize_hook
= signal_finalize_hook
;
1022 node_check_deprecated (node
);
1024 hook
= g_hook_alloc (node
->emission_hooks
);
1025 hook
->data
= hook_data
;
1026 hook
->func
= (gpointer
) hook_func
;
1027 hook
->destroy
= data_destroy
;
1028 signal_hook
= SIGNAL_HOOK (hook
);
1029 signal_hook
->detail
= detail
;
1030 node
->emission_hooks
->seq_id
= seq_hook_id
;
1031 g_hook_append (node
->emission_hooks
, hook
);
1032 seq_hook_id
= node
->emission_hooks
->seq_id
;
1036 return hook
->hook_id
;
1040 * g_signal_remove_emission_hook:
1041 * @signal_id: the id of the signal
1042 * @hook_id: the id of the emission hook, as returned by
1043 * g_signal_add_emission_hook()
1045 * Deletes an emission hook.
1048 g_signal_remove_emission_hook (guint signal_id
,
1053 g_return_if_fail (signal_id
> 0);
1054 g_return_if_fail (hook_id
> 0);
1057 node
= LOOKUP_SIGNAL_NODE (signal_id
);
1058 if (!node
|| node
->destroyed
)
1060 g_warning ("%s: invalid signal id '%u'", G_STRLOC
, signal_id
);
1063 else if (!node
->emission_hooks
|| !g_hook_destroy (node
->emission_hooks
, hook_id
))
1064 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC
, node
->name
, hook_id
);
1066 node
->single_va_closure_is_valid
= FALSE
;
1073 signal_parse_name (const gchar
*name
,
1076 gboolean force_quark
)
1078 const gchar
*colon
= strchr (name
, ':');
1083 signal_id
= signal_id_lookup (g_quark_try_string (name
), itype
);
1084 if (signal_id
&& detail_p
)
1087 else if (colon
[1] == ':')
1090 guint l
= colon
- name
;
1094 memcpy (buffer
, name
, l
);
1096 signal_id
= signal_id_lookup (g_quark_try_string (buffer
), itype
);
1100 gchar
*signal
= g_new (gchar
, l
+ 1);
1102 memcpy (signal
, name
, l
);
1104 signal_id
= signal_id_lookup (g_quark_try_string (signal
), itype
);
1108 if (signal_id
&& detail_p
)
1109 *detail_p
= colon
[2] ? (force_quark
? g_quark_from_string
: g_quark_try_string
) (colon
+ 2) : 0;
1117 * g_signal_parse_name:
1118 * @detailed_signal: a string of the form "signal-name::detail".
1119 * @itype: The interface/instance type that introduced "signal-name".
1120 * @signal_id_p: (out): Location to store the signal id.
1121 * @detail_p: (out): Location to store the detail quark.
1122 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1124 * Internal function to parse a signal name into its @signal_id
1125 * and @detail quark.
1127 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1130 g_signal_parse_name (const gchar
*detailed_signal
,
1134 gboolean force_detail_quark
)
1140 g_return_val_if_fail (detailed_signal
!= NULL
, FALSE
);
1141 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype
) || G_TYPE_IS_INTERFACE (itype
), FALSE
);
1144 signal_id
= signal_parse_name (detailed_signal
, itype
, &detail
, force_detail_quark
);
1147 node
= signal_id
? LOOKUP_SIGNAL_NODE (signal_id
) : NULL
;
1148 if (!node
|| node
->destroyed
||
1149 (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
)))
1153 *signal_id_p
= signal_id
;
1161 * g_signal_stop_emission_by_name:
1162 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
1163 * @detailed_signal: a string of the form "signal-name::detail".
1165 * Stops a signal's current emission.
1167 * This is just like g_signal_stop_emission() except it will look up the
1168 * signal id for you.
1171 g_signal_stop_emission_by_name (gpointer instance
,
1172 const gchar
*detailed_signal
)
1178 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
1179 g_return_if_fail (detailed_signal
!= NULL
);
1182 itype
= G_TYPE_FROM_INSTANCE (instance
);
1183 signal_id
= signal_parse_name (detailed_signal
, itype
, &detail
, TRUE
);
1186 SignalNode
*node
= LOOKUP_SIGNAL_NODE (signal_id
);
1188 if (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
1189 g_warning ("%s: signal '%s' does not support details", G_STRLOC
, detailed_signal
);
1190 else if (!g_type_is_a (itype
, node
->itype
))
1191 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1192 G_STRLOC
, detailed_signal
, instance
, g_type_name (itype
));
1195 Emission
*emission
= emission_find (signal_id
, detail
, instance
);
1199 if (emission
->state
== EMISSION_HOOK
)
1200 g_warning (G_STRLOC
": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1201 node
->name
, instance
);
1202 else if (emission
->state
== EMISSION_RUN
)
1203 emission
->state
= EMISSION_STOP
;
1206 g_warning (G_STRLOC
": no emission of signal \"%s\" to stop for instance '%p'",
1207 node
->name
, instance
);
1211 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1212 G_STRLOC
, detailed_signal
, instance
, g_type_name (itype
));
1218 * @name: the signal's name.
1219 * @itype: the type that the signal operates on.
1221 * Given the name of the signal and the type of object it connects to, gets
1222 * the signal's identifying integer. Emitting the signal by number is
1223 * somewhat faster than using the name each time.
1225 * Also tries the ancestors of the given type.
1227 * See g_signal_new() for details on allowed signal names.
1229 * Returns: the signal's identifying number, or 0 if no signal was found.
1232 g_signal_lookup (const gchar
*name
,
1236 g_return_val_if_fail (name
!= NULL
, 0);
1237 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype
) || G_TYPE_IS_INTERFACE (itype
), 0);
1240 signal_id
= signal_id_lookup (g_quark_try_string (name
), itype
);
1244 /* give elaborate warnings */
1245 if (!g_type_name (itype
))
1246 g_warning (G_STRLOC
": unable to lookup signal \"%s\" for invalid type id '%"G_GSIZE_FORMAT
"'",
1248 else if (!G_TYPE_IS_INSTANTIATABLE (itype
))
1249 g_warning (G_STRLOC
": unable to lookup signal \"%s\" for non instantiatable type '%s'",
1250 name
, g_type_name (itype
));
1251 else if (!g_type_class_peek (itype
))
1252 g_warning (G_STRLOC
": unable to lookup signal \"%s\" of unloaded type '%s'",
1253 name
, g_type_name (itype
));
1260 * g_signal_list_ids:
1261 * @itype: Instance or interface type.
1262 * @n_ids: Location to store the number of signal ids for @itype.
1264 * Lists the signals by id that a certain instance or interface type
1265 * created. Further information about the signals can be acquired through
1268 * Returns: (array length=n_ids) (transfer full): Newly allocated array of signal IDs.
1271 g_signal_list_ids (GType itype
,
1279 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype
) || G_TYPE_IS_INTERFACE (itype
), NULL
);
1280 g_return_val_if_fail (n_ids
!= NULL
, NULL
);
1283 keys
= g_bsearch_array_get_nth (g_signal_key_bsa
, &g_signal_key_bconfig
, 0);
1284 n_nodes
= g_bsearch_array_get_n_nodes (g_signal_key_bsa
);
1285 result
= g_array_new (FALSE
, FALSE
, sizeof (guint
));
1287 for (i
= 0; i
< n_nodes
; i
++)
1288 if (keys
[i
].itype
== itype
)
1290 const gchar
*name
= g_quark_to_string (keys
[i
].quark
);
1292 /* Signal names with "_" in them are aliases to the same
1293 * name with "-" instead of "_".
1295 if (!strchr (name
, '_'))
1296 g_array_append_val (result
, keys
[i
].signal_id
);
1298 *n_ids
= result
->len
;
1302 /* give elaborate warnings */
1303 if (!g_type_name (itype
))
1304 g_warning (G_STRLOC
": unable to list signals for invalid type id '%"G_GSIZE_FORMAT
"'",
1306 else if (!G_TYPE_IS_INSTANTIATABLE (itype
) && !G_TYPE_IS_INTERFACE (itype
))
1307 g_warning (G_STRLOC
": unable to list signals of non instantiatable type '%s'",
1308 g_type_name (itype
));
1309 else if (!g_type_class_peek (itype
) && !G_TYPE_IS_INTERFACE (itype
))
1310 g_warning (G_STRLOC
": unable to list signals of unloaded type '%s'",
1311 g_type_name (itype
));
1314 return (guint
*) g_array_free (result
, FALSE
);
1319 * @signal_id: the signal's identifying number.
1321 * Given the signal's identifier, finds its name.
1323 * Two different signals may have the same name, if they have differing types.
1325 * Returns: the signal name, or %NULL if the signal number was invalid.
1328 g_signal_name (guint signal_id
)
1334 node
= LOOKUP_SIGNAL_NODE (signal_id
);
1335 name
= node
? node
->name
: NULL
;
1338 return (char*) name
;
1343 * @signal_id: The signal id of the signal to query information for.
1344 * @query: (out caller-allocates): A user provided structure that is
1345 * filled in with constant values upon success.
1347 * Queries the signal system for in-depth information about a
1348 * specific signal. This function will fill in a user-provided
1349 * structure to hold signal-specific information. If an invalid
1350 * signal id is passed in, the @signal_id member of the #GSignalQuery
1351 * is 0. All members filled into the #GSignalQuery structure should
1352 * be considered constant and have to be left untouched.
1355 g_signal_query (guint signal_id
,
1356 GSignalQuery
*query
)
1360 g_return_if_fail (query
!= NULL
);
1363 node
= LOOKUP_SIGNAL_NODE (signal_id
);
1364 if (!node
|| node
->destroyed
)
1365 query
->signal_id
= 0;
1368 query
->signal_id
= node
->signal_id
;
1369 query
->signal_name
= node
->name
;
1370 query
->itype
= node
->itype
;
1371 query
->signal_flags
= node
->flags
;
1372 query
->return_type
= node
->return_type
;
1373 query
->n_params
= node
->n_params
;
1374 query
->param_types
= node
->param_types
;
1381 * @signal_name: the name for the signal
1382 * @itype: the type this signal pertains to. It will also pertain to
1383 * types which are derived from this type.
1384 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1385 * the default handler is to be invoked. You should at least specify
1386 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1387 * @class_offset: The offset of the function pointer in the class structure
1388 * for this type. Used to invoke a class method generically. Pass 0 to
1389 * not associate a class method slot with this signal.
1390 * @accumulator: the accumulator for this signal; may be %NULL.
1391 * @accu_data: user data for the @accumulator.
1392 * @c_marshaller: (nullable): the function to translate arrays of parameter
1393 * values to signal emissions into C language callback invocations or %NULL.
1394 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1395 * without a return value.
1396 * @n_params: the number of parameter types to follow.
1397 * @...: a list of types, one for each parameter.
1399 * Creates a new signal. (This is usually done in the class initializer.)
1401 * A signal name consists of segments consisting of ASCII letters and
1402 * digits, separated by either the '-' or '_' character. The first
1403 * character of a signal name must be a letter. Names which violate these
1404 * rules lead to undefined behaviour of the GSignal system.
1406 * When registering a signal and looking up a signal, either separator can
1407 * be used, but they cannot be mixed.
1409 * If 0 is used for @class_offset subclasses cannot override the class handler
1410 * in their class_init method by doing super_class->signal_handler = my_signal_handler.
1411 * Instead they will have to use g_signal_override_class_handler().
1413 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1414 * the marshaller for this signal.
1416 * Returns: the signal id
1419 g_signal_new (const gchar
*signal_name
,
1421 GSignalFlags signal_flags
,
1423 GSignalAccumulator accumulator
,
1425 GSignalCMarshaller c_marshaller
,
1433 g_return_val_if_fail (signal_name
!= NULL
, 0);
1435 va_start (args
, n_params
);
1437 signal_id
= g_signal_new_valist (signal_name
, itype
, signal_flags
,
1438 class_offset
? g_signal_type_cclosure_new (itype
, class_offset
) : NULL
,
1439 accumulator
, accu_data
, c_marshaller
,
1440 return_type
, n_params
, args
);
1448 * g_signal_new_class_handler:
1449 * @signal_name: the name for the signal
1450 * @itype: the type this signal pertains to. It will also pertain to
1451 * types which are derived from this type.
1452 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1453 * the default handler is to be invoked. You should at least specify
1454 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1455 * @class_handler: a #GCallback which acts as class implementation of
1456 * this signal. Used to invoke a class method generically. Pass %NULL to
1457 * not associate a class method with this signal.
1458 * @accumulator: the accumulator for this signal; may be %NULL.
1459 * @accu_data: user data for the @accumulator.
1460 * @c_marshaller: (nullable): the function to translate arrays of parameter
1461 * values to signal emissions into C language callback invocations or %NULL.
1462 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1463 * without a return value.
1464 * @n_params: the number of parameter types to follow.
1465 * @...: a list of types, one for each parameter.
1467 * Creates a new signal. (This is usually done in the class initializer.)
1469 * This is a variant of g_signal_new() that takes a C callback instead
1470 * off a class offset for the signal's class handler. This function
1471 * doesn't need a function pointer exposed in the class structure of
1472 * an object definition, instead the function pointer is passed
1473 * directly and can be overriden by derived classes with
1474 * g_signal_override_class_closure() or
1475 * g_signal_override_class_handler()and chained to with
1476 * g_signal_chain_from_overridden() or
1477 * g_signal_chain_from_overridden_handler().
1479 * See g_signal_new() for information about signal names.
1481 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1482 * the marshaller for this signal.
1484 * Returns: the signal id
1489 g_signal_new_class_handler (const gchar
*signal_name
,
1491 GSignalFlags signal_flags
,
1492 GCallback class_handler
,
1493 GSignalAccumulator accumulator
,
1495 GSignalCMarshaller c_marshaller
,
1503 g_return_val_if_fail (signal_name
!= NULL
, 0);
1505 va_start (args
, n_params
);
1507 signal_id
= g_signal_new_valist (signal_name
, itype
, signal_flags
,
1508 class_handler
? g_cclosure_new (class_handler
, NULL
, NULL
) : NULL
,
1509 accumulator
, accu_data
, c_marshaller
,
1510 return_type
, n_params
, args
);
1517 static inline ClassClosure
*
1518 signal_find_class_closure (SignalNode
*node
,
1521 GBSearchArray
*bsa
= node
->class_closure_bsa
;
1528 /* cc->instance_type is 0 for default closure */
1530 if (g_bsearch_array_get_n_nodes (bsa
) == 1)
1532 cc
= g_bsearch_array_get_nth (bsa
, &g_class_closure_bconfig
, 0);
1533 if (cc
&& cc
->instance_type
== 0) /* check for default closure */
1537 key
.instance_type
= itype
;
1538 cc
= g_bsearch_array_lookup (bsa
, &g_class_closure_bconfig
, &key
);
1539 while (!cc
&& key
.instance_type
)
1541 key
.instance_type
= g_type_parent (key
.instance_type
);
1542 cc
= g_bsearch_array_lookup (bsa
, &g_class_closure_bconfig
, &key
);
1550 static inline GClosure
*
1551 signal_lookup_closure (SignalNode
*node
,
1552 GTypeInstance
*instance
)
1556 cc
= signal_find_class_closure (node
, G_TYPE_FROM_INSTANCE (instance
));
1557 return cc
? cc
->closure
: NULL
;
1561 signal_add_class_closure (SignalNode
*node
,
1567 node
->single_va_closure_is_valid
= FALSE
;
1569 if (!node
->class_closure_bsa
)
1570 node
->class_closure_bsa
= g_bsearch_array_create (&g_class_closure_bconfig
);
1571 key
.instance_type
= itype
;
1572 key
.closure
= g_closure_ref (closure
);
1573 node
->class_closure_bsa
= g_bsearch_array_insert (node
->class_closure_bsa
,
1574 &g_class_closure_bconfig
,
1576 g_closure_sink (closure
);
1577 if (node
->c_marshaller
&& closure
&& G_CLOSURE_NEEDS_MARSHAL (closure
))
1579 g_closure_set_marshal (closure
, node
->c_marshaller
);
1580 if (node
->va_marshaller
)
1581 _g_closure_set_va_marshal (closure
, node
->va_marshaller
);
1587 * @signal_name: the name for the signal
1588 * @itype: the type this signal pertains to. It will also pertain to
1589 * types which are derived from this type
1590 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1591 * the default handler is to be invoked. You should at least specify
1592 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1593 * @class_closure: (nullable): The closure to invoke on signal emission;
1595 * @accumulator: (nullable): the accumulator for this signal; may be %NULL
1596 * @accu_data: user data for the @accumulator
1597 * @c_marshaller: (nullable): the function to translate arrays of
1598 * parameter values to signal emissions into C language callback
1599 * invocations or %NULL
1600 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1601 * without a return value
1602 * @n_params: the length of @param_types
1603 * @param_types: (array length=n_params): an array of types, one for
1606 * Creates a new signal. (This is usually done in the class initializer.)
1608 * See g_signal_new() for details on allowed signal names.
1610 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1611 * the marshaller for this signal.
1613 * Returns: the signal id
1616 g_signal_newv (const gchar
*signal_name
,
1618 GSignalFlags signal_flags
,
1619 GClosure
*class_closure
,
1620 GSignalAccumulator accumulator
,
1622 GSignalCMarshaller c_marshaller
,
1630 GSignalCMarshaller builtin_c_marshaller
;
1631 GSignalCVaMarshaller builtin_va_marshaller
;
1632 GSignalCVaMarshaller va_marshaller
;
1634 g_return_val_if_fail (signal_name
!= NULL
, 0);
1635 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype
) || G_TYPE_IS_INTERFACE (itype
), 0);
1637 g_return_val_if_fail (param_types
!= NULL
, 0);
1638 g_return_val_if_fail ((return_type
& G_SIGNAL_TYPE_STATIC_SCOPE
) == 0, 0);
1639 if (return_type
== (G_TYPE_NONE
& ~G_SIGNAL_TYPE_STATIC_SCOPE
))
1640 g_return_val_if_fail (accumulator
== NULL
, 0);
1642 g_return_val_if_fail (accu_data
== NULL
, 0);
1644 name
= g_strdup (signal_name
);
1645 g_strdelimit (name
, G_STR_DELIMITERS
":^", '_'); /* FIXME do character checks like for types */
1649 signal_id
= signal_id_lookup (g_quark_try_string (name
), itype
);
1650 node
= LOOKUP_SIGNAL_NODE (signal_id
);
1651 if (node
&& !node
->destroyed
)
1653 g_warning (G_STRLOC
": signal \"%s\" already exists in the '%s' %s",
1655 type_debug_name (node
->itype
),
1656 G_TYPE_IS_INTERFACE (node
->itype
) ? "interface" : "class ancestry");
1661 if (node
&& node
->itype
!= itype
)
1663 g_warning (G_STRLOC
": signal \"%s\" for type '%s' was previously created for type '%s'",
1665 type_debug_name (itype
),
1666 type_debug_name (node
->itype
));
1671 for (i
= 0; i
< n_params
; i
++)
1672 if (!G_TYPE_IS_VALUE (param_types
[i
] & ~G_SIGNAL_TYPE_STATIC_SCOPE
))
1674 g_warning (G_STRLOC
": parameter %d of type '%s' for signal \"%s::%s\" is not a value type",
1675 i
+ 1, type_debug_name (param_types
[i
]), type_debug_name (itype
), name
);
1680 if (return_type
!= G_TYPE_NONE
&& !G_TYPE_IS_VALUE (return_type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
))
1682 g_warning (G_STRLOC
": return value of type '%s' for signal \"%s::%s\" is not a value type",
1683 type_debug_name (return_type
), type_debug_name (itype
), name
);
1688 if (return_type
!= G_TYPE_NONE
&&
1689 (signal_flags
& (G_SIGNAL_RUN_FIRST
| G_SIGNAL_RUN_LAST
| G_SIGNAL_RUN_CLEANUP
)) == G_SIGNAL_RUN_FIRST
)
1691 g_warning (G_STRLOC
": signal \"%s::%s\" has return type '%s' and is only G_SIGNAL_RUN_FIRST",
1692 type_debug_name (itype
), name
, type_debug_name (return_type
));
1698 /* setup permanent portion of signal node */
1703 signal_id
= g_n_signal_nodes
++;
1704 node
= g_new (SignalNode
, 1);
1705 node
->signal_id
= signal_id
;
1706 g_signal_nodes
= g_renew (SignalNode
*, g_signal_nodes
, g_n_signal_nodes
);
1707 g_signal_nodes
[signal_id
] = node
;
1708 node
->itype
= itype
;
1711 key
.quark
= g_quark_from_string (node
->name
);
1712 key
.signal_id
= signal_id
;
1713 g_signal_key_bsa
= g_bsearch_array_insert (g_signal_key_bsa
, &g_signal_key_bconfig
, &key
);
1714 g_strdelimit (name
, "_", '-');
1715 node
->name
= g_intern_string (name
);
1716 key
.quark
= g_quark_from_string (name
);
1717 g_signal_key_bsa
= g_bsearch_array_insert (g_signal_key_bsa
, &g_signal_key_bconfig
, &key
);
1719 TRACE(GOBJECT_SIGNAL_NEW(signal_id
, name
, itype
));
1721 node
->destroyed
= FALSE
;
1723 /* setup reinitializable portion */
1724 node
->single_va_closure_is_valid
= FALSE
;
1725 node
->flags
= signal_flags
& G_SIGNAL_FLAGS_MASK
;
1726 node
->n_params
= n_params
;
1727 node
->param_types
= g_memdup (param_types
, sizeof (GType
) * n_params
);
1728 node
->return_type
= return_type
;
1729 node
->class_closure_bsa
= NULL
;
1732 node
->accumulator
= g_new (SignalAccumulator
, 1);
1733 node
->accumulator
->func
= accumulator
;
1734 node
->accumulator
->data
= accu_data
;
1737 node
->accumulator
= NULL
;
1739 builtin_c_marshaller
= NULL
;
1740 builtin_va_marshaller
= NULL
;
1742 /* Pick up built-in va marshallers for standard types, and
1743 instead of generic marshaller if no marshaller specified */
1744 if (n_params
== 0 && return_type
== G_TYPE_NONE
)
1746 builtin_c_marshaller
= g_cclosure_marshal_VOID__VOID
;
1747 builtin_va_marshaller
= g_cclosure_marshal_VOID__VOIDv
;
1749 else if (n_params
== 1 && return_type
== G_TYPE_NONE
)
1751 #define ADD_CHECK(__type__) \
1752 else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \
1754 builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \
1755 builtin_va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \
1778 if (c_marshaller
== NULL
)
1780 if (builtin_c_marshaller
)
1782 c_marshaller
= builtin_c_marshaller
;
1783 va_marshaller
= builtin_va_marshaller
;
1787 c_marshaller
= g_cclosure_marshal_generic
;
1788 va_marshaller
= g_cclosure_marshal_generic_va
;
1792 va_marshaller
= NULL
;
1794 node
->c_marshaller
= c_marshaller
;
1795 node
->va_marshaller
= va_marshaller
;
1796 node
->emission_hooks
= NULL
;
1798 signal_add_class_closure (node
, 0, class_closure
);
1808 * g_signal_set_va_marshaller:
1809 * @signal_id: the signal id
1810 * @instance_type: the instance type on which to set the marshaller.
1811 * @va_marshaller: the marshaller to set.
1813 * Change the #GSignalCVaMarshaller used for a given signal. This is a
1814 * specialised form of the marshaller that can often be used for the
1815 * common case of a single connected signal handler and avoids the
1816 * overhead of #GValue. Its use is optional.
1821 g_signal_set_va_marshaller (guint signal_id
,
1822 GType instance_type
,
1823 GSignalCVaMarshaller va_marshaller
)
1827 g_return_if_fail (signal_id
> 0);
1828 g_return_if_fail (va_marshaller
!= NULL
);
1831 node
= LOOKUP_SIGNAL_NODE (signal_id
);
1834 node
->va_marshaller
= va_marshaller
;
1835 if (node
->class_closure_bsa
)
1837 ClassClosure
*cc
= g_bsearch_array_get_nth (node
->class_closure_bsa
, &g_class_closure_bconfig
, 0);
1838 if (cc
->closure
->marshal
== node
->c_marshaller
)
1839 _g_closure_set_va_marshal (cc
->closure
, va_marshaller
);
1842 node
->single_va_closure_is_valid
= FALSE
;
1850 * g_signal_new_valist:
1851 * @signal_name: the name for the signal
1852 * @itype: the type this signal pertains to. It will also pertain to
1853 * types which are derived from this type.
1854 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1855 * the default handler is to be invoked. You should at least specify
1856 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1857 * @class_closure: The closure to invoke on signal emission; may be %NULL.
1858 * @accumulator: the accumulator for this signal; may be %NULL.
1859 * @accu_data: user data for the @accumulator.
1860 * @c_marshaller: (nullable): the function to translate arrays of parameter
1861 * values to signal emissions into C language callback invocations or %NULL.
1862 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1863 * without a return value.
1864 * @n_params: the number of parameter types in @args.
1865 * @args: va_list of #GType, one for each parameter.
1867 * Creates a new signal. (This is usually done in the class initializer.)
1869 * See g_signal_new() for details on allowed signal names.
1871 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1872 * the marshaller for this signal.
1874 * Returns: the signal id
1877 g_signal_new_valist (const gchar
*signal_name
,
1879 GSignalFlags signal_flags
,
1880 GClosure
*class_closure
,
1881 GSignalAccumulator accumulator
,
1883 GSignalCMarshaller c_marshaller
,
1894 param_types
= g_new (GType
, n_params
);
1896 for (i
= 0; i
< n_params
; i
++)
1897 param_types
[i
] = va_arg (args
, GType
);
1902 signal_id
= g_signal_newv (signal_name
, itype
, signal_flags
,
1903 class_closure
, accumulator
, accu_data
, c_marshaller
,
1904 return_type
, n_params
, param_types
);
1905 g_free (param_types
);
1911 signal_destroy_R (SignalNode
*signal_node
)
1913 SignalNode node
= *signal_node
;
1915 signal_node
->destroyed
= TRUE
;
1917 /* reentrancy caution, zero out real contents first */
1918 signal_node
->single_va_closure_is_valid
= FALSE
;
1919 signal_node
->n_params
= 0;
1920 signal_node
->param_types
= NULL
;
1921 signal_node
->return_type
= 0;
1922 signal_node
->class_closure_bsa
= NULL
;
1923 signal_node
->accumulator
= NULL
;
1924 signal_node
->c_marshaller
= NULL
;
1925 signal_node
->va_marshaller
= NULL
;
1926 signal_node
->emission_hooks
= NULL
;
1928 #ifdef G_ENABLE_DEBUG
1929 /* check current emissions */
1933 for (emission
= g_emissions
; emission
; emission
= emission
->next
)
1934 if (emission
->ihint
.signal_id
== node
.signal_id
)
1935 g_critical (G_STRLOC
": signal \"%s\" being destroyed is currently in emission (instance '%p')",
1936 node
.name
, emission
->instance
);
1940 /* free contents that need to
1943 g_free (node
.param_types
);
1944 if (node
.class_closure_bsa
)
1948 for (i
= 0; i
< node
.class_closure_bsa
->n_nodes
; i
++)
1950 ClassClosure
*cc
= g_bsearch_array_get_nth (node
.class_closure_bsa
, &g_class_closure_bconfig
, i
);
1952 g_closure_unref (cc
->closure
);
1954 g_bsearch_array_free (node
.class_closure_bsa
, &g_class_closure_bconfig
);
1956 g_free (node
.accumulator
);
1957 if (node
.emission_hooks
)
1959 g_hook_list_clear (node
.emission_hooks
);
1960 g_free (node
.emission_hooks
);
1966 * g_signal_override_class_closure:
1967 * @signal_id: the signal id
1968 * @instance_type: the instance type on which to override the class closure
1970 * @class_closure: the closure.
1972 * Overrides the class closure (i.e. the default handler) for the given signal
1973 * for emissions on instances of @instance_type. @instance_type must be derived
1974 * from the type to which the signal belongs.
1976 * See g_signal_chain_from_overridden() and
1977 * g_signal_chain_from_overridden_handler() for how to chain up to the
1978 * parent class closure from inside the overridden one.
1981 g_signal_override_class_closure (guint signal_id
,
1982 GType instance_type
,
1983 GClosure
*class_closure
)
1987 g_return_if_fail (signal_id
> 0);
1988 g_return_if_fail (class_closure
!= NULL
);
1991 node
= LOOKUP_SIGNAL_NODE (signal_id
);
1992 node_check_deprecated (node
);
1993 if (!g_type_is_a (instance_type
, node
->itype
))
1994 g_warning ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC
, type_debug_name (instance_type
), signal_id
);
1997 ClassClosure
*cc
= signal_find_class_closure (node
, instance_type
);
1999 if (cc
&& cc
->instance_type
== instance_type
)
2000 g_warning ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC
, type_debug_name (instance_type
), signal_id
);
2002 signal_add_class_closure (node
, instance_type
, class_closure
);
2008 * g_signal_override_class_handler:
2009 * @signal_name: the name for the signal
2010 * @instance_type: the instance type on which to override the class handler
2012 * @class_handler: the handler.
2014 * Overrides the class closure (i.e. the default handler) for the
2015 * given signal for emissions on instances of @instance_type with
2016 * callback @class_handler. @instance_type must be derived from the
2017 * type to which the signal belongs.
2019 * See g_signal_chain_from_overridden() and
2020 * g_signal_chain_from_overridden_handler() for how to chain up to the
2021 * parent class closure from inside the overridden one.
2026 g_signal_override_class_handler (const gchar
*signal_name
,
2027 GType instance_type
,
2028 GCallback class_handler
)
2032 g_return_if_fail (signal_name
!= NULL
);
2033 g_return_if_fail (instance_type
!= G_TYPE_NONE
);
2034 g_return_if_fail (class_handler
!= NULL
);
2036 signal_id
= g_signal_lookup (signal_name
, instance_type
);
2039 g_signal_override_class_closure (signal_id
, instance_type
,
2040 g_cclosure_new (class_handler
, NULL
, NULL
));
2042 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT
"'",
2043 G_STRLOC
, signal_name
, instance_type
);
2048 * g_signal_chain_from_overridden:
2049 * @instance_and_params: (array) the argument list of the signal emission.
2050 * The first element in the array is a #GValue for the instance the signal
2051 * is being emitted on. The rest are any arguments to be passed to the signal.
2052 * @return_value: Location for the return value.
2054 * Calls the original class closure of a signal. This function should only
2055 * be called from an overridden class closure; see
2056 * g_signal_override_class_closure() and
2057 * g_signal_override_class_handler().
2060 g_signal_chain_from_overridden (const GValue
*instance_and_params
,
2061 GValue
*return_value
)
2063 GType chain_type
= 0, restore_type
= 0;
2064 Emission
*emission
= NULL
;
2065 GClosure
*closure
= NULL
;
2069 g_return_if_fail (instance_and_params
!= NULL
);
2070 instance
= g_value_peek_pointer (instance_and_params
);
2071 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
2074 emission
= emission_find_innermost (instance
);
2077 SignalNode
*node
= LOOKUP_SIGNAL_NODE (emission
->ihint
.signal_id
);
2079 g_assert (node
!= NULL
); /* paranoid */
2081 /* we should probably do the same parameter checks as g_signal_emit() here.
2083 if (emission
->chain_type
!= G_TYPE_NONE
)
2085 ClassClosure
*cc
= signal_find_class_closure (node
, emission
->chain_type
);
2087 g_assert (cc
!= NULL
); /* closure currently in call stack */
2089 n_params
= node
->n_params
;
2090 restore_type
= cc
->instance_type
;
2091 cc
= signal_find_class_closure (node
, g_type_parent (cc
->instance_type
));
2092 if (cc
&& cc
->instance_type
!= restore_type
)
2094 closure
= cc
->closure
;
2095 chain_type
= cc
->instance_type
;
2099 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC
, node
->signal_id
, instance
);
2102 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC
, instance
);
2106 emission
->chain_type
= chain_type
;
2108 g_closure_invoke (closure
,
2111 instance_and_params
,
2114 emission
->chain_type
= restore_type
;
2120 * g_signal_chain_from_overridden_handler: (skip)
2121 * @instance: (type GObject.TypeInstance): the instance the signal is being
2123 * @...: parameters to be passed to the parent class closure, followed by a
2124 * location for the return value. If the return type of the signal
2125 * is #G_TYPE_NONE, the return value location can be omitted.
2127 * Calls the original class closure of a signal. This function should
2128 * only be called from an overridden class closure; see
2129 * g_signal_override_class_closure() and
2130 * g_signal_override_class_handler().
2135 g_signal_chain_from_overridden_handler (gpointer instance
,
2138 GType chain_type
= 0, restore_type
= 0;
2139 Emission
*emission
= NULL
;
2140 GClosure
*closure
= NULL
;
2144 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
2147 emission
= emission_find_innermost (instance
);
2150 node
= LOOKUP_SIGNAL_NODE (emission
->ihint
.signal_id
);
2152 g_assert (node
!= NULL
); /* paranoid */
2154 /* we should probably do the same parameter checks as g_signal_emit() here.
2156 if (emission
->chain_type
!= G_TYPE_NONE
)
2158 ClassClosure
*cc
= signal_find_class_closure (node
, emission
->chain_type
);
2160 g_assert (cc
!= NULL
); /* closure currently in call stack */
2162 n_params
= node
->n_params
;
2163 restore_type
= cc
->instance_type
;
2164 cc
= signal_find_class_closure (node
, g_type_parent (cc
->instance_type
));
2165 if (cc
&& cc
->instance_type
!= restore_type
)
2167 closure
= cc
->closure
;
2168 chain_type
= cc
->instance_type
;
2172 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC
, node
->signal_id
, instance
);
2175 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC
, instance
);
2179 GValue
*instance_and_params
;
2180 GType signal_return_type
;
2181 GValue
*param_values
;
2185 va_start (var_args
, instance
);
2187 signal_return_type
= node
->return_type
;
2188 instance_and_params
= g_alloca (sizeof (GValue
) * (n_params
+ 1));
2189 memset (instance_and_params
, 0, sizeof (GValue
) * (n_params
+ 1));
2190 param_values
= instance_and_params
+ 1;
2192 for (i
= 0; i
< node
->n_params
; i
++)
2195 GType ptype
= node
->param_types
[i
] & ~G_SIGNAL_TYPE_STATIC_SCOPE
;
2196 gboolean static_scope
= node
->param_types
[i
] & G_SIGNAL_TYPE_STATIC_SCOPE
;
2199 G_VALUE_COLLECT_INIT (param_values
+ i
, ptype
,
2201 static_scope
? G_VALUE_NOCOPY_CONTENTS
: 0,
2205 g_warning ("%s: %s", G_STRLOC
, error
);
2208 /* we purposely leak the value here, it might not be
2209 * in a sane state if an error condition occoured
2212 g_value_unset (param_values
+ i
);
2221 instance_and_params
->g_type
= 0;
2222 g_value_init_from_instance (instance_and_params
, instance
);
2225 emission
->chain_type
= chain_type
;
2228 if (signal_return_type
== G_TYPE_NONE
)
2230 g_closure_invoke (closure
,
2233 instance_and_params
,
2238 GValue return_value
= G_VALUE_INIT
;
2239 gchar
*error
= NULL
;
2240 GType rtype
= signal_return_type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
;
2241 gboolean static_scope
= signal_return_type
& G_SIGNAL_TYPE_STATIC_SCOPE
;
2243 g_value_init (&return_value
, rtype
);
2245 g_closure_invoke (closure
,
2248 instance_and_params
,
2251 G_VALUE_LCOPY (&return_value
,
2253 static_scope
? G_VALUE_NOCOPY_CONTENTS
: 0,
2257 g_value_unset (&return_value
);
2261 g_warning ("%s: %s", G_STRLOC
, error
);
2264 /* we purposely leak the value here, it might not be
2265 * in a sane state if an error condition occurred
2270 for (i
= 0; i
< n_params
; i
++)
2271 g_value_unset (param_values
+ i
);
2272 g_value_unset (instance_and_params
);
2277 emission
->chain_type
= restore_type
;
2283 * g_signal_get_invocation_hint:
2284 * @instance: (type GObject.Object): the instance to query
2286 * Returns the invocation hint of the innermost signal emission of instance.
2288 * Returns: (transfer none): the invocation hint of the innermost signal emission.
2290 GSignalInvocationHint
*
2291 g_signal_get_invocation_hint (gpointer instance
)
2293 Emission
*emission
= NULL
;
2295 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), NULL
);
2298 emission
= emission_find_innermost (instance
);
2301 return emission
? &emission
->ihint
: NULL
;
2305 * g_signal_connect_closure_by_id:
2306 * @instance: (type GObject.Object): the instance to connect to.
2307 * @signal_id: the id of the signal.
2308 * @detail: the detail.
2309 * @closure: the closure to connect.
2310 * @after: whether the handler should be called before or after the
2311 * default handler of the signal.
2313 * Connects a closure to a signal for a particular object.
2315 * Returns: the handler ID (always greater than 0 for successful connections)
2318 g_signal_connect_closure_by_id (gpointer instance
,
2325 gulong handler_seq_no
= 0;
2327 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2328 g_return_val_if_fail (signal_id
> 0, 0);
2329 g_return_val_if_fail (closure
!= NULL
, 0);
2332 node
= LOOKUP_SIGNAL_NODE (signal_id
);
2335 if (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
2336 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC
, signal_id
, detail
);
2337 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance
), node
->itype
))
2338 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC
, signal_id
, instance
);
2341 Handler
*handler
= handler_new (signal_id
, instance
, after
);
2343 handler_seq_no
= handler
->sequential_number
;
2344 handler
->detail
= detail
;
2345 handler
->closure
= g_closure_ref (closure
);
2346 g_closure_sink (closure
);
2347 add_invalid_closure_notify (handler
, instance
);
2348 handler_insert (signal_id
, instance
, handler
);
2349 if (node
->c_marshaller
&& G_CLOSURE_NEEDS_MARSHAL (closure
))
2351 g_closure_set_marshal (closure
, node
->c_marshaller
);
2352 if (node
->va_marshaller
)
2353 _g_closure_set_va_marshal (closure
, node
->va_marshaller
);
2358 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC
, signal_id
, instance
);
2361 return handler_seq_no
;
2365 * g_signal_connect_closure:
2366 * @instance: (type GObject.Object): the instance to connect to.
2367 * @detailed_signal: a string of the form "signal-name::detail".
2368 * @closure: the closure to connect.
2369 * @after: whether the handler should be called before or after the
2370 * default handler of the signal.
2372 * Connects a closure to a signal for a particular object.
2374 * Returns: the handler ID (always greater than 0 for successful connections)
2377 g_signal_connect_closure (gpointer instance
,
2378 const gchar
*detailed_signal
,
2383 gulong handler_seq_no
= 0;
2387 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2388 g_return_val_if_fail (detailed_signal
!= NULL
, 0);
2389 g_return_val_if_fail (closure
!= NULL
, 0);
2392 itype
= G_TYPE_FROM_INSTANCE (instance
);
2393 signal_id
= signal_parse_name (detailed_signal
, itype
, &detail
, TRUE
);
2396 SignalNode
*node
= LOOKUP_SIGNAL_NODE (signal_id
);
2398 if (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
2399 g_warning ("%s: signal '%s' does not support details", G_STRLOC
, detailed_signal
);
2400 else if (!g_type_is_a (itype
, node
->itype
))
2401 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2402 G_STRLOC
, detailed_signal
, instance
, g_type_name (itype
));
2405 Handler
*handler
= handler_new (signal_id
, instance
, after
);
2407 handler_seq_no
= handler
->sequential_number
;
2408 handler
->detail
= detail
;
2409 handler
->closure
= g_closure_ref (closure
);
2410 g_closure_sink (closure
);
2411 add_invalid_closure_notify (handler
, instance
);
2412 handler_insert (signal_id
, instance
, handler
);
2413 if (node
->c_marshaller
&& G_CLOSURE_NEEDS_MARSHAL (handler
->closure
))
2415 g_closure_set_marshal (handler
->closure
, node
->c_marshaller
);
2416 if (node
->va_marshaller
)
2417 _g_closure_set_va_marshal (handler
->closure
, node
->va_marshaller
);
2422 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2423 G_STRLOC
, detailed_signal
, instance
, g_type_name (itype
));
2426 return handler_seq_no
;
2430 node_check_deprecated (const SignalNode
*node
)
2432 static const gchar
* g_enable_diagnostic
= NULL
;
2434 if (G_UNLIKELY (!g_enable_diagnostic
))
2436 g_enable_diagnostic
= g_getenv ("G_ENABLE_DIAGNOSTIC");
2437 if (!g_enable_diagnostic
)
2438 g_enable_diagnostic
= "0";
2441 if (g_enable_diagnostic
[0] == '1')
2443 if (node
->flags
& G_SIGNAL_DEPRECATED
)
2445 g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2446 "anymore. It will be removed in a future version.",
2447 type_debug_name (node
->itype
), node
->name
);
2453 * g_signal_connect_data:
2454 * @instance: (type GObject.Object): the instance to connect to.
2455 * @detailed_signal: a string of the form "signal-name::detail".
2456 * @c_handler: the #GCallback to connect.
2457 * @data: data to pass to @c_handler calls.
2458 * @destroy_data: a #GClosureNotify for @data.
2459 * @connect_flags: a combination of #GConnectFlags.
2461 * Connects a #GCallback function to a signal for a particular object. Similar
2462 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2463 * which will be called when the signal handler is disconnected and no longer
2464 * used. Specify @connect_flags if you need `..._after()` or
2465 * `..._swapped()` variants of this function.
2467 * Returns: the handler ID (always greater than 0 for successful connections)
2470 g_signal_connect_data (gpointer instance
,
2471 const gchar
*detailed_signal
,
2472 GCallback c_handler
,
2474 GClosureNotify destroy_data
,
2475 GConnectFlags connect_flags
)
2478 gulong handler_seq_no
= 0;
2481 gboolean swapped
, after
;
2483 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2484 g_return_val_if_fail (detailed_signal
!= NULL
, 0);
2485 g_return_val_if_fail (c_handler
!= NULL
, 0);
2487 swapped
= (connect_flags
& G_CONNECT_SWAPPED
) != FALSE
;
2488 after
= (connect_flags
& G_CONNECT_AFTER
) != FALSE
;
2491 itype
= G_TYPE_FROM_INSTANCE (instance
);
2492 signal_id
= signal_parse_name (detailed_signal
, itype
, &detail
, TRUE
);
2495 SignalNode
*node
= LOOKUP_SIGNAL_NODE (signal_id
);
2497 node_check_deprecated (node
);
2499 if (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
2500 g_warning ("%s: signal '%s' does not support details", G_STRLOC
, detailed_signal
);
2501 else if (!g_type_is_a (itype
, node
->itype
))
2502 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2503 G_STRLOC
, detailed_signal
, instance
, g_type_name (itype
));
2506 Handler
*handler
= handler_new (signal_id
, instance
, after
);
2508 handler_seq_no
= handler
->sequential_number
;
2509 handler
->detail
= detail
;
2510 handler
->closure
= g_closure_ref ((swapped
? g_cclosure_new_swap
: g_cclosure_new
) (c_handler
, data
, destroy_data
));
2511 g_closure_sink (handler
->closure
);
2512 handler_insert (signal_id
, instance
, handler
);
2513 if (node
->c_marshaller
&& G_CLOSURE_NEEDS_MARSHAL (handler
->closure
))
2515 g_closure_set_marshal (handler
->closure
, node
->c_marshaller
);
2516 if (node
->va_marshaller
)
2517 _g_closure_set_va_marshal (handler
->closure
, node
->va_marshaller
);
2522 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2523 G_STRLOC
, detailed_signal
, instance
, g_type_name (itype
));
2526 return handler_seq_no
;
2530 * g_signal_handler_block:
2531 * @instance: (type GObject.Object): The instance to block the signal handler of.
2532 * @handler_id: Handler id of the handler to be blocked.
2534 * Blocks a handler of an instance so it will not be called during any
2535 * signal emissions unless it is unblocked again. Thus "blocking" a
2536 * signal handler means to temporarily deactive it, a signal handler
2537 * has to be unblocked exactly the same amount of times it has been
2538 * blocked before to become active again.
2540 * The @handler_id has to be a valid signal handler id, connected to a
2541 * signal of @instance.
2544 g_signal_handler_block (gpointer instance
,
2549 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
2550 g_return_if_fail (handler_id
> 0);
2553 handler
= handler_lookup (instance
, handler_id
, NULL
, NULL
);
2556 #ifndef G_DISABLE_CHECKS
2557 if (handler
->block_count
>= HANDLER_MAX_BLOCK_COUNT
- 1)
2558 g_error (G_STRLOC
": handler block_count overflow, %s", REPORT_BUG
);
2560 handler
->block_count
+= 1;
2563 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC
, instance
, handler_id
);
2568 * g_signal_handler_unblock:
2569 * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2570 * @handler_id: Handler id of the handler to be unblocked.
2572 * Undoes the effect of a previous g_signal_handler_block() call. A
2573 * blocked handler is skipped during signal emissions and will not be
2574 * invoked, unblocking it (for exactly the amount of times it has been
2575 * blocked before) reverts its "blocked" state, so the handler will be
2576 * recognized by the signal system and is called upon future or
2577 * currently ongoing signal emissions (since the order in which
2578 * handlers are called during signal emissions is deterministic,
2579 * whether the unblocked handler in question is called as part of a
2580 * currently ongoing emission depends on how far that emission has
2583 * The @handler_id has to be a valid id of a signal handler that is
2584 * connected to a signal of @instance and is currently blocked.
2587 g_signal_handler_unblock (gpointer instance
,
2592 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
2593 g_return_if_fail (handler_id
> 0);
2596 handler
= handler_lookup (instance
, handler_id
, NULL
, NULL
);
2599 if (handler
->block_count
)
2600 handler
->block_count
-= 1;
2602 g_warning (G_STRLOC
": handler '%lu' of instance '%p' is not blocked", handler_id
, instance
);
2605 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC
, instance
, handler_id
);
2610 * g_signal_handler_disconnect:
2611 * @instance: (type GObject.Object): The instance to remove the signal handler from.
2612 * @handler_id: Handler id of the handler to be disconnected.
2614 * Disconnects a handler from an instance so it will not be called during
2615 * any future or currently ongoing emissions of the signal it has been
2616 * connected to. The @handler_id becomes invalid and may be reused.
2618 * The @handler_id has to be a valid signal handler id, connected to a
2619 * signal of @instance.
2622 g_signal_handler_disconnect (gpointer instance
,
2627 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
2628 g_return_if_fail (handler_id
> 0);
2631 handler
= handler_lookup (instance
, handler_id
, 0, 0);
2634 g_hash_table_remove (g_handlers
, handler
);
2635 handler
->sequential_number
= 0;
2636 handler
->block_count
= 1;
2637 remove_invalid_closure_notify (handler
, instance
);
2638 handler_unref_R (handler
->signal_id
, instance
, handler
);
2641 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC
, instance
, handler_id
);
2646 * g_signal_handler_is_connected:
2647 * @instance: (type GObject.Object): The instance where a signal handler is sought.
2648 * @handler_id: the handler ID.
2650 * Returns whether @handler_id is the ID of a handler connected to @instance.
2652 * Returns: whether @handler_id identifies a handler connected to @instance.
2655 g_signal_handler_is_connected (gpointer instance
,
2661 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), FALSE
);
2664 handler
= handler_lookup (instance
, handler_id
, NULL
, NULL
);
2665 connected
= handler
!= NULL
;
2672 * g_signal_handlers_destroy:
2673 * @instance: (type GObject.Object): The instance whose signal handlers are destroyed
2675 * Destroy all signal handlers of a type instance. This function is
2676 * an implementation detail of the #GObject dispose implementation,
2677 * and should not be used outside of the type system.
2680 g_signal_handlers_destroy (gpointer instance
)
2682 GBSearchArray
*hlbsa
;
2684 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
2687 hlbsa
= g_hash_table_lookup (g_handler_list_bsa_ht
, instance
);
2692 /* reentrancy caution, delete instance trace first */
2693 g_hash_table_remove (g_handler_list_bsa_ht
, instance
);
2695 for (i
= 0; i
< hlbsa
->n_nodes
; i
++)
2697 HandlerList
*hlist
= g_bsearch_array_get_nth (hlbsa
, &g_signal_hlbsa_bconfig
, i
);
2698 Handler
*handler
= hlist
->handlers
;
2702 Handler
*tmp
= handler
;
2704 handler
= tmp
->next
;
2705 tmp
->block_count
= 1;
2706 /* cruel unlink, this works because _all_ handlers vanish */
2709 if (tmp
->sequential_number
)
2711 g_hash_table_remove (g_handlers
, tmp
);
2712 remove_invalid_closure_notify (tmp
, instance
);
2713 tmp
->sequential_number
= 0;
2714 handler_unref_R (0, NULL
, tmp
);
2718 g_bsearch_array_free (hlbsa
, &g_signal_hlbsa_bconfig
);
2724 * g_signal_handler_find:
2725 * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2726 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2727 * and/or @data the handler has to match.
2728 * @signal_id: Signal the handler has to be connected to.
2729 * @detail: Signal detail the handler has to be connected to.
2730 * @closure: (nullable): The closure the handler will invoke.
2731 * @func: The C closure callback of the handler (useless for non-C closures).
2732 * @data: The closure data of the handler's closure.
2734 * Finds the first signal handler that matches certain selection criteria.
2735 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2736 * flags, and the criteria values are passed as arguments.
2737 * The match @mask has to be non-0 for successful matches.
2738 * If no handler was found, 0 is returned.
2740 * Returns: A valid non-0 signal handler id for a successful match.
2743 g_signal_handler_find (gpointer instance
,
2744 GSignalMatchType mask
,
2751 gulong handler_seq_no
= 0;
2753 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2754 g_return_val_if_fail ((mask
& ~G_SIGNAL_MATCH_MASK
) == 0, 0);
2756 if (mask
& G_SIGNAL_MATCH_MASK
)
2758 HandlerMatch
*mlist
;
2761 mlist
= handlers_find (instance
, mask
, signal_id
, detail
, closure
, func
, data
, TRUE
);
2764 handler_seq_no
= mlist
->handler
->sequential_number
;
2765 handler_match_free1_R (mlist
, instance
);
2770 return handler_seq_no
;
2774 signal_handlers_foreach_matched_R (gpointer instance
,
2775 GSignalMatchType mask
,
2781 void (*callback
) (gpointer instance
,
2782 gulong handler_seq_no
))
2784 HandlerMatch
*mlist
;
2785 guint n_handlers
= 0;
2787 mlist
= handlers_find (instance
, mask
, signal_id
, detail
, closure
, func
, data
, FALSE
);
2791 if (mlist
->handler
->sequential_number
)
2794 callback (instance
, mlist
->handler
->sequential_number
);
2797 mlist
= handler_match_free1_R (mlist
, instance
);
2804 * g_signal_handlers_block_matched:
2805 * @instance: (type GObject.Object): The instance to block handlers from.
2806 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2807 * and/or @data the handlers have to match.
2808 * @signal_id: Signal the handlers have to be connected to.
2809 * @detail: Signal detail the handlers have to be connected to.
2810 * @closure: (nullable): The closure the handlers will invoke.
2811 * @func: The C closure callback of the handlers (useless for non-C closures).
2812 * @data: The closure data of the handlers' closures.
2814 * Blocks all handlers on an instance that match a certain selection criteria.
2815 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2816 * flags, and the criteria values are passed as arguments.
2817 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2818 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2819 * If no handlers were found, 0 is returned, the number of blocked handlers
2822 * Returns: The number of handlers that matched.
2825 g_signal_handlers_block_matched (gpointer instance
,
2826 GSignalMatchType mask
,
2833 guint n_handlers
= 0;
2835 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2836 g_return_val_if_fail ((mask
& ~G_SIGNAL_MATCH_MASK
) == 0, 0);
2838 if (mask
& (G_SIGNAL_MATCH_CLOSURE
| G_SIGNAL_MATCH_FUNC
| G_SIGNAL_MATCH_DATA
))
2841 n_handlers
= signal_handlers_foreach_matched_R (instance
, mask
, signal_id
, detail
,
2842 closure
, func
, data
,
2843 g_signal_handler_block
);
2851 * g_signal_handlers_unblock_matched:
2852 * @instance: (type GObject.Object): The instance to unblock handlers from.
2853 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2854 * and/or @data the handlers have to match.
2855 * @signal_id: Signal the handlers have to be connected to.
2856 * @detail: Signal detail the handlers have to be connected to.
2857 * @closure: (nullable): The closure the handlers will invoke.
2858 * @func: The C closure callback of the handlers (useless for non-C closures).
2859 * @data: The closure data of the handlers' closures.
2861 * Unblocks all handlers on an instance that match a certain selection
2862 * criteria. The criteria mask is passed as an OR-ed combination of
2863 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2864 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2865 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2866 * If no handlers were found, 0 is returned, the number of unblocked handlers
2867 * otherwise. The match criteria should not apply to any handlers that are
2868 * not currently blocked.
2870 * Returns: The number of handlers that matched.
2873 g_signal_handlers_unblock_matched (gpointer instance
,
2874 GSignalMatchType mask
,
2881 guint n_handlers
= 0;
2883 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2884 g_return_val_if_fail ((mask
& ~G_SIGNAL_MATCH_MASK
) == 0, 0);
2886 if (mask
& (G_SIGNAL_MATCH_CLOSURE
| G_SIGNAL_MATCH_FUNC
| G_SIGNAL_MATCH_DATA
))
2889 n_handlers
= signal_handlers_foreach_matched_R (instance
, mask
, signal_id
, detail
,
2890 closure
, func
, data
,
2891 g_signal_handler_unblock
);
2899 * g_signal_handlers_disconnect_matched:
2900 * @instance: (type GObject.Object): The instance to remove handlers from.
2901 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2902 * and/or @data the handlers have to match.
2903 * @signal_id: Signal the handlers have to be connected to.
2904 * @detail: Signal detail the handlers have to be connected to.
2905 * @closure: (nullable): The closure the handlers will invoke.
2906 * @func: The C closure callback of the handlers (useless for non-C closures).
2907 * @data: The closure data of the handlers' closures.
2909 * Disconnects all handlers on an instance that match a certain
2910 * selection criteria. The criteria mask is passed as an OR-ed
2911 * combination of #GSignalMatchType flags, and the criteria values are
2912 * passed as arguments. Passing at least one of the
2913 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2914 * %G_SIGNAL_MATCH_DATA match flags is required for successful
2915 * matches. If no handlers were found, 0 is returned, the number of
2916 * disconnected handlers otherwise.
2918 * Returns: The number of handlers that matched.
2921 g_signal_handlers_disconnect_matched (gpointer instance
,
2922 GSignalMatchType mask
,
2929 guint n_handlers
= 0;
2931 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), 0);
2932 g_return_val_if_fail ((mask
& ~G_SIGNAL_MATCH_MASK
) == 0, 0);
2934 if (mask
& (G_SIGNAL_MATCH_CLOSURE
| G_SIGNAL_MATCH_FUNC
| G_SIGNAL_MATCH_DATA
))
2937 n_handlers
= signal_handlers_foreach_matched_R (instance
, mask
, signal_id
, detail
,
2938 closure
, func
, data
,
2939 g_signal_handler_disconnect
);
2947 * g_signal_has_handler_pending:
2948 * @instance: (type GObject.Object): the object whose signal handlers are sought.
2949 * @signal_id: the signal id.
2950 * @detail: the detail.
2951 * @may_be_blocked: whether blocked handlers should count as match.
2953 * Returns whether there are any handlers connected to @instance for the
2954 * given signal id and detail.
2956 * If @detail is 0 then it will only match handlers that were connected
2957 * without detail. If @detail is non-zero then it will match handlers
2958 * connected both without detail and with the given detail. This is
2959 * consistent with how a signal emitted with @detail would be delivered
2960 * to those handlers.
2962 * Since 2.46 this also checks for a non-default class closure being
2963 * installed, as this is basically always what you want.
2965 * One example of when you might use this is when the arguments to the
2966 * signal are difficult to compute. A class implementor may opt to not
2967 * emit the signal if no one is attached anyway, thus saving the cost
2968 * of building the arguments.
2970 * Returns: %TRUE if a handler is connected to the signal, %FALSE
2974 g_signal_has_handler_pending (gpointer instance
,
2977 gboolean may_be_blocked
)
2979 HandlerMatch
*mlist
;
2980 gboolean has_pending
;
2983 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance
), FALSE
);
2984 g_return_val_if_fail (signal_id
> 0, FALSE
);
2988 node
= LOOKUP_SIGNAL_NODE (signal_id
);
2991 if (!(node
->flags
& G_SIGNAL_DETAILED
))
2993 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC
, signal_id
, detail
);
2998 mlist
= handlers_find (instance
,
2999 (G_SIGNAL_MATCH_ID
| G_SIGNAL_MATCH_DETAIL
| (may_be_blocked
? 0 : G_SIGNAL_MATCH_UNBLOCKED
)),
3000 signal_id
, detail
, NULL
, NULL
, NULL
, TRUE
);
3004 handler_match_free1_R (mlist
, instance
);
3008 ClassClosure
*class_closure
= signal_find_class_closure (node
, G_TYPE_FROM_INSTANCE (instance
));
3009 if (class_closure
!= NULL
&& class_closure
->instance_type
!= 0)
3012 has_pending
= FALSE
;
3021 * @instance_and_params: (array): argument list for the signal emission.
3022 * The first element in the array is a #GValue for the instance the signal
3023 * is being emitted on. The rest are any arguments to be passed to the signal.
3024 * @signal_id: the signal id
3025 * @detail: the detail
3026 * @return_value: (inout) (optional): Location to
3027 * store the return value of the signal emission. This must be provided if the
3028 * specified signal returns a value, but may be ignored otherwise.
3032 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
3033 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
3036 g_signal_emitv (const GValue
*instance_and_params
,
3039 GValue
*return_value
)
3043 #ifdef G_ENABLE_DEBUG
3044 const GValue
*param_values
;
3048 g_return_if_fail (instance_and_params
!= NULL
);
3049 instance
= g_value_peek_pointer (instance_and_params
);
3050 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
3051 g_return_if_fail (signal_id
> 0);
3053 #ifdef G_ENABLE_DEBUG
3054 param_values
= instance_and_params
+ 1;
3058 node
= LOOKUP_SIGNAL_NODE (signal_id
);
3059 if (!node
|| !g_type_is_a (G_TYPE_FROM_INSTANCE (instance
), node
->itype
))
3061 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC
, signal_id
, instance
);
3065 #ifdef G_ENABLE_DEBUG
3066 if (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
3068 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC
, signal_id
, detail
);
3072 for (i
= 0; i
< node
->n_params
; i
++)
3073 if (!G_TYPE_CHECK_VALUE_TYPE (param_values
+ i
, node
->param_types
[i
] & ~G_SIGNAL_TYPE_STATIC_SCOPE
))
3075 g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
3077 type_debug_name (node
->param_types
[i
]),
3080 G_VALUE_TYPE_NAME (param_values
+ i
));
3084 if (node
->return_type
!= G_TYPE_NONE
)
3088 g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3090 type_debug_name (node
->return_type
),
3095 else if (!node
->accumulator
&& !G_TYPE_CHECK_VALUE_TYPE (return_value
, node
->return_type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
))
3097 g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3099 type_debug_name (node
->return_type
),
3101 G_VALUE_TYPE_NAME (return_value
));
3107 return_value
= NULL
;
3108 #endif /* G_ENABLE_DEBUG */
3110 /* optimize NOP emissions */
3111 if (!node
->single_va_closure_is_valid
)
3112 node_update_single_va_closure (node
);
3114 if (node
->single_va_closure
!= NULL
&&
3115 (node
->single_va_closure
== SINGLE_VA_CLOSURE_EMPTY_MAGIC
||
3116 _g_closure_is_void (node
->single_va_closure
, instance
)))
3118 HandlerList
* hlist
= handler_list_lookup (node
->signal_id
, instance
);
3119 if (hlist
== NULL
|| hlist
->handlers
== NULL
)
3121 /* nothing to do to emit this signal */
3123 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3129 signal_emit_unlocked_R (node
, detail
, instance
, return_value
, instance_and_params
);
3132 static inline gboolean
3133 accumulate (GSignalInvocationHint
*ihint
,
3134 GValue
*return_accu
,
3135 GValue
*handler_return
,
3136 SignalAccumulator
*accumulator
)
3138 gboolean continue_emission
;
3143 continue_emission
= accumulator
->func (ihint
, return_accu
, handler_return
, accumulator
->data
);
3144 g_value_reset (handler_return
);
3146 return continue_emission
;
3150 * g_signal_emit_valist: (skip)
3151 * @instance: (type GObject.TypeInstance): the instance the signal is being
3153 * @signal_id: the signal id
3154 * @detail: the detail
3155 * @var_args: a list of parameters to be passed to the signal, followed by a
3156 * location for the return value. If the return type of the signal
3157 * is #G_TYPE_NONE, the return value location can be omitted.
3161 * Note that g_signal_emit_valist() resets the return value to the default
3162 * if no handlers are connected, in contrast to g_signal_emitv().
3165 g_signal_emit_valist (gpointer instance
,
3170 GValue
*instance_and_params
;
3171 GType signal_return_type
;
3172 GValue
*param_values
;
3176 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
3177 g_return_if_fail (signal_id
> 0);
3180 node
= LOOKUP_SIGNAL_NODE (signal_id
);
3181 if (!node
|| !g_type_is_a (G_TYPE_FROM_INSTANCE (instance
), node
->itype
))
3183 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC
, signal_id
, instance
);
3187 #ifndef G_DISABLE_CHECKS
3188 if (detail
&& !(node
->flags
& G_SIGNAL_DETAILED
))
3190 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC
, signal_id
, detail
);
3194 #endif /* !G_DISABLE_CHECKS */
3196 if (!node
->single_va_closure_is_valid
)
3197 node_update_single_va_closure (node
);
3199 if (node
->single_va_closure
!= NULL
)
3201 HandlerList
* hlist
= handler_list_lookup (node
->signal_id
, instance
);
3202 Handler
*fastpath_handler
= NULL
;
3204 GClosure
*closure
= NULL
;
3205 gboolean fastpath
= TRUE
;
3206 GSignalFlags run_type
= G_SIGNAL_RUN_FIRST
;
3208 if (node
->single_va_closure
!= SINGLE_VA_CLOSURE_EMPTY_MAGIC
&&
3209 !_g_closure_is_void (node
->single_va_closure
, instance
))
3211 if (_g_closure_supports_invoke_va (node
->single_va_closure
))
3213 closure
= node
->single_va_closure
;
3214 if (node
->single_va_closure_is_after
)
3215 run_type
= G_SIGNAL_RUN_LAST
;
3217 run_type
= G_SIGNAL_RUN_FIRST
;
3223 for (l
= hlist
? hlist
->handlers
: NULL
; fastpath
&& l
!= NULL
; l
= l
->next
)
3225 if (!l
->block_count
&&
3226 (!l
->detail
|| l
->detail
== detail
))
3228 if (closure
!= NULL
|| !_g_closure_supports_invoke_va (l
->closure
))
3235 fastpath_handler
= l
;
3236 closure
= l
->closure
;
3238 run_type
= G_SIGNAL_RUN_LAST
;
3240 run_type
= G_SIGNAL_RUN_FIRST
;
3245 if (fastpath
&& closure
== NULL
&& node
->return_type
== G_TYPE_NONE
)
3251 /* Don't allow no-recurse emission as we might have to restart, which means
3252 we will run multiple handlers and thus must ref all arguments */
3253 if (closure
!= NULL
&& (node
->flags
& (G_SIGNAL_NO_RECURSE
)) != 0)
3258 SignalAccumulator
*accumulator
;
3260 GValue
*return_accu
, accu
= G_VALUE_INIT
;
3262 GType instance_type
= G_TYPE_FROM_INSTANCE (instance
);
3263 GValue emission_return
= G_VALUE_INIT
;
3264 GType rtype
= node
->return_type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
;
3265 gboolean static_scope
= node
->return_type
& G_SIGNAL_TYPE_STATIC_SCOPE
;
3267 signal_id
= node
->signal_id
;
3268 accumulator
= node
->accumulator
;
3269 if (rtype
== G_TYPE_NONE
)
3271 else if (accumulator
)
3272 return_accu
= &accu
;
3274 return_accu
= &emission_return
;
3276 emission
.instance
= instance
;
3277 emission
.ihint
.signal_id
= signal_id
;
3278 emission
.ihint
.detail
= detail
;
3279 emission
.ihint
.run_type
= run_type
;
3280 emission
.state
= EMISSION_RUN
;
3281 emission
.chain_type
= instance_type
;
3282 emission_push (&emission
);
3284 if (fastpath_handler
)
3285 handler_ref (fastpath_handler
);
3289 TRACE(GOBJECT_SIGNAL_EMIT(signal_id
, detail
, instance
, instance_type
));
3291 if (rtype
!= G_TYPE_NONE
)
3292 g_value_init (&emission_return
, rtype
);
3295 g_value_init (&accu
, rtype
);
3297 if (closure
!= NULL
)
3299 g_object_ref (instance
);
3300 _g_closure_invoke_va (closure
,
3306 accumulate (&emission
.ihint
, &emission_return
, &accu
, accumulator
);
3311 emission
.chain_type
= G_TYPE_NONE
;
3312 emission_pop (&emission
);
3314 if (fastpath_handler
)
3315 handler_unref_R (signal_id
, instance
, fastpath_handler
);
3320 g_value_unset (&accu
);
3322 if (rtype
!= G_TYPE_NONE
)
3324 gchar
*error
= NULL
;
3325 for (i
= 0; i
< node
->n_params
; i
++)
3327 GType ptype
= node
->param_types
[i
] & ~G_SIGNAL_TYPE_STATIC_SCOPE
;
3328 G_VALUE_COLLECT_SKIP (ptype
, var_args
);
3331 G_VALUE_LCOPY (&emission_return
,
3333 static_scope
? G_VALUE_NOCOPY_CONTENTS
: 0,
3336 g_value_unset (&emission_return
);
3339 g_warning ("%s: %s", G_STRLOC
, error
);
3341 /* we purposely leak the value here, it might not be
3342 * in a sane state if an error condition occurred
3347 TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id
, detail
, instance
, instance_type
));
3349 if (closure
!= NULL
)
3350 g_object_unref (instance
);
3357 n_params
= node
->n_params
;
3358 signal_return_type
= node
->return_type
;
3359 instance_and_params
= g_alloca (sizeof (GValue
) * (n_params
+ 1));
3360 memset (instance_and_params
, 0, sizeof (GValue
) * (n_params
+ 1));
3361 param_values
= instance_and_params
+ 1;
3363 for (i
= 0; i
< node
->n_params
; i
++)
3366 GType ptype
= node
->param_types
[i
] & ~G_SIGNAL_TYPE_STATIC_SCOPE
;
3367 gboolean static_scope
= node
->param_types
[i
] & G_SIGNAL_TYPE_STATIC_SCOPE
;
3369 G_VALUE_COLLECT_INIT (param_values
+ i
, ptype
,
3371 static_scope
? G_VALUE_NOCOPY_CONTENTS
: 0,
3375 g_warning ("%s: %s", G_STRLOC
, error
);
3378 /* we purposely leak the value here, it might not be
3379 * in a sane state if an error condition occoured
3382 g_value_unset (param_values
+ i
);
3388 instance_and_params
->g_type
= 0;
3389 g_value_init_from_instance (instance_and_params
, instance
);
3390 if (signal_return_type
== G_TYPE_NONE
)
3391 signal_emit_unlocked_R (node
, detail
, instance
, NULL
, instance_and_params
);
3394 GValue return_value
= G_VALUE_INIT
;
3395 gchar
*error
= NULL
;
3396 GType rtype
= signal_return_type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
;
3397 gboolean static_scope
= signal_return_type
& G_SIGNAL_TYPE_STATIC_SCOPE
;
3399 g_value_init (&return_value
, rtype
);
3401 signal_emit_unlocked_R (node
, detail
, instance
, &return_value
, instance_and_params
);
3403 G_VALUE_LCOPY (&return_value
,
3405 static_scope
? G_VALUE_NOCOPY_CONTENTS
: 0,
3408 g_value_unset (&return_value
);
3411 g_warning ("%s: %s", G_STRLOC
, error
);
3414 /* we purposely leak the value here, it might not be
3415 * in a sane state if an error condition occurred
3419 for (i
= 0; i
< n_params
; i
++)
3420 g_value_unset (param_values
+ i
);
3421 g_value_unset (instance_and_params
);
3426 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3427 * @signal_id: the signal id
3428 * @detail: the detail
3429 * @...: parameters to be passed to the signal, followed by a
3430 * location for the return value. If the return type of the signal
3431 * is #G_TYPE_NONE, the return value location can be omitted.
3435 * Note that g_signal_emit() resets the return value to the default
3436 * if no handlers are connected, in contrast to g_signal_emitv().
3439 g_signal_emit (gpointer instance
,
3446 va_start (var_args
, detail
);
3447 g_signal_emit_valist (instance
, signal_id
, detail
, var_args
);
3452 * g_signal_emit_by_name:
3453 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3454 * @detailed_signal: a string of the form "signal-name::detail".
3455 * @...: parameters to be passed to the signal, followed by a
3456 * location for the return value. If the return type of the signal
3457 * is #G_TYPE_NONE, the return value location can be omitted.
3461 * Note that g_signal_emit_by_name() resets the return value to the default
3462 * if no handlers are connected, in contrast to g_signal_emitv().
3465 g_signal_emit_by_name (gpointer instance
,
3466 const gchar
*detailed_signal
,
3473 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance
));
3474 g_return_if_fail (detailed_signal
!= NULL
);
3476 itype
= G_TYPE_FROM_INSTANCE (instance
);
3479 signal_id
= signal_parse_name (detailed_signal
, itype
, &detail
, TRUE
);
3486 va_start (var_args
, detailed_signal
);
3487 g_signal_emit_valist (instance
, signal_id
, detail
, var_args
);
3491 g_warning ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3492 G_STRLOC
, detailed_signal
, instance
, g_type_name (itype
));
3496 signal_emit_unlocked_R (SignalNode
*node
,
3499 GValue
*emission_return
,
3500 const GValue
*instance_and_params
)
3502 SignalAccumulator
*accumulator
;
3504 GClosure
*class_closure
;
3506 Handler
*handler_list
= NULL
;
3507 GValue
*return_accu
, accu
= G_VALUE_INIT
;
3509 gulong max_sequential_handler_number
;
3510 gboolean return_value_altered
= FALSE
;
3512 TRACE(GOBJECT_SIGNAL_EMIT(node
->signal_id
, detail
, instance
, G_TYPE_FROM_INSTANCE (instance
)));
3515 signal_id
= node
->signal_id
;
3517 if (node
->flags
& G_SIGNAL_NO_RECURSE
)
3519 Emission
*node
= emission_find (signal_id
, detail
, instance
);
3523 node
->state
= EMISSION_RESTART
;
3525 return return_value_altered
;
3528 accumulator
= node
->accumulator
;
3532 g_value_init (&accu
, node
->return_type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
);
3533 return_accu
= &accu
;
3537 return_accu
= emission_return
;
3538 emission
.instance
= instance
;
3539 emission
.ihint
.signal_id
= node
->signal_id
;
3540 emission
.ihint
.detail
= detail
;
3541 emission
.ihint
.run_type
= 0;
3543 emission
.chain_type
= G_TYPE_NONE
;
3544 emission_push (&emission
);
3545 class_closure
= signal_lookup_closure (node
, instance
);
3550 handler_unref_R (signal_id
, instance
, handler_list
);
3551 max_sequential_handler_number
= g_handler_sequential_number
;
3552 hlist
= handler_list_lookup (signal_id
, instance
);
3553 handler_list
= hlist
? hlist
->handlers
: NULL
;
3555 handler_ref (handler_list
);
3557 emission
.ihint
.run_type
= G_SIGNAL_RUN_FIRST
;
3559 if ((node
->flags
& G_SIGNAL_RUN_FIRST
) && class_closure
)
3561 emission
.state
= EMISSION_RUN
;
3563 emission
.chain_type
= G_TYPE_FROM_INSTANCE (instance
);
3565 g_closure_invoke (class_closure
,
3568 instance_and_params
,
3570 if (!accumulate (&emission
.ihint
, emission_return
, &accu
, accumulator
) &&
3571 emission
.state
== EMISSION_RUN
)
3572 emission
.state
= EMISSION_STOP
;
3574 emission
.chain_type
= G_TYPE_NONE
;
3575 return_value_altered
= TRUE
;
3577 if (emission
.state
== EMISSION_STOP
)
3579 else if (emission
.state
== EMISSION_RESTART
)
3583 if (node
->emission_hooks
)
3585 gboolean need_destroy
, was_in_call
, may_recurse
= TRUE
;
3588 emission
.state
= EMISSION_HOOK
;
3589 hook
= g_hook_first_valid (node
->emission_hooks
, may_recurse
);
3592 SignalHook
*signal_hook
= SIGNAL_HOOK (hook
);
3594 if (!signal_hook
->detail
|| signal_hook
->detail
== detail
)
3596 GSignalEmissionHook hook_func
= (GSignalEmissionHook
) hook
->func
;
3598 was_in_call
= G_HOOK_IN_CALL (hook
);
3599 hook
->flags
|= G_HOOK_FLAG_IN_CALL
;
3601 need_destroy
= !hook_func (&emission
.ihint
, node
->n_params
+ 1, instance_and_params
, hook
->data
);
3604 hook
->flags
&= ~G_HOOK_FLAG_IN_CALL
;
3606 g_hook_destroy_link (node
->emission_hooks
, hook
);
3608 hook
= g_hook_next_valid (node
->emission_hooks
, hook
, may_recurse
);
3611 if (emission
.state
== EMISSION_RESTART
)
3617 Handler
*handler
= handler_list
;
3619 emission
.state
= EMISSION_RUN
;
3620 handler_ref (handler
);
3627 handler_unref_R (signal_id
, instance
, handler_list
);
3628 handler_list
= handler
;
3631 else if (!handler
->block_count
&& (!handler
->detail
|| handler
->detail
== detail
) &&
3632 handler
->sequential_number
< max_sequential_handler_number
)
3635 g_closure_invoke (handler
->closure
,
3638 instance_and_params
,
3640 if (!accumulate (&emission
.ihint
, emission_return
, &accu
, accumulator
) &&
3641 emission
.state
== EMISSION_RUN
)
3642 emission
.state
= EMISSION_STOP
;
3644 return_value_altered
= TRUE
;
3646 tmp
= emission
.state
== EMISSION_RUN
? handler
->next
: NULL
;
3649 tmp
= handler
->next
;
3653 handler_unref_R (signal_id
, instance
, handler_list
);
3654 handler_list
= handler
;
3659 if (emission
.state
== EMISSION_STOP
)
3661 else if (emission
.state
== EMISSION_RESTART
)
3665 emission
.ihint
.run_type
= G_SIGNAL_RUN_LAST
;
3667 if ((node
->flags
& G_SIGNAL_RUN_LAST
) && class_closure
)
3669 emission
.state
= EMISSION_RUN
;
3671 emission
.chain_type
= G_TYPE_FROM_INSTANCE (instance
);
3673 g_closure_invoke (class_closure
,
3676 instance_and_params
,
3678 if (!accumulate (&emission
.ihint
, emission_return
, &accu
, accumulator
) &&
3679 emission
.state
== EMISSION_RUN
)
3680 emission
.state
= EMISSION_STOP
;
3682 emission
.chain_type
= G_TYPE_NONE
;
3683 return_value_altered
= TRUE
;
3685 if (emission
.state
== EMISSION_STOP
)
3687 else if (emission
.state
== EMISSION_RESTART
)
3693 Handler
*handler
= handler_list
;
3695 emission
.state
= EMISSION_RUN
;
3696 handler_ref (handler
);
3701 if (handler
->after
&& !handler
->block_count
&& (!handler
->detail
|| handler
->detail
== detail
) &&
3702 handler
->sequential_number
< max_sequential_handler_number
)
3705 g_closure_invoke (handler
->closure
,
3708 instance_and_params
,
3710 if (!accumulate (&emission
.ihint
, emission_return
, &accu
, accumulator
) &&
3711 emission
.state
== EMISSION_RUN
)
3712 emission
.state
= EMISSION_STOP
;
3714 return_value_altered
= TRUE
;
3716 tmp
= emission
.state
== EMISSION_RUN
? handler
->next
: NULL
;
3719 tmp
= handler
->next
;
3723 handler_unref_R (signal_id
, instance
, handler
);
3728 if (emission
.state
== EMISSION_STOP
)
3730 else if (emission
.state
== EMISSION_RESTART
)
3736 emission
.ihint
.run_type
= G_SIGNAL_RUN_CLEANUP
;
3738 if ((node
->flags
& G_SIGNAL_RUN_CLEANUP
) && class_closure
)
3740 gboolean need_unset
= FALSE
;
3742 emission
.state
= EMISSION_STOP
;
3744 emission
.chain_type
= G_TYPE_FROM_INSTANCE (instance
);
3746 if (node
->return_type
!= G_TYPE_NONE
&& !accumulator
)
3748 g_value_init (&accu
, node
->return_type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
);
3751 g_closure_invoke (class_closure
,
3752 node
->return_type
!= G_TYPE_NONE
? &accu
: NULL
,
3754 instance_and_params
,
3757 g_value_unset (&accu
);
3759 emission
.chain_type
= G_TYPE_NONE
;
3761 if (emission
.state
== EMISSION_RESTART
)
3766 handler_unref_R (signal_id
, instance
, handler_list
);
3768 emission_pop (&emission
);
3771 g_value_unset (&accu
);
3773 TRACE(GOBJECT_SIGNAL_EMIT_END(node
->signal_id
, detail
, instance
, G_TYPE_FROM_INSTANCE (instance
)));
3775 return return_value_altered
;
3779 add_invalid_closure_notify (Handler
*handler
,
3782 g_closure_add_invalidate_notifier (handler
->closure
, instance
, invalid_closure_notify
);
3783 handler
->has_invalid_closure_notify
= 1;
3787 remove_invalid_closure_notify (Handler
*handler
,
3790 if (handler
->has_invalid_closure_notify
)
3792 g_closure_remove_invalidate_notifier (handler
->closure
, instance
, invalid_closure_notify
);
3793 handler
->has_invalid_closure_notify
= 0;
3798 invalid_closure_notify (gpointer instance
,
3806 handler
= handler_lookup (instance
, 0, closure
, &signal_id
);
3807 g_assert (handler
->closure
== closure
);
3809 handler
->sequential_number
= 0;
3810 handler
->block_count
= 1;
3811 handler_unref_R (signal_id
, instance
, handler
);
3817 type_debug_name (GType type
)
3821 const char *name
= g_type_name (type
& ~G_SIGNAL_TYPE_STATIC_SCOPE
);
3822 return name
? name
: "<unknown>";
3829 * g_signal_accumulator_true_handled:
3830 * @ihint: standard #GSignalAccumulator parameter
3831 * @return_accu: standard #GSignalAccumulator parameter
3832 * @handler_return: standard #GSignalAccumulator parameter
3833 * @dummy: standard #GSignalAccumulator parameter
3835 * A predefined #GSignalAccumulator for signals that return a
3836 * boolean values. The behavior that this accumulator gives is
3837 * that a return of %TRUE stops the signal emission: no further
3838 * callbacks will be invoked, while a return of %FALSE allows
3839 * the emission to continue. The idea here is that a %TRUE return
3840 * indicates that the callback handled the signal, and no further
3841 * handling is needed.
3845 * Returns: standard #GSignalAccumulator result
3848 g_signal_accumulator_true_handled (GSignalInvocationHint
*ihint
,
3849 GValue
*return_accu
,
3850 const GValue
*handler_return
,
3853 gboolean continue_emission
;
3854 gboolean signal_handled
;
3856 signal_handled
= g_value_get_boolean (handler_return
);
3857 g_value_set_boolean (return_accu
, signal_handled
);
3858 continue_emission
= !signal_handled
;
3860 return continue_emission
;
3864 * g_signal_accumulator_first_wins:
3865 * @ihint: standard #GSignalAccumulator parameter
3866 * @return_accu: standard #GSignalAccumulator parameter
3867 * @handler_return: standard #GSignalAccumulator parameter
3868 * @dummy: standard #GSignalAccumulator parameter
3870 * A predefined #GSignalAccumulator for signals intended to be used as a
3871 * hook for application code to provide a particular value. Usually
3872 * only one such value is desired and multiple handlers for the same
3873 * signal don't make much sense (except for the case of the default
3874 * handler defined in the class structure, in which case you will
3875 * usually want the signal connection to override the class handler).
3877 * This accumulator will use the return value from the first signal
3878 * handler that is run as the return value for the signal and not run
3879 * any further handlers (ie: the first handler "wins").
3881 * Returns: standard #GSignalAccumulator result
3886 g_signal_accumulator_first_wins (GSignalInvocationHint
*ihint
,
3887 GValue
*return_accu
,
3888 const GValue
*handler_return
,
3891 g_value_copy (handler_return
, return_accu
);