Increase the timeout for some GLib tests
[glib.git] / gobject / gsignal.c
blobb22dfcca844fe5028813b0633f9e7d1a9789eb2a
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.1 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>
22 * MT safe
25 #include "config.h"
27 #include <string.h>
28 #include <signal.h>
30 #include "gsignal.h"
31 #include "gtype-private.h"
32 #include "gbsearcharray.h"
33 #include "gvaluecollector.h"
34 #include "gvaluetypes.h"
35 #include "gobject.h"
36 #include "genums.h"
37 #include "gobject_trace.h"
40 /**
41 * SECTION:signals
42 * @short_description: A means for customization of object behaviour
43 * and a general purpose notification mechanism
44 * @title: Signals
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
50 * that is inherited.
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
72 * flag is not set)
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
81 * connected in.
83 * All handlers may prematurely stop a signal emission, and any number of
84 * handlers may be connected, disconnected, blocked or unblocked during
85 * a signal emission.
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;
138 typedef enum
140 EMISSION_STOP,
141 EMISSION_RUN,
142 EMISSION_HOOK,
143 EMISSION_RESTART
144 } EmissionState;
147 /* --- prototypes --- */
148 static inline guint signal_id_lookup (GQuark quark,
149 GType itype);
150 static void signal_destroy_R (SignalNode *signal_node);
151 static inline HandlerList* handler_list_ensure (guint signal_id,
152 gpointer instance);
153 static inline HandlerList* handler_list_lookup (guint signal_id,
154 gpointer instance);
155 static inline Handler* handler_new (guint signal_id,
156 gpointer instance,
157 gboolean after);
158 static void handler_insert (guint signal_id,
159 gpointer instance,
160 Handler *handler);
161 static Handler* handler_lookup (gpointer instance,
162 gulong handler_id,
163 GClosure *closure,
164 guint *signal_id_p);
165 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
166 Handler *handler,
167 guint signal_id);
168 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
169 gpointer instance);
170 static HandlerMatch* handlers_find (gpointer instance,
171 GSignalMatchType mask,
172 guint signal_id,
173 GQuark detail,
174 GClosure *closure,
175 gpointer func,
176 gpointer data,
177 gboolean one_and_only);
178 static inline void handler_ref (Handler *handler);
179 static inline void handler_unref_R (guint signal_id,
180 gpointer instance,
181 Handler *handler);
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,
187 GQuark detail,
188 gpointer instance);
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,
194 GQuark detail,
195 gpointer instance,
196 GValue *return_value,
197 const GValue *instance_and_params);
198 static void add_invalid_closure_notify (Handler *handler,
199 gpointer instance);
200 static void remove_invalid_closure_notify (Handler *handler,
201 gpointer instance);
202 static void invalid_closure_notify (gpointer data,
203 GClosure *closure);
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 --- */
210 typedef struct
212 GSignalAccumulator func;
213 gpointer data;
214 } SignalAccumulator;
215 typedef struct
217 GHook hook;
218 GQuark detail;
219 } SignalHook;
220 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
222 struct _SignalNode
224 /* permanent portion */
225 guint signal_id;
226 GType itype;
227 const gchar *name;
228 guint destroyed : 1;
230 /* reinitializable portion */
231 guint flags : 9;
232 guint n_params : 8;
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 */
248 struct _SignalKey
250 GType itype;
251 GQuark quark;
252 guint signal_id;
255 struct _Emission
257 Emission *next;
258 gpointer instance;
259 GSignalInvocationHint ihint;
260 EmissionState state;
261 GType chain_type;
264 struct _HandlerList
266 guint signal_id;
267 Handler *handlers;
268 Handler *tail_before; /* normal signal handlers are appended here */
269 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
272 struct _Handler
274 gulong sequential_number;
275 Handler *next;
276 Handler *prev;
277 GQuark detail;
278 guint signal_id;
279 guint ref_count;
280 guint block_count : 16;
281 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
282 guint after : 1;
283 guint has_invalid_closure_notify : 1;
284 GClosure *closure;
285 gpointer instance;
287 struct _HandlerMatch
289 Handler *handler;
290 HandlerMatch *next;
291 guint signal_id;
294 typedef struct
296 GType instance_type; /* 0 for default closure */
297 GClosure *closure;
298 } ClassClosure;
301 /* --- variables --- */
302 static GBSearchArray *g_signal_key_bsa = NULL;
303 static const GBSearchConfig g_signal_key_bconfig = {
304 sizeof (SignalKey),
305 signal_key_cmp,
306 G_BSEARCH_ARRAY_ALIGN_POWER2,
308 static GBSearchConfig g_signal_hlbsa_bconfig = {
309 sizeof (HandlerList),
310 handler_lists_cmp,
313 static GBSearchConfig g_class_closure_bconfig = {
314 sizeof (ClassClosure),
315 class_closures_cmp,
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];
337 else
338 return NULL;
342 /* --- functions --- */
343 static inline guint
344 signal_id_lookup (GQuark quark,
345 GType itype)
347 GType *ifaces, type = itype;
348 SignalKey key;
349 guint n_ifaces;
351 key.quark = quark;
353 /* try looking up signals for this type and its ancestors */
356 SignalKey *signal_key;
358 key.itype = type;
359 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
361 if (signal_key)
362 return signal_key->signal_id;
364 type = g_type_parent (type);
366 while (type);
368 /* no luck, try interfaces it exports */
369 ifaces = g_type_interfaces (itype, &n_ifaces);
370 while (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);
377 if (signal_key)
379 g_free (ifaces);
380 return signal_key->signal_id;
383 g_free (ifaces);
385 return 0;
388 static gint
389 class_closures_cmp (gconstpointer node1,
390 gconstpointer node2)
392 const ClassClosure *c1 = node1, *c2 = node2;
394 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
397 static gint
398 handler_lists_cmp (gconstpointer node1,
399 gconstpointer node2)
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,
408 gpointer instance)
410 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
411 HandlerList key;
413 key.signal_id = signal_id;
414 key.handlers = NULL;
415 key.tail_before = NULL;
416 key.tail_after = NULL;
417 if (!hlbsa)
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);
423 else
425 GBSearchArray *o = hlbsa;
427 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
428 if (hlbsa != o)
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,
436 gpointer instance)
438 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
439 HandlerList key;
441 key.signal_id = signal_id;
443 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
446 static guint
447 handler_hash (gconstpointer key)
449 return (guint)((Handler*)key)->sequential_number;
452 static gboolean
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);
461 static Handler*
462 handler_lookup (gpointer instance,
463 gulong handler_id,
464 GClosure *closure,
465 guint *signal_id_p)
467 GBSearchArray *hlbsa;
469 if (handler_id)
471 Handler key;
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);
480 if (hlbsa)
482 guint i;
484 for (i = 0; i < hlbsa->n_nodes; i++)
486 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
487 Handler *handler;
489 for (handler = hlist->handlers; handler; handler = handler->next)
490 if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id))
492 if (signal_id_p)
493 *signal_id_p = hlist->signal_id;
495 return handler;
500 return NULL;
503 static inline HandlerMatch*
504 handler_match_prepend (HandlerMatch *list,
505 Handler *handler,
506 guint signal_id)
508 HandlerMatch *node;
510 node = g_slice_new (HandlerMatch);
511 node->handler = handler;
512 node->next = list;
513 node->signal_id = signal_id;
514 handler_ref (handler);
516 return node;
518 static inline HandlerMatch*
519 handler_match_free1_R (HandlerMatch *node,
520 gpointer instance)
522 HandlerMatch *next = node->next;
524 handler_unref_R (node->signal_id, instance, node->handler);
525 g_slice_free (HandlerMatch, node);
527 return next;
530 static HandlerMatch*
531 handlers_find (gpointer instance,
532 GSignalMatchType mask,
533 guint signal_id,
534 GQuark detail,
535 GClosure *closure,
536 gpointer func,
537 gpointer data,
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);
545 Handler *handler;
546 SignalNode *node = NULL;
548 if (mask & G_SIGNAL_MATCH_FUNC)
550 node = LOOKUP_SIGNAL_NODE (signal_id);
551 if (!node || !node->c_marshaller)
552 return NULL;
555 mask = ~mask;
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);
567 if (one_and_only)
568 return mlist;
571 else
573 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
575 mask = ~mask;
576 if (hlbsa)
578 guint i;
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;
584 Handler *handler;
586 if (!(mask & G_SIGNAL_MATCH_FUNC))
588 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
589 if (!node->c_marshaller)
590 continue;
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);
604 if (one_and_only)
605 return mlist;
611 return mlist;
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);
621 #endif
623 handler->sequential_number = g_handler_sequential_number++;
624 handler->prev = NULL;
625 handler->next = NULL;
626 handler->detail = 0;
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);
637 return handler;
640 static inline void
641 handler_ref (Handler *handler)
643 g_return_if_fail (handler->ref_count > 0);
645 handler->ref_count++;
648 static inline void
649 handler_unref_R (guint signal_id,
650 gpointer instance,
651 Handler *handler)
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;
661 if (handler->next)
662 handler->next->prev = handler->prev;
663 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
664 handler->prev->next = handler->next;
665 else
667 hlist = handler_list_lookup (signal_id, instance);
668 g_assert (hlist != NULL);
669 hlist->handlers = handler->next;
672 if (instance)
674 /* check if we are removing the handler pointed to by tail_before */
675 if (!handler->after && (!handler->next || handler->next->after))
677 if (!hlist)
678 hlist = handler_list_lookup (signal_id, instance);
679 if (hlist)
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 */
687 if (!handler->next)
689 if (!hlist)
690 hlist = handler_list_lookup (signal_id, instance);
691 if (hlist)
693 g_assert (hlist->tail_after == handler); /* paranoid */
694 hlist->tail_after = handler->prev;
699 SIGNAL_UNLOCK ();
700 g_closure_unref (handler->closure);
701 SIGNAL_LOCK ();
702 g_slice_free (Handler, handler);
706 static void
707 handler_insert (guint signal_id,
708 gpointer instance,
709 Handler *handler)
711 HandlerList *hlist;
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;
719 if (!handler->after)
720 hlist->tail_before = handler;
722 else if (handler->after)
724 handler->prev = hlist->tail_after;
725 hlist->tail_after->next = handler;
727 else
729 if (hlist->tail_before)
731 handler->next = hlist->tail_before->next;
732 if (handler->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;
740 if (handler->next)
741 handler->next->prev = handler;
742 hlist->handlers = handler;
744 hlist->tail_before = handler;
747 if (!handler->next)
748 hlist->tail_after = handler;
751 static void
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;
763 ClassClosure * cc;
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;
792 static inline void
793 emission_push (Emission *emission)
795 emission->next = g_emissions;
796 g_emissions = emission;
799 static inline void
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)
807 if (last)
808 last->next = node->next;
809 else
810 g_emissions = node->next;
811 return;
813 g_assert_not_reached ();
816 static inline Emission*
817 emission_find (guint signal_id,
818 GQuark detail,
819 gpointer instance)
821 Emission *emission;
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)
827 return emission;
828 return NULL;
831 static inline Emission*
832 emission_find_innermost (gpointer instance)
834 Emission *emission;
836 for (emission = g_emissions; emission; emission = emission->next)
837 if (emission->instance == instance)
838 return emission;
840 return NULL;
843 static gint
844 signal_key_cmp (gconstpointer node1,
845 gconstpointer node2)
847 const SignalKey *key1 = node1, *key2 = node2;
849 if (key1->itype == key2->itype)
850 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
851 else
852 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
855 void
856 _g_signal_init (void)
858 SIGNAL_LOCK ();
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);
871 SIGNAL_UNLOCK ();
874 void
875 _g_signals_destroy (GType itype)
877 guint i;
879 SIGNAL_LOCK ();
880 for (i = 1; i < g_n_signal_nodes; i++)
882 SignalNode *node = g_signal_nodes[i];
884 if (node->itype == itype)
886 if (node->destroyed)
887 g_warning (G_STRLOC ": signal \"%s\" of type '%s' already destroyed",
888 node->name,
889 type_debug_name (node->itype));
890 else
891 signal_destroy_R (node);
894 SIGNAL_UNLOCK ();
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"
907 * flag).
909 * Prints a warning if used on a signal which isn't being emitted.
911 void
912 g_signal_stop_emission (gpointer instance,
913 guint signal_id,
914 GQuark detail)
916 SignalNode *node;
918 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
919 g_return_if_fail (signal_id > 0);
921 SIGNAL_LOCK ();
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);
926 SIGNAL_UNLOCK ();
927 return;
929 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
931 Emission *emission = emission_find (signal_id, detail, instance);
933 if (emission)
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;
941 else
942 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
943 node->name, instance);
945 else
946 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
947 SIGNAL_UNLOCK ();
950 static void
951 signal_finalize_hook (GHookList *hook_list,
952 GHook *hook)
954 GDestroyNotify destroy = hook->destroy;
956 if (destroy)
958 hook->destroy = NULL;
959 SIGNAL_UNLOCK ();
960 destroy (hook->data);
961 SIGNAL_LOCK ();
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().
979 gulong
980 g_signal_add_emission_hook (guint signal_id,
981 GQuark detail,
982 GSignalEmissionHook hook_func,
983 gpointer hook_data,
984 GDestroyNotify data_destroy)
986 static gulong seq_hook_id = 1;
987 SignalNode *node;
988 GHook *hook;
989 SignalHook *signal_hook;
991 g_return_val_if_fail (signal_id > 0, 0);
992 g_return_val_if_fail (hook_func != NULL, 0);
994 SIGNAL_LOCK ();
995 node = LOOKUP_SIGNAL_NODE (signal_id);
996 if (!node || node->destroyed)
998 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
999 SIGNAL_UNLOCK ();
1000 return 0;
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);
1005 SIGNAL_UNLOCK ();
1006 return 0;
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);
1011 SIGNAL_UNLOCK ();
1012 return 0;
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;
1034 SIGNAL_UNLOCK ();
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.
1047 void
1048 g_signal_remove_emission_hook (guint signal_id,
1049 gulong hook_id)
1051 SignalNode *node;
1053 g_return_if_fail (signal_id > 0);
1054 g_return_if_fail (hook_id > 0);
1056 SIGNAL_LOCK ();
1057 node = LOOKUP_SIGNAL_NODE (signal_id);
1058 if (!node || node->destroyed)
1060 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1061 goto out;
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;
1068 out:
1069 SIGNAL_UNLOCK ();
1072 static inline guint
1073 signal_parse_name (const gchar *name,
1074 GType itype,
1075 GQuark *detail_p,
1076 gboolean force_quark)
1078 const gchar *colon = strchr (name, ':');
1079 guint signal_id;
1081 if (!colon)
1083 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1084 if (signal_id && detail_p)
1085 *detail_p = 0;
1087 else if (colon[1] == ':')
1089 gchar buffer[32];
1090 guint l = colon - name;
1092 if (l < 32)
1094 memcpy (buffer, name, l);
1095 buffer[l] = 0;
1096 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
1098 else
1100 gchar *signal = g_new (gchar, l + 1);
1102 memcpy (signal, name, l);
1103 signal[l] = 0;
1104 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
1105 g_free (signal);
1108 if (signal_id && detail_p)
1109 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
1111 else
1112 signal_id = 0;
1113 return signal_id;
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.
1129 gboolean
1130 g_signal_parse_name (const gchar *detailed_signal,
1131 GType itype,
1132 guint *signal_id_p,
1133 GQuark *detail_p,
1134 gboolean force_detail_quark)
1136 SignalNode *node;
1137 GQuark detail = 0;
1138 guint signal_id;
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);
1143 SIGNAL_LOCK ();
1144 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1145 SIGNAL_UNLOCK ();
1147 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1148 if (!node || node->destroyed ||
1149 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1150 return FALSE;
1152 if (signal_id_p)
1153 *signal_id_p = signal_id;
1154 if (detail_p)
1155 *detail_p = detail;
1157 return TRUE;
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.
1170 void
1171 g_signal_stop_emission_by_name (gpointer instance,
1172 const gchar *detailed_signal)
1174 guint signal_id;
1175 GQuark detail = 0;
1176 GType itype;
1178 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1179 g_return_if_fail (detailed_signal != NULL);
1181 SIGNAL_LOCK ();
1182 itype = G_TYPE_FROM_INSTANCE (instance);
1183 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1184 if (signal_id)
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));
1193 else
1195 Emission *emission = emission_find (signal_id, detail, instance);
1197 if (emission)
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;
1205 else
1206 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1207 node->name, instance);
1210 else
1211 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1212 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1213 SIGNAL_UNLOCK ();
1217 * g_signal_lookup:
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.
1231 guint
1232 g_signal_lookup (const gchar *name,
1233 GType itype)
1235 guint signal_id;
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);
1239 SIGNAL_LOCK ();
1240 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1241 SIGNAL_UNLOCK ();
1242 if (!signal_id)
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"'",
1247 name, itype);
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));
1256 return signal_id;
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
1266 * g_signal_query().
1268 * Returns: (array length=n_ids) (transfer full): Newly allocated array of signal IDs.
1270 guint*
1271 g_signal_list_ids (GType itype,
1272 guint *n_ids)
1274 SignalKey *keys;
1275 GArray *result;
1276 guint n_nodes;
1277 guint i;
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);
1282 SIGNAL_LOCK ();
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;
1299 SIGNAL_UNLOCK ();
1300 if (!n_nodes)
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"'",
1305 itype);
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);
1318 * g_signal_name:
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.
1327 const gchar *
1328 g_signal_name (guint signal_id)
1330 SignalNode *node;
1331 const gchar *name;
1333 SIGNAL_LOCK ();
1334 node = LOOKUP_SIGNAL_NODE (signal_id);
1335 name = node ? node->name : NULL;
1336 SIGNAL_UNLOCK ();
1338 return (char*) name;
1342 * g_signal_query:
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.
1354 void
1355 g_signal_query (guint signal_id,
1356 GSignalQuery *query)
1358 SignalNode *node;
1360 g_return_if_fail (query != NULL);
1362 SIGNAL_LOCK ();
1363 node = LOOKUP_SIGNAL_NODE (signal_id);
1364 if (!node || node->destroyed)
1365 query->signal_id = 0;
1366 else
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;
1376 SIGNAL_UNLOCK ();
1380 * g_signal_new:
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
1418 guint
1419 g_signal_new (const gchar *signal_name,
1420 GType itype,
1421 GSignalFlags signal_flags,
1422 guint class_offset,
1423 GSignalAccumulator accumulator,
1424 gpointer accu_data,
1425 GSignalCMarshaller c_marshaller,
1426 GType return_type,
1427 guint n_params,
1428 ...)
1430 va_list args;
1431 guint signal_id;
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);
1442 va_end (args);
1444 return signal_id;
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
1486 * Since: 2.18
1488 guint
1489 g_signal_new_class_handler (const gchar *signal_name,
1490 GType itype,
1491 GSignalFlags signal_flags,
1492 GCallback class_handler,
1493 GSignalAccumulator accumulator,
1494 gpointer accu_data,
1495 GSignalCMarshaller c_marshaller,
1496 GType return_type,
1497 guint n_params,
1498 ...)
1500 va_list args;
1501 guint signal_id;
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);
1512 va_end (args);
1514 return signal_id;
1517 static inline ClassClosure*
1518 signal_find_class_closure (SignalNode *node,
1519 GType itype)
1521 GBSearchArray *bsa = node->class_closure_bsa;
1522 ClassClosure *cc;
1524 if (bsa)
1526 ClassClosure key;
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 */
1534 return cc;
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);
1545 else
1546 cc = NULL;
1547 return cc;
1550 static inline GClosure*
1551 signal_lookup_closure (SignalNode *node,
1552 GTypeInstance *instance)
1554 ClassClosure *cc;
1556 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1557 return cc ? cc->closure : NULL;
1560 static void
1561 signal_add_class_closure (SignalNode *node,
1562 GType itype,
1563 GClosure *closure)
1565 ClassClosure key;
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,
1575 &key);
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);
1586 * g_signal_newv:
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;
1594 * may be %NULL
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
1604 * each parameter
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
1615 guint
1616 g_signal_newv (const gchar *signal_name,
1617 GType itype,
1618 GSignalFlags signal_flags,
1619 GClosure *class_closure,
1620 GSignalAccumulator accumulator,
1621 gpointer accu_data,
1622 GSignalCMarshaller c_marshaller,
1623 GType return_type,
1624 guint n_params,
1625 GType *param_types)
1627 gchar *name;
1628 guint signal_id, i;
1629 SignalNode *node;
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);
1636 if (n_params)
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);
1641 if (!accumulator)
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 */
1647 SIGNAL_LOCK ();
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",
1654 name,
1655 type_debug_name (node->itype),
1656 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1657 g_free (name);
1658 SIGNAL_UNLOCK ();
1659 return 0;
1661 if (node && node->itype != itype)
1663 g_warning (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'",
1664 name,
1665 type_debug_name (itype),
1666 type_debug_name (node->itype));
1667 g_free (name);
1668 SIGNAL_UNLOCK ();
1669 return 0;
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);
1676 g_free (name);
1677 SIGNAL_UNLOCK ();
1678 return 0;
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);
1684 g_free (name);
1685 SIGNAL_UNLOCK ();
1686 return 0;
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));
1693 g_free (name);
1694 SIGNAL_UNLOCK ();
1695 return 0;
1698 /* setup permanent portion of signal node */
1699 if (!node)
1701 SignalKey key;
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;
1709 node->name = name;
1710 key.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;
1730 if (accumulator)
1732 node->accumulator = g_new (SignalAccumulator, 1);
1733 node->accumulator->func = accumulator;
1734 node->accumulator->data = accu_data;
1736 else
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; \
1758 if (0) {}
1759 ADD_CHECK (BOOLEAN)
1760 ADD_CHECK (CHAR)
1761 ADD_CHECK (UCHAR)
1762 ADD_CHECK (INT)
1763 ADD_CHECK (UINT)
1764 ADD_CHECK (LONG)
1765 ADD_CHECK (ULONG)
1766 ADD_CHECK (ENUM)
1767 ADD_CHECK (FLAGS)
1768 ADD_CHECK (FLOAT)
1769 ADD_CHECK (DOUBLE)
1770 ADD_CHECK (STRING)
1771 ADD_CHECK (PARAM)
1772 ADD_CHECK (BOXED)
1773 ADD_CHECK (POINTER)
1774 ADD_CHECK (OBJECT)
1775 ADD_CHECK (VARIANT)
1778 if (c_marshaller == NULL)
1780 if (builtin_c_marshaller)
1782 c_marshaller = builtin_c_marshaller;
1783 va_marshaller = builtin_va_marshaller;
1785 else
1787 c_marshaller = g_cclosure_marshal_generic;
1788 va_marshaller = g_cclosure_marshal_generic_va;
1791 else
1792 va_marshaller = NULL;
1794 node->c_marshaller = c_marshaller;
1795 node->va_marshaller = va_marshaller;
1796 node->emission_hooks = NULL;
1797 if (class_closure)
1798 signal_add_class_closure (node, 0, class_closure);
1800 SIGNAL_UNLOCK ();
1802 g_free (name);
1804 return signal_id;
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.
1818 * Since: 2.32
1820 void
1821 g_signal_set_va_marshaller (guint signal_id,
1822 GType instance_type,
1823 GSignalCVaMarshaller va_marshaller)
1825 SignalNode *node;
1827 g_return_if_fail (signal_id > 0);
1828 g_return_if_fail (va_marshaller != NULL);
1830 SIGNAL_LOCK ();
1831 node = LOOKUP_SIGNAL_NODE (signal_id);
1832 if (node)
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;
1845 SIGNAL_UNLOCK ();
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
1876 guint
1877 g_signal_new_valist (const gchar *signal_name,
1878 GType itype,
1879 GSignalFlags signal_flags,
1880 GClosure *class_closure,
1881 GSignalAccumulator accumulator,
1882 gpointer accu_data,
1883 GSignalCMarshaller c_marshaller,
1884 GType return_type,
1885 guint n_params,
1886 va_list args)
1888 GType *param_types;
1889 guint i;
1890 guint signal_id;
1892 if (n_params > 0)
1894 param_types = g_new (GType, n_params);
1896 for (i = 0; i < n_params; i++)
1897 param_types[i] = va_arg (args, GType);
1899 else
1900 param_types = NULL;
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);
1907 return signal_id;
1910 static void
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 */
1931 Emission *emission;
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);
1938 #endif
1940 /* free contents that need to
1942 SIGNAL_UNLOCK ();
1943 g_free (node.param_types);
1944 if (node.class_closure_bsa)
1946 guint i;
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);
1962 SIGNAL_LOCK ();
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
1969 * for the signal.
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.
1980 void
1981 g_signal_override_class_closure (guint signal_id,
1982 GType instance_type,
1983 GClosure *class_closure)
1985 SignalNode *node;
1987 g_return_if_fail (signal_id > 0);
1988 g_return_if_fail (class_closure != NULL);
1990 SIGNAL_LOCK ();
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);
1995 else
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);
2001 else
2002 signal_add_class_closure (node, instance_type, class_closure);
2004 SIGNAL_UNLOCK ();
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
2011 * for the signal.
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.
2023 * Since: 2.18
2025 void
2026 g_signal_override_class_handler (const gchar *signal_name,
2027 GType instance_type,
2028 GCallback class_handler)
2030 guint signal_id;
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);
2038 if (signal_id)
2039 g_signal_override_class_closure (signal_id, instance_type,
2040 g_cclosure_new (class_handler, NULL, NULL));
2041 else
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().
2059 void
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;
2066 guint n_params = 0;
2067 gpointer instance;
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));
2073 SIGNAL_LOCK ();
2074 emission = emission_find_innermost (instance);
2075 if (emission)
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;
2098 else
2099 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2101 else
2102 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2104 if (closure)
2106 emission->chain_type = chain_type;
2107 SIGNAL_UNLOCK ();
2108 g_closure_invoke (closure,
2109 return_value,
2110 n_params + 1,
2111 instance_and_params,
2112 &emission->ihint);
2113 SIGNAL_LOCK ();
2114 emission->chain_type = restore_type;
2116 SIGNAL_UNLOCK ();
2120 * g_signal_chain_from_overridden_handler: (skip)
2121 * @instance: (type GObject.TypeInstance): the instance the signal is being
2122 * emitted on.
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().
2132 * Since: 2.18
2134 void
2135 g_signal_chain_from_overridden_handler (gpointer instance,
2136 ...)
2138 GType chain_type = 0, restore_type = 0;
2139 Emission *emission = NULL;
2140 GClosure *closure = NULL;
2141 SignalNode *node;
2142 guint n_params = 0;
2144 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2146 SIGNAL_LOCK ();
2147 emission = emission_find_innermost (instance);
2148 if (emission)
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;
2171 else
2172 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2174 else
2175 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2177 if (closure)
2179 GValue *instance_and_params;
2180 GType signal_return_type;
2181 GValue *param_values;
2182 va_list var_args;
2183 guint i;
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++)
2194 gchar *error;
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;
2198 SIGNAL_UNLOCK ();
2199 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2200 var_args,
2201 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2202 &error);
2203 if (error)
2205 g_warning ("%s: %s", G_STRLOC, error);
2206 g_free (error);
2208 /* we purposely leak the value here, it might not be
2209 * in a sane state if an error condition occoured
2211 while (i--)
2212 g_value_unset (param_values + i);
2214 va_end (var_args);
2215 return;
2217 SIGNAL_LOCK ();
2220 SIGNAL_UNLOCK ();
2221 instance_and_params->g_type = 0;
2222 g_value_init_from_instance (instance_and_params, instance);
2223 SIGNAL_LOCK ();
2225 emission->chain_type = chain_type;
2226 SIGNAL_UNLOCK ();
2228 if (signal_return_type == G_TYPE_NONE)
2230 g_closure_invoke (closure,
2231 NULL,
2232 n_params + 1,
2233 instance_and_params,
2234 &emission->ihint);
2236 else
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,
2246 &return_value,
2247 n_params + 1,
2248 instance_and_params,
2249 &emission->ihint);
2251 G_VALUE_LCOPY (&return_value,
2252 var_args,
2253 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2254 &error);
2255 if (!error)
2257 g_value_unset (&return_value);
2259 else
2261 g_warning ("%s: %s", G_STRLOC, error);
2262 g_free (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);
2274 va_end (var_args);
2276 SIGNAL_LOCK ();
2277 emission->chain_type = restore_type;
2279 SIGNAL_UNLOCK ();
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);
2297 SIGNAL_LOCK ();
2298 emission = emission_find_innermost (instance);
2299 SIGNAL_UNLOCK ();
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)
2317 gulong
2318 g_signal_connect_closure_by_id (gpointer instance,
2319 guint signal_id,
2320 GQuark detail,
2321 GClosure *closure,
2322 gboolean after)
2324 SignalNode *node;
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);
2331 SIGNAL_LOCK ();
2332 node = LOOKUP_SIGNAL_NODE (signal_id);
2333 if (node)
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);
2339 else
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);
2357 else
2358 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2359 SIGNAL_UNLOCK ();
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)
2376 gulong
2377 g_signal_connect_closure (gpointer instance,
2378 const gchar *detailed_signal,
2379 GClosure *closure,
2380 gboolean after)
2382 guint signal_id;
2383 gulong handler_seq_no = 0;
2384 GQuark detail = 0;
2385 GType itype;
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);
2391 SIGNAL_LOCK ();
2392 itype = G_TYPE_FROM_INSTANCE (instance);
2393 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2394 if (signal_id)
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));
2403 else
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);
2421 else
2422 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2423 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2424 SIGNAL_UNLOCK ();
2426 return handler_seq_no;
2429 static void
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)
2469 gulong
2470 g_signal_connect_data (gpointer instance,
2471 const gchar *detailed_signal,
2472 GCallback c_handler,
2473 gpointer data,
2474 GClosureNotify destroy_data,
2475 GConnectFlags connect_flags)
2477 guint signal_id;
2478 gulong handler_seq_no = 0;
2479 GQuark detail = 0;
2480 GType itype;
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;
2490 SIGNAL_LOCK ();
2491 itype = G_TYPE_FROM_INSTANCE (instance);
2492 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2493 if (signal_id)
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));
2504 else
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);
2521 else
2522 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2523 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2524 SIGNAL_UNLOCK ();
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.
2543 void
2544 g_signal_handler_block (gpointer instance,
2545 gulong handler_id)
2547 Handler *handler;
2549 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2550 g_return_if_fail (handler_id > 0);
2552 SIGNAL_LOCK ();
2553 handler = handler_lookup (instance, handler_id, NULL, NULL);
2554 if (handler)
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);
2559 #endif
2560 handler->block_count += 1;
2562 else
2563 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2564 SIGNAL_UNLOCK ();
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
2581 * proceeded yet).
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.
2586 void
2587 g_signal_handler_unblock (gpointer instance,
2588 gulong handler_id)
2590 Handler *handler;
2592 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2593 g_return_if_fail (handler_id > 0);
2595 SIGNAL_LOCK ();
2596 handler = handler_lookup (instance, handler_id, NULL, NULL);
2597 if (handler)
2599 if (handler->block_count)
2600 handler->block_count -= 1;
2601 else
2602 g_warning (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2604 else
2605 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2606 SIGNAL_UNLOCK ();
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.
2621 void
2622 g_signal_handler_disconnect (gpointer instance,
2623 gulong handler_id)
2625 Handler *handler;
2627 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2628 g_return_if_fail (handler_id > 0);
2630 SIGNAL_LOCK ();
2631 handler = handler_lookup (instance, handler_id, 0, 0);
2632 if (handler)
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);
2640 else
2641 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2642 SIGNAL_UNLOCK ();
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.
2654 gboolean
2655 g_signal_handler_is_connected (gpointer instance,
2656 gulong handler_id)
2658 Handler *handler;
2659 gboolean connected;
2661 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2663 SIGNAL_LOCK ();
2664 handler = handler_lookup (instance, handler_id, NULL, NULL);
2665 connected = handler != NULL;
2666 SIGNAL_UNLOCK ();
2668 return connected;
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.
2679 void
2680 g_signal_handlers_destroy (gpointer instance)
2682 GBSearchArray *hlbsa;
2684 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2686 SIGNAL_LOCK ();
2687 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2688 if (hlbsa)
2690 guint i;
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;
2700 while (handler)
2702 Handler *tmp = handler;
2704 handler = tmp->next;
2705 tmp->block_count = 1;
2706 /* cruel unlink, this works because _all_ handlers vanish */
2707 tmp->next = NULL;
2708 tmp->prev = tmp;
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);
2720 SIGNAL_UNLOCK ();
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.
2742 gulong
2743 g_signal_handler_find (gpointer instance,
2744 GSignalMatchType mask,
2745 guint signal_id,
2746 GQuark detail,
2747 GClosure *closure,
2748 gpointer func,
2749 gpointer data)
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;
2760 SIGNAL_LOCK ();
2761 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2762 if (mlist)
2764 handler_seq_no = mlist->handler->sequential_number;
2765 handler_match_free1_R (mlist, instance);
2767 SIGNAL_UNLOCK ();
2770 return handler_seq_no;
2773 static guint
2774 signal_handlers_foreach_matched_R (gpointer instance,
2775 GSignalMatchType mask,
2776 guint signal_id,
2777 GQuark detail,
2778 GClosure *closure,
2779 gpointer func,
2780 gpointer data,
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);
2788 while (mlist)
2790 n_handlers++;
2791 if (mlist->handler->sequential_number)
2793 SIGNAL_UNLOCK ();
2794 callback (instance, mlist->handler->sequential_number);
2795 SIGNAL_LOCK ();
2797 mlist = handler_match_free1_R (mlist, instance);
2800 return n_handlers;
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
2820 * otherwise.
2822 * Returns: The number of handlers that matched.
2824 guint
2825 g_signal_handlers_block_matched (gpointer instance,
2826 GSignalMatchType mask,
2827 guint signal_id,
2828 GQuark detail,
2829 GClosure *closure,
2830 gpointer func,
2831 gpointer data)
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))
2840 SIGNAL_LOCK ();
2841 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2842 closure, func, data,
2843 g_signal_handler_block);
2844 SIGNAL_UNLOCK ();
2847 return n_handlers;
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.
2872 guint
2873 g_signal_handlers_unblock_matched (gpointer instance,
2874 GSignalMatchType mask,
2875 guint signal_id,
2876 GQuark detail,
2877 GClosure *closure,
2878 gpointer func,
2879 gpointer data)
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))
2888 SIGNAL_LOCK ();
2889 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2890 closure, func, data,
2891 g_signal_handler_unblock);
2892 SIGNAL_UNLOCK ();
2895 return n_handlers;
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.
2920 guint
2921 g_signal_handlers_disconnect_matched (gpointer instance,
2922 GSignalMatchType mask,
2923 guint signal_id,
2924 GQuark detail,
2925 GClosure *closure,
2926 gpointer func,
2927 gpointer data)
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))
2936 SIGNAL_LOCK ();
2937 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2938 closure, func, data,
2939 g_signal_handler_disconnect);
2940 SIGNAL_UNLOCK ();
2943 return n_handlers;
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
2971 * otherwise.
2973 gboolean
2974 g_signal_has_handler_pending (gpointer instance,
2975 guint signal_id,
2976 GQuark detail,
2977 gboolean may_be_blocked)
2979 HandlerMatch *mlist;
2980 gboolean has_pending;
2981 SignalNode *node;
2983 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2984 g_return_val_if_fail (signal_id > 0, FALSE);
2986 SIGNAL_LOCK ();
2988 node = LOOKUP_SIGNAL_NODE (signal_id);
2989 if (detail)
2991 if (!(node->flags & G_SIGNAL_DETAILED))
2993 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2994 SIGNAL_UNLOCK ();
2995 return FALSE;
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);
3001 if (mlist)
3003 has_pending = TRUE;
3004 handler_match_free1_R (mlist, instance);
3006 else
3008 ClassClosure *class_closure = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
3009 if (class_closure != NULL && class_closure->instance_type != 0)
3010 has_pending = TRUE;
3011 else
3012 has_pending = FALSE;
3014 SIGNAL_UNLOCK ();
3016 return has_pending;
3020 * g_signal_emitv:
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.
3030 * Emits a signal.
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().
3035 void
3036 g_signal_emitv (const GValue *instance_and_params,
3037 guint signal_id,
3038 GQuark detail,
3039 GValue *return_value)
3041 gpointer instance;
3042 SignalNode *node;
3043 #ifdef G_ENABLE_DEBUG
3044 const GValue *param_values;
3045 guint i;
3046 #endif
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;
3055 #endif
3057 SIGNAL_LOCK ();
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);
3062 SIGNAL_UNLOCK ();
3063 return;
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);
3069 SIGNAL_UNLOCK ();
3070 return;
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'",
3076 G_STRLOC,
3077 type_debug_name (node->param_types[i]),
3079 node->name,
3080 G_VALUE_TYPE_NAME (param_values + i));
3081 SIGNAL_UNLOCK ();
3082 return;
3084 if (node->return_type != G_TYPE_NONE)
3086 if (!return_value)
3088 g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3089 G_STRLOC,
3090 type_debug_name (node->return_type),
3091 node->name);
3092 SIGNAL_UNLOCK ();
3093 return;
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'",
3098 G_STRLOC,
3099 type_debug_name (node->return_type),
3100 node->name,
3101 G_VALUE_TYPE_NAME (return_value));
3102 SIGNAL_UNLOCK ();
3103 return;
3106 else
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 */
3122 SIGNAL_UNLOCK ();
3123 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3124 return;
3128 SIGNAL_UNLOCK ();
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;
3140 if (!accumulator)
3141 return TRUE;
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
3152 * emitted on.
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.
3159 * Emits a signal.
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().
3164 void
3165 g_signal_emit_valist (gpointer instance,
3166 guint signal_id,
3167 GQuark detail,
3168 va_list var_args)
3170 GValue *instance_and_params;
3171 GType signal_return_type;
3172 GValue *param_values;
3173 SignalNode *node;
3174 guint i, n_params;
3176 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3177 g_return_if_fail (signal_id > 0);
3179 SIGNAL_LOCK ();
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);
3184 SIGNAL_UNLOCK ();
3185 return;
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);
3191 SIGNAL_UNLOCK ();
3192 return;
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;
3203 Handler *l;
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;
3216 else
3217 run_type = G_SIGNAL_RUN_FIRST;
3219 else
3220 fastpath = FALSE;
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))
3230 fastpath = FALSE;
3231 break;
3233 else
3235 fastpath_handler = l;
3236 closure = l->closure;
3237 if (l->after)
3238 run_type = G_SIGNAL_RUN_LAST;
3239 else
3240 run_type = G_SIGNAL_RUN_FIRST;
3245 if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3247 SIGNAL_UNLOCK ();
3248 return;
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)
3254 fastpath = FALSE;
3256 if (fastpath)
3258 SignalAccumulator *accumulator;
3259 Emission emission;
3260 GValue *return_accu, accu = G_VALUE_INIT;
3261 guint signal_id;
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)
3270 return_accu = NULL;
3271 else if (accumulator)
3272 return_accu = &accu;
3273 else
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);
3287 SIGNAL_UNLOCK ();
3289 TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3291 if (rtype != G_TYPE_NONE)
3292 g_value_init (&emission_return, rtype);
3294 if (accumulator)
3295 g_value_init (&accu, rtype);
3297 if (closure != NULL)
3299 g_object_ref (instance);
3300 _g_closure_invoke_va (closure,
3301 return_accu,
3302 instance,
3303 var_args,
3304 node->n_params,
3305 node->param_types);
3306 accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3309 SIGNAL_LOCK ();
3311 emission.chain_type = G_TYPE_NONE;
3312 emission_pop (&emission);
3314 if (fastpath_handler)
3315 handler_unref_R (signal_id, instance, fastpath_handler);
3317 SIGNAL_UNLOCK ();
3319 if (accumulator)
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,
3332 var_args,
3333 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3334 &error);
3335 if (!error)
3336 g_value_unset (&emission_return);
3337 else
3339 g_warning ("%s: %s", G_STRLOC, error);
3340 g_free (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);
3352 return;
3355 SIGNAL_UNLOCK ();
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++)
3365 gchar *error;
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,
3370 var_args,
3371 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3372 &error);
3373 if (error)
3375 g_warning ("%s: %s", G_STRLOC, error);
3376 g_free (error);
3378 /* we purposely leak the value here, it might not be
3379 * in a sane state if an error condition occoured
3381 while (i--)
3382 g_value_unset (param_values + i);
3384 return;
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);
3392 else
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,
3404 var_args,
3405 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3406 &error);
3407 if (!error)
3408 g_value_unset (&return_value);
3409 else
3411 g_warning ("%s: %s", G_STRLOC, error);
3412 g_free (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);
3425 * g_signal_emit:
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.
3433 * Emits a signal.
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().
3438 void
3439 g_signal_emit (gpointer instance,
3440 guint signal_id,
3441 GQuark detail,
3442 ...)
3444 va_list var_args;
3446 va_start (var_args, detail);
3447 g_signal_emit_valist (instance, signal_id, detail, var_args);
3448 va_end (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.
3459 * Emits a signal.
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().
3464 void
3465 g_signal_emit_by_name (gpointer instance,
3466 const gchar *detailed_signal,
3467 ...)
3469 GQuark detail = 0;
3470 guint signal_id;
3471 GType itype;
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);
3478 SIGNAL_LOCK ();
3479 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3480 SIGNAL_UNLOCK ();
3482 if (signal_id)
3484 va_list var_args;
3486 va_start (var_args, detailed_signal);
3487 g_signal_emit_valist (instance, signal_id, detail, var_args);
3488 va_end (var_args);
3490 else
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));
3495 static gboolean
3496 signal_emit_unlocked_R (SignalNode *node,
3497 GQuark detail,
3498 gpointer instance,
3499 GValue *emission_return,
3500 const GValue *instance_and_params)
3502 SignalAccumulator *accumulator;
3503 Emission emission;
3504 GClosure *class_closure;
3505 HandlerList *hlist;
3506 Handler *handler_list = NULL;
3507 GValue *return_accu, accu = G_VALUE_INIT;
3508 guint signal_id;
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)));
3514 SIGNAL_LOCK ();
3515 signal_id = node->signal_id;
3517 if (node->flags & G_SIGNAL_NO_RECURSE)
3519 Emission *node = emission_find (signal_id, detail, instance);
3521 if (node)
3523 node->state = EMISSION_RESTART;
3524 SIGNAL_UNLOCK ();
3525 return return_value_altered;
3528 accumulator = node->accumulator;
3529 if (accumulator)
3531 SIGNAL_UNLOCK ();
3532 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3533 return_accu = &accu;
3534 SIGNAL_LOCK ();
3536 else
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;
3542 emission.state = 0;
3543 emission.chain_type = G_TYPE_NONE;
3544 emission_push (&emission);
3545 class_closure = signal_lookup_closure (node, instance);
3547 EMIT_RESTART:
3549 if (handler_list)
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;
3554 if (handler_list)
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);
3564 SIGNAL_UNLOCK ();
3565 g_closure_invoke (class_closure,
3566 return_accu,
3567 node->n_params + 1,
3568 instance_and_params,
3569 &emission.ihint);
3570 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3571 emission.state == EMISSION_RUN)
3572 emission.state = EMISSION_STOP;
3573 SIGNAL_LOCK ();
3574 emission.chain_type = G_TYPE_NONE;
3575 return_value_altered = TRUE;
3577 if (emission.state == EMISSION_STOP)
3578 goto EMIT_CLEANUP;
3579 else if (emission.state == EMISSION_RESTART)
3580 goto EMIT_RESTART;
3583 if (node->emission_hooks)
3585 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3586 GHook *hook;
3588 emission.state = EMISSION_HOOK;
3589 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3590 while (hook)
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;
3600 SIGNAL_UNLOCK ();
3601 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3602 SIGNAL_LOCK ();
3603 if (!was_in_call)
3604 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3605 if (need_destroy)
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)
3612 goto EMIT_RESTART;
3615 if (handler_list)
3617 Handler *handler = handler_list;
3619 emission.state = EMISSION_RUN;
3620 handler_ref (handler);
3623 Handler *tmp;
3625 if (handler->after)
3627 handler_unref_R (signal_id, instance, handler_list);
3628 handler_list = handler;
3629 break;
3631 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3632 handler->sequential_number < max_sequential_handler_number)
3634 SIGNAL_UNLOCK ();
3635 g_closure_invoke (handler->closure,
3636 return_accu,
3637 node->n_params + 1,
3638 instance_and_params,
3639 &emission.ihint);
3640 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3641 emission.state == EMISSION_RUN)
3642 emission.state = EMISSION_STOP;
3643 SIGNAL_LOCK ();
3644 return_value_altered = TRUE;
3646 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3648 else
3649 tmp = handler->next;
3651 if (tmp)
3652 handler_ref (tmp);
3653 handler_unref_R (signal_id, instance, handler_list);
3654 handler_list = handler;
3655 handler = tmp;
3657 while (handler);
3659 if (emission.state == EMISSION_STOP)
3660 goto EMIT_CLEANUP;
3661 else if (emission.state == EMISSION_RESTART)
3662 goto EMIT_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);
3672 SIGNAL_UNLOCK ();
3673 g_closure_invoke (class_closure,
3674 return_accu,
3675 node->n_params + 1,
3676 instance_and_params,
3677 &emission.ihint);
3678 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3679 emission.state == EMISSION_RUN)
3680 emission.state = EMISSION_STOP;
3681 SIGNAL_LOCK ();
3682 emission.chain_type = G_TYPE_NONE;
3683 return_value_altered = TRUE;
3685 if (emission.state == EMISSION_STOP)
3686 goto EMIT_CLEANUP;
3687 else if (emission.state == EMISSION_RESTART)
3688 goto EMIT_RESTART;
3691 if (handler_list)
3693 Handler *handler = handler_list;
3695 emission.state = EMISSION_RUN;
3696 handler_ref (handler);
3699 Handler *tmp;
3701 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3702 handler->sequential_number < max_sequential_handler_number)
3704 SIGNAL_UNLOCK ();
3705 g_closure_invoke (handler->closure,
3706 return_accu,
3707 node->n_params + 1,
3708 instance_and_params,
3709 &emission.ihint);
3710 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3711 emission.state == EMISSION_RUN)
3712 emission.state = EMISSION_STOP;
3713 SIGNAL_LOCK ();
3714 return_value_altered = TRUE;
3716 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3718 else
3719 tmp = handler->next;
3721 if (tmp)
3722 handler_ref (tmp);
3723 handler_unref_R (signal_id, instance, handler);
3724 handler = tmp;
3726 while (handler);
3728 if (emission.state == EMISSION_STOP)
3729 goto EMIT_CLEANUP;
3730 else if (emission.state == EMISSION_RESTART)
3731 goto EMIT_RESTART;
3734 EMIT_CLEANUP:
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);
3745 SIGNAL_UNLOCK ();
3746 if (node->return_type != G_TYPE_NONE && !accumulator)
3748 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3749 need_unset = TRUE;
3751 g_closure_invoke (class_closure,
3752 node->return_type != G_TYPE_NONE ? &accu : NULL,
3753 node->n_params + 1,
3754 instance_and_params,
3755 &emission.ihint);
3756 if (need_unset)
3757 g_value_unset (&accu);
3758 SIGNAL_LOCK ();
3759 emission.chain_type = G_TYPE_NONE;
3761 if (emission.state == EMISSION_RESTART)
3762 goto EMIT_RESTART;
3765 if (handler_list)
3766 handler_unref_R (signal_id, instance, handler_list);
3768 emission_pop (&emission);
3769 SIGNAL_UNLOCK ();
3770 if (accumulator)
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;
3778 static void
3779 add_invalid_closure_notify (Handler *handler,
3780 gpointer instance)
3782 g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3783 handler->has_invalid_closure_notify = 1;
3786 static void
3787 remove_invalid_closure_notify (Handler *handler,
3788 gpointer instance)
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;
3797 static void
3798 invalid_closure_notify (gpointer instance,
3799 GClosure *closure)
3801 Handler *handler;
3802 guint signal_id;
3804 SIGNAL_LOCK ();
3806 handler = handler_lookup (instance, 0, closure, &signal_id);
3807 /* See https://bugzilla.gnome.org/show_bug.cgi?id=730296 for discussion about this... */
3808 g_assert (handler != NULL);
3809 g_assert (handler->closure == closure);
3811 handler->sequential_number = 0;
3812 handler->block_count = 1;
3813 handler_unref_R (signal_id, instance, handler);
3815 SIGNAL_UNLOCK ();
3818 static const gchar*
3819 type_debug_name (GType type)
3821 if (type)
3823 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3824 return name ? name : "<unknown>";
3826 else
3827 return "<invalid>";
3831 * g_signal_accumulator_true_handled:
3832 * @ihint: standard #GSignalAccumulator parameter
3833 * @return_accu: standard #GSignalAccumulator parameter
3834 * @handler_return: standard #GSignalAccumulator parameter
3835 * @dummy: standard #GSignalAccumulator parameter
3837 * A predefined #GSignalAccumulator for signals that return a
3838 * boolean values. The behavior that this accumulator gives is
3839 * that a return of %TRUE stops the signal emission: no further
3840 * callbacks will be invoked, while a return of %FALSE allows
3841 * the emission to continue. The idea here is that a %TRUE return
3842 * indicates that the callback handled the signal, and no further
3843 * handling is needed.
3845 * Since: 2.4
3847 * Returns: standard #GSignalAccumulator result
3849 gboolean
3850 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3851 GValue *return_accu,
3852 const GValue *handler_return,
3853 gpointer dummy)
3855 gboolean continue_emission;
3856 gboolean signal_handled;
3858 signal_handled = g_value_get_boolean (handler_return);
3859 g_value_set_boolean (return_accu, signal_handled);
3860 continue_emission = !signal_handled;
3862 return continue_emission;
3866 * g_signal_accumulator_first_wins:
3867 * @ihint: standard #GSignalAccumulator parameter
3868 * @return_accu: standard #GSignalAccumulator parameter
3869 * @handler_return: standard #GSignalAccumulator parameter
3870 * @dummy: standard #GSignalAccumulator parameter
3872 * A predefined #GSignalAccumulator for signals intended to be used as a
3873 * hook for application code to provide a particular value. Usually
3874 * only one such value is desired and multiple handlers for the same
3875 * signal don't make much sense (except for the case of the default
3876 * handler defined in the class structure, in which case you will
3877 * usually want the signal connection to override the class handler).
3879 * This accumulator will use the return value from the first signal
3880 * handler that is run as the return value for the signal and not run
3881 * any further handlers (ie: the first handler "wins").
3883 * Returns: standard #GSignalAccumulator result
3885 * Since: 2.28
3887 gboolean
3888 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
3889 GValue *return_accu,
3890 const GValue *handler_return,
3891 gpointer dummy)
3893 g_value_copy (handler_return, return_accu);
3894 return FALSE;