1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 * this code is based on the original GtkSignal implementation
18 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
19 */
20
21 /*
22 * MT safe
23 */
24
25 #include "config.h"
26
27 #include <string.h>
28 #include <signal.h>
29
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"
38
39
40 /**
41 * SECTION:signals
42 * @short_description: A means for customization of object behaviour
43 * and a general purpose notification mechanism
44 * @title: Signals
45 *
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.
51 *
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".
61 *
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.
66 *
67 * A signal emission consists of five stages, unless prematurely stopped:
68 *
69 * 1. Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
70 *
71 * 2. Invocation of normal user-provided signal handlers (where the @after
72 * flag is not set)
73 *
74 * 3. Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
75 *
76 * 4. Invocation of user provided signal handlers (where the @after flag is set)
77 *
78 * 5. Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
79 *
80 * The user-provided signal handlers are called in the order they were
81 * connected in.
82 *
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.
86 *
87 * There are certain criteria for skipping user handlers in stages 2 and 4
88 * of a signal emission.
89 *
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.
93 *
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.
100 *
101 * While the @detail argument is typically used to pass an object property name
102 * (as with #GObject::notify), no specific format is mandated for the detail
103 * string, other than that it must be non-empty.
104 *
105 * ## Memory management of signal handlers # {#signal-memory-management}
106 *
107 * If you are connecting handlers to signals and using a #GObject instance as
108 * your signal handler user data, you should remember to pair calls to
109 * g_signal_connect() with calls to g_signal_handler_disconnect() or
110 * g_signal_handlers_disconnect_by_func(). While signal handlers are
111 * automatically disconnected when the object emitting the signal is finalised,
112 * they are not automatically disconnected when the signal handler user data is
113 * destroyed. If this user data is a #GObject instance, using it from a
114 * signal handler after it has been finalised is an error.
115 *
116 * There are two strategies for managing such user data. The first is to
117 * disconnect the signal handler (using g_signal_handler_disconnect() or
118 * g_signal_handlers_disconnect_by_func()) when the user data (object) is
119 * finalised; this has to be implemented manually. For non-threaded programs,
120 * g_signal_connect_object() can be used to implement this automatically.
121 * Currently, however, it is unsafe to use in threaded programs.
122 *
123 * The second is to hold a strong reference on the user data until after the
124 * signal is disconnected for other reasons. This can be implemented
125 * automatically using g_signal_connect_data().
126 *
127 * The first approach is recommended, as the second approach can result in
128 * effective memory leaks of the user data if the signal handler is never
129 * disconnected for some reason.
130 */
131
132
133 #define REPORT_BUG "please report occurrence circumstances to https://gitlab.gnome.org/GNOME/glib/issues/new"
134
135 /* --- typedefs --- */
136 typedef struct _SignalNode SignalNode;
137 typedef struct _SignalKey SignalKey;
138 typedef struct _Emission Emission;
139 typedef struct _Handler Handler;
140 typedef struct _HandlerList HandlerList;
141 typedef struct _HandlerMatch HandlerMatch;
142 typedef enum
143 {
144 EMISSION_STOP,
145 EMISSION_RUN,
146 EMISSION_HOOK,
147 EMISSION_RESTART
148 } EmissionState;
149
150
151 /* --- prototypes --- */
152 static inline guint signal_id_lookup (const gchar *name,
153 GType itype);
154 static void signal_destroy_R (SignalNode *signal_node);
155 static inline HandlerList* handler_list_ensure (guint signal_id,
156 gpointer instance);
157 static inline HandlerList* handler_list_lookup (guint signal_id,
158 gpointer instance);
159 static inline Handler* handler_new (guint signal_id,
160 gpointer instance,
161 gboolean after);
162 static void handler_insert (guint signal_id,
163 gpointer instance,
164 Handler *handler);
165 static Handler* handler_lookup (gpointer instance,
166 gulong handler_id,
167 GClosure *closure,
168 guint *signal_id_p);
169 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
170 Handler *handler,
171 guint signal_id);
172 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
173 gpointer instance);
174 static HandlerMatch* handlers_find (gpointer instance,
175 GSignalMatchType mask,
176 guint signal_id,
177 GQuark detail,
178 GClosure *closure,
179 gpointer func,
180 gpointer data,
181 gboolean one_and_only);
182 static inline void handler_ref (Handler *handler);
183 static inline void handler_unref_R (guint signal_id,
184 gpointer instance,
185 Handler *handler);
186 static gint handler_lists_cmp (gconstpointer node1,
187 gconstpointer node2);
188 static inline void emission_push (Emission *emission);
189 static inline void emission_pop (Emission *emission);
190 static inline Emission* emission_find (guint signal_id,
191 GQuark detail,
192 gpointer instance);
193 static gint class_closures_cmp (gconstpointer node1,
194 gconstpointer node2);
195 static gint signal_key_cmp (gconstpointer node1,
196 gconstpointer node2);
197 static gboolean signal_emit_unlocked_R (SignalNode *node,
198 GQuark detail,
199 gpointer instance,
200 GValue *return_value,
201 const GValue *instance_and_params);
202 static void add_invalid_closure_notify (Handler *handler,
203 gpointer instance);
204 static void remove_invalid_closure_notify (Handler *handler,
205 gpointer instance);
206 static void invalid_closure_notify (gpointer data,
207 GClosure *closure);
208 static const gchar * type_debug_name (GType type);
209 static void node_check_deprecated (const SignalNode *node);
210 static void node_update_single_va_closure (SignalNode *node);
211
212
213 /* --- structures --- */
214 typedef struct
215 {
216 GSignalAccumulator func;
217 gpointer data;
218 } SignalAccumulator;
219 typedef struct
220 {
221 GHook hook;
222 GQuark detail;
223 } SignalHook;
224 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
225
226 struct _SignalNode
227 {
228 /* permanent portion */
229 guint signal_id;
230 GType itype;
231 const gchar *name;
232 guint destroyed : 1;
233
234 /* reinitializable portion */
235 guint flags : 9;
236 guint n_params : 8;
237 guint single_va_closure_is_valid : 1;
238 guint single_va_closure_is_after : 1;
239 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
240 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
241 GBSearchArray *class_closure_bsa;
242 SignalAccumulator *accumulator;
243 GSignalCMarshaller c_marshaller;
244 GSignalCVaMarshaller va_marshaller;
245 GHookList *emission_hooks;
246
247 GClosure *single_va_closure;
248 };
249
250 #define SINGLE_VA_CLOSURE_EMPTY_MAGIC GINT_TO_POINTER(1) /* indicates single_va_closure is valid but empty */
251
252 struct _SignalKey
253 {
254 GType itype;
255 GQuark quark;
256 guint signal_id;
257 };
258
259 struct _Emission
260 {
261 Emission *next;
262 gpointer instance;
263 GSignalInvocationHint ihint;
264 EmissionState state;
265 GType chain_type;
266 };
267
268 struct _HandlerList
269 {
270 guint signal_id;
271 Handler *handlers;
272 Handler *tail_before; /* normal signal handlers are appended here */
273 Handler *tail_after; /* CONNECT_AFTER handlers are appended here */
274 };
275
276 struct _Handler
277 {
278 gulong sequential_number;
279 Handler *next;
280 Handler *prev;
281 GQuark detail;
282 guint signal_id;
283 guint ref_count;
284 guint block_count : 16;
285 #define HANDLER_MAX_BLOCK_COUNT (1 << 16)
286 guint after : 1;
287 guint has_invalid_closure_notify : 1;
288 GClosure *closure;
289 gpointer instance;
290 };
291 struct _HandlerMatch
292 {
293 Handler *handler;
294 HandlerMatch *next;
295 guint signal_id;
296 };
297
298 typedef struct
299 {
300 GType instance_type; /* 0 for default closure */
301 GClosure *closure;
302 } ClassClosure;
303
304
305 /* --- variables --- */
306 static GBSearchArray *g_signal_key_bsa = NULL;
307 static const GBSearchConfig g_signal_key_bconfig = {
308 sizeof (SignalKey),
309 signal_key_cmp,
310 G_BSEARCH_ARRAY_ALIGN_POWER2,
311 };
312 static GBSearchConfig g_signal_hlbsa_bconfig = {
313 sizeof (HandlerList),
314 handler_lists_cmp,
315 0,
316 };
317 static GBSearchConfig g_class_closure_bconfig = {
318 sizeof (ClassClosure),
319 class_closures_cmp,
320 0,
321 };
322 static GHashTable *g_handler_list_bsa_ht = NULL;
323 static Emission *g_emissions = NULL;
324 static gulong g_handler_sequential_number = 1;
325 static GHashTable *g_handlers = NULL;
326
327 G_LOCK_DEFINE_STATIC (g_signal_mutex);
328 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
329 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
330
331
332 /* --- signal nodes --- */
333 static guint g_n_signal_nodes = 0;
334 static SignalNode **g_signal_nodes = NULL;
335
336 static inline SignalNode*
LOOKUP_SIGNAL_NODE(guint signal_id)337 LOOKUP_SIGNAL_NODE (guint signal_id)
338 {
339 if (signal_id < g_n_signal_nodes)
340 return g_signal_nodes[signal_id];
341 else
342 return NULL;
343 }
344
345
346 /* --- functions --- */
347 /* @key must have already been validated with is_valid()
348 * Modifies @key in place. */
349 static void
canonicalize_key(gchar * key)350 canonicalize_key (gchar *key)
351 {
352 gchar *p;
353
354 for (p = key; *p != 0; p++)
355 {
356 gchar c = *p;
357
358 if (c == '_')
359 *p = '-';
360 }
361 }
362
363 /* @key must have already been validated with is_valid() */
364 static gboolean
is_canonical(const gchar * key)365 is_canonical (const gchar *key)
366 {
367 return (strchr (key, '_') == NULL);
368 }
369
370 /**
371 * g_signal_is_valid_name:
372 * @name: the canonical name of the signal
373 *
374 * Validate a signal name. This can be useful for dynamically-generated signals
375 * which need to be validated at run-time before actually trying to create them.
376 *
377 * See [canonical parameter names][canonical-parameter-names] for details of
378 * the rules for valid names. The rules for signal names are the same as those
379 * for property names.
380 *
381 * Returns: %TRUE if @name is a valid signal name, %FALSE otherwise.
382 * Since: 2.66
383 */
384 gboolean
g_signal_is_valid_name(const gchar * name)385 g_signal_is_valid_name (const gchar *name)
386 {
387 /* FIXME: We allow this, against our own documentation (the leading `-` is
388 * invalid), because GTK has historically used this. */
389 if (g_str_equal (name, "-gtk-private-changed"))
390 return TRUE;
391
392 return g_param_spec_is_valid_name (name);
393 }
394
395 static inline guint
signal_id_lookup(const gchar * name,GType itype)396 signal_id_lookup (const gchar *name,
397 GType itype)
398 {
399 GQuark quark;
400 GType *ifaces, type = itype;
401 SignalKey key;
402 guint n_ifaces;
403
404 quark = g_quark_try_string (name);
405 key.quark = quark;
406
407 /* try looking up signals for this type and its ancestors */
408 do
409 {
410 SignalKey *signal_key;
411
412 key.itype = type;
413 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
414
415 if (signal_key)
416 return signal_key->signal_id;
417
418 type = g_type_parent (type);
419 }
420 while (type);
421
422 /* no luck, try interfaces it exports */
423 ifaces = g_type_interfaces (itype, &n_ifaces);
424 while (n_ifaces--)
425 {
426 SignalKey *signal_key;
427
428 key.itype = ifaces[n_ifaces];
429 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
430
431 if (signal_key)
432 {
433 g_free (ifaces);
434 return signal_key->signal_id;
435 }
436 }
437 g_free (ifaces);
438
439 /* If the @name is non-canonical, try again. This is the slow path — people
440 * should use canonical names in their queries if they want performance. */
441 if (!is_canonical (name))
442 {
443 guint signal_id;
444 gchar *name_copy = g_strdup (name);
445 canonicalize_key (name_copy);
446
447 signal_id = signal_id_lookup (name_copy, itype);
448
449 g_free (name_copy);
450
451 return signal_id;
452 }
453
454 return 0;
455 }
456
457 static gint
class_closures_cmp(gconstpointer node1,gconstpointer node2)458 class_closures_cmp (gconstpointer node1,
459 gconstpointer node2)
460 {
461 const ClassClosure *c1 = node1, *c2 = node2;
462
463 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
464 }
465
466 static gint
handler_lists_cmp(gconstpointer node1,gconstpointer node2)467 handler_lists_cmp (gconstpointer node1,
468 gconstpointer node2)
469 {
470 const HandlerList *hlist1 = node1, *hlist2 = node2;
471
472 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
473 }
474
475 static inline HandlerList*
handler_list_ensure(guint signal_id,gpointer instance)476 handler_list_ensure (guint signal_id,
477 gpointer instance)
478 {
479 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
480 HandlerList key;
481
482 key.signal_id = signal_id;
483 key.handlers = NULL;
484 key.tail_before = NULL;
485 key.tail_after = NULL;
486 if (!hlbsa)
487 {
488 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
489 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
490 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
491 }
492 else
493 {
494 GBSearchArray *o = hlbsa;
495
496 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
497 if (hlbsa != o)
498 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
499 }
500 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
501 }
502
503 static inline HandlerList*
handler_list_lookup(guint signal_id,gpointer instance)504 handler_list_lookup (guint signal_id,
505 gpointer instance)
506 {
507 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
508 HandlerList key;
509
510 key.signal_id = signal_id;
511
512 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
513 }
514
515 static guint
handler_hash(gconstpointer key)516 handler_hash (gconstpointer key)
517 {
518 return (guint)((Handler*)key)->sequential_number;
519 }
520
521 static gboolean
handler_equal(gconstpointer a,gconstpointer b)522 handler_equal (gconstpointer a, gconstpointer b)
523 {
524 Handler *ha = (Handler *)a;
525 Handler *hb = (Handler *)b;
526 return (ha->sequential_number == hb->sequential_number) &&
527 (ha->instance == hb->instance);
528 }
529
530 static Handler*
handler_lookup(gpointer instance,gulong handler_id,GClosure * closure,guint * signal_id_p)531 handler_lookup (gpointer instance,
532 gulong handler_id,
533 GClosure *closure,
534 guint *signal_id_p)
535 {
536 GBSearchArray *hlbsa;
537
538 if (handler_id)
539 {
540 Handler key;
541 key.sequential_number = handler_id;
542 key.instance = instance;
543 return g_hash_table_lookup (g_handlers, &key);
544
545 }
546
547 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
548
549 if (hlbsa)
550 {
551 guint i;
552
553 for (i = 0; i < hlbsa->n_nodes; i++)
554 {
555 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
556 Handler *handler;
557
558 for (handler = hlist->handlers; handler; handler = handler->next)
559 if (closure ? (handler->closure == closure) : (handler->sequential_number == handler_id))
560 {
561 if (signal_id_p)
562 *signal_id_p = hlist->signal_id;
563
564 return handler;
565 }
566 }
567 }
568
569 return NULL;
570 }
571
572 static inline HandlerMatch*
handler_match_prepend(HandlerMatch * list,Handler * handler,guint signal_id)573 handler_match_prepend (HandlerMatch *list,
574 Handler *handler,
575 guint signal_id)
576 {
577 HandlerMatch *node;
578
579 node = g_slice_new (HandlerMatch);
580 node->handler = handler;
581 node->next = list;
582 node->signal_id = signal_id;
583 handler_ref (handler);
584
585 return node;
586 }
587 static inline HandlerMatch*
handler_match_free1_R(HandlerMatch * node,gpointer instance)588 handler_match_free1_R (HandlerMatch *node,
589 gpointer instance)
590 {
591 HandlerMatch *next = node->next;
592
593 handler_unref_R (node->signal_id, instance, node->handler);
594 g_slice_free (HandlerMatch, node);
595
596 return next;
597 }
598
599 static HandlerMatch*
handlers_find(gpointer instance,GSignalMatchType mask,guint signal_id,GQuark detail,GClosure * closure,gpointer func,gpointer data,gboolean one_and_only)600 handlers_find (gpointer instance,
601 GSignalMatchType mask,
602 guint signal_id,
603 GQuark detail,
604 GClosure *closure,
605 gpointer func,
606 gpointer data,
607 gboolean one_and_only)
608 {
609 HandlerMatch *mlist = NULL;
610
611 if (mask & G_SIGNAL_MATCH_ID)
612 {
613 HandlerList *hlist = handler_list_lookup (signal_id, instance);
614 Handler *handler;
615 SignalNode *node = NULL;
616
617 if (mask & G_SIGNAL_MATCH_FUNC)
618 {
619 node = LOOKUP_SIGNAL_NODE (signal_id);
620 if (!node || !node->c_marshaller)
621 return NULL;
622 }
623
624 mask = ~mask;
625 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
626 if (handler->sequential_number &&
627 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
628 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
629 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
630 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
631 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
632 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
633 ((GCClosure*) handler->closure)->callback == func)))
634 {
635 mlist = handler_match_prepend (mlist, handler, signal_id);
636 if (one_and_only)
637 return mlist;
638 }
639 }
640 else
641 {
642 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
643
644 mask = ~mask;
645 if (hlbsa)
646 {
647 guint i;
648
649 for (i = 0; i < hlbsa->n_nodes; i++)
650 {
651 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
652 SignalNode *node = NULL;
653 Handler *handler;
654
655 if (!(mask & G_SIGNAL_MATCH_FUNC))
656 {
657 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
658 if (!node->c_marshaller)
659 continue;
660 }
661
662 for (handler = hlist->handlers; handler; handler = handler->next)
663 if (handler->sequential_number &&
664 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
665 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
666 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
667 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
668 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
669 G_REAL_CLOSURE (handler->closure)->meta_marshal == NULL &&
670 ((GCClosure*) handler->closure)->callback == func)))
671 {
672 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
673 if (one_and_only)
674 return mlist;
675 }
676 }
677 }
678 }
679
680 return mlist;
681 }
682
683 static inline Handler*
handler_new(guint signal_id,gpointer instance,gboolean after)684 handler_new (guint signal_id, gpointer instance, gboolean after)
685 {
686 Handler *handler = g_slice_new (Handler);
687 #ifndef G_DISABLE_CHECKS
688 if (g_handler_sequential_number < 1)
689 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
690 #endif
691
692 handler->sequential_number = g_handler_sequential_number++;
693 handler->prev = NULL;
694 handler->next = NULL;
695 handler->detail = 0;
696 handler->signal_id = signal_id;
697 handler->instance = instance;
698 handler->ref_count = 1;
699 handler->block_count = 0;
700 handler->after = after != FALSE;
701 handler->closure = NULL;
702 handler->has_invalid_closure_notify = 0;
703
704 g_hash_table_add (g_handlers, handler);
705
706 return handler;
707 }
708
709 static inline void
handler_ref(Handler * handler)710 handler_ref (Handler *handler)
711 {
712 g_return_if_fail (handler->ref_count > 0);
713
714 handler->ref_count++;
715 }
716
717 static inline void
handler_unref_R(guint signal_id,gpointer instance,Handler * handler)718 handler_unref_R (guint signal_id,
719 gpointer instance,
720 Handler *handler)
721 {
722 g_return_if_fail (handler->ref_count > 0);
723
724 handler->ref_count--;
725
726 if (G_UNLIKELY (handler->ref_count == 0))
727 {
728 HandlerList *hlist = NULL;
729
730 if (handler->next)
731 handler->next->prev = handler->prev;
732 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
733 handler->prev->next = handler->next;
734 else
735 {
736 hlist = handler_list_lookup (signal_id, instance);
737 g_assert (hlist != NULL);
738 hlist->handlers = handler->next;
739 }
740
741 if (instance)
742 {
743 /* check if we are removing the handler pointed to by tail_before */
744 if (!handler->after && (!handler->next || handler->next->after))
745 {
746 if (!hlist)
747 hlist = handler_list_lookup (signal_id, instance);
748 if (hlist)
749 {
750 g_assert (hlist->tail_before == handler); /* paranoid */
751 hlist->tail_before = handler->prev;
752 }
753 }
754
755 /* check if we are removing the handler pointed to by tail_after */
756 if (!handler->next)
757 {
758 if (!hlist)
759 hlist = handler_list_lookup (signal_id, instance);
760 if (hlist)
761 {
762 g_assert (hlist->tail_after == handler); /* paranoid */
763 hlist->tail_after = handler->prev;
764 }
765 }
766 }
767
768 SIGNAL_UNLOCK ();
769 g_closure_unref (handler->closure);
770 SIGNAL_LOCK ();
771 g_slice_free (Handler, handler);
772 }
773 }
774
775 static void
handler_insert(guint signal_id,gpointer instance,Handler * handler)776 handler_insert (guint signal_id,
777 gpointer instance,
778 Handler *handler)
779 {
780 HandlerList *hlist;
781
782 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
783
784 hlist = handler_list_ensure (signal_id, instance);
785 if (!hlist->handlers)
786 {
787 hlist->handlers = handler;
788 if (!handler->after)
789 hlist->tail_before = handler;
790 }
791 else if (handler->after)
792 {
793 handler->prev = hlist->tail_after;
794 hlist->tail_after->next = handler;
795 }
796 else
797 {
798 if (hlist->tail_before)
799 {
800 handler->next = hlist->tail_before->next;
801 if (handler->next)
802 handler->next->prev = handler;
803 handler->prev = hlist->tail_before;
804 hlist->tail_before->next = handler;
805 }
806 else /* insert !after handler into a list of only after handlers */
807 {
808 handler->next = hlist->handlers;
809 if (handler->next)
810 handler->next->prev = handler;
811 hlist->handlers = handler;
812 }
813 hlist->tail_before = handler;
814 }
815
816 if (!handler->next)
817 hlist->tail_after = handler;
818 }
819
820 static void
node_update_single_va_closure(SignalNode * node)821 node_update_single_va_closure (SignalNode *node)
822 {
823 GClosure *closure = NULL;
824 gboolean is_after = FALSE;
825
826 /* Fast path single-handler without boxing the arguments in GValues */
827 if (G_TYPE_IS_OBJECT (node->itype) &&
828 (node->flags & (G_SIGNAL_MUST_COLLECT)) == 0 &&
829 (node->emission_hooks == NULL || node->emission_hooks->hooks == NULL))
830 {
831 GSignalFlags run_type;
832 ClassClosure * cc;
833 GBSearchArray *bsa = node->class_closure_bsa;
834
835 if (bsa == NULL || bsa->n_nodes == 0)
836 closure = SINGLE_VA_CLOSURE_EMPTY_MAGIC;
837 else if (bsa->n_nodes == 1)
838 {
839 /* Look for default class closure (can't support non-default as it
840 chains up using GValues */
841 cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
842 if (cc->instance_type == 0)
843 {
844 run_type = node->flags & (G_SIGNAL_RUN_FIRST|G_SIGNAL_RUN_LAST|G_SIGNAL_RUN_CLEANUP);
845 /* Only support *one* of run-first or run-last, not multiple or cleanup */
846 if (run_type == G_SIGNAL_RUN_FIRST ||
847 run_type == G_SIGNAL_RUN_LAST)
848 {
849 closure = cc->closure;
850 is_after = (run_type == G_SIGNAL_RUN_LAST);
851 }
852 }
853 }
854 }
855
856 node->single_va_closure_is_valid = TRUE;
857 node->single_va_closure = closure;
858 node->single_va_closure_is_after = is_after;
859 }
860
861 static inline void
emission_push(Emission * emission)862 emission_push (Emission *emission)
863 {
864 emission->next = g_emissions;
865 g_emissions = emission;
866 }
867
868 static inline void
emission_pop(Emission * emission)869 emission_pop (Emission *emission)
870 {
871 Emission *node, *last = NULL;
872
873 for (node = g_emissions; node; last = node, node = last->next)
874 if (node == emission)
875 {
876 if (last)
877 last->next = node->next;
878 else
879 g_emissions = node->next;
880 return;
881 }
882 g_assert_not_reached ();
883 }
884
885 static inline Emission*
emission_find(guint signal_id,GQuark detail,gpointer instance)886 emission_find (guint signal_id,
887 GQuark detail,
888 gpointer instance)
889 {
890 Emission *emission;
891
892 for (emission = g_emissions; emission; emission = emission->next)
893 if (emission->instance == instance &&
894 emission->ihint.signal_id == signal_id &&
895 emission->ihint.detail == detail)
896 return emission;
897 return NULL;
898 }
899
900 static inline Emission*
emission_find_innermost(gpointer instance)901 emission_find_innermost (gpointer instance)
902 {
903 Emission *emission;
904
905 for (emission = g_emissions; emission; emission = emission->next)
906 if (emission->instance == instance)
907 return emission;
908
909 return NULL;
910 }
911
912 static gint
signal_key_cmp(gconstpointer node1,gconstpointer node2)913 signal_key_cmp (gconstpointer node1,
914 gconstpointer node2)
915 {
916 const SignalKey *key1 = node1, *key2 = node2;
917
918 if (key1->itype == key2->itype)
919 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
920 else
921 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
922 }
923
924 void
_g_signal_init(void)925 _g_signal_init (void)
926 {
927 SIGNAL_LOCK ();
928 if (!g_n_signal_nodes)
929 {
930 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
931 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
932 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
933
934 /* invalid (0) signal_id */
935 g_n_signal_nodes = 1;
936 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
937 g_signal_nodes[0] = NULL;
938 g_handlers = g_hash_table_new (handler_hash, handler_equal);
939 }
940 SIGNAL_UNLOCK ();
941 }
942
943 void
_g_signals_destroy(GType itype)944 _g_signals_destroy (GType itype)
945 {
946 guint i;
947
948 SIGNAL_LOCK ();
949 for (i = 1; i < g_n_signal_nodes; i++)
950 {
951 SignalNode *node = g_signal_nodes[i];
952
953 if (node->itype == itype)
954 {
955 if (node->destroyed)
956 g_warning (G_STRLOC ": signal \"%s\" of type '%s' already destroyed",
957 node->name,
958 type_debug_name (node->itype));
959 else
960 signal_destroy_R (node);
961 }
962 }
963 SIGNAL_UNLOCK ();
964 }
965
966 /**
967 * g_signal_stop_emission:
968 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
969 * @signal_id: the signal identifier, as returned by g_signal_lookup().
970 * @detail: the detail which the signal was emitted with.
971 *
972 * Stops a signal's current emission.
973 *
974 * This will prevent the default method from running, if the signal was
975 * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after"
976 * flag).
977 *
978 * Prints a warning if used on a signal which isn't being emitted.
979 */
980 void
g_signal_stop_emission(gpointer instance,guint signal_id,GQuark detail)981 g_signal_stop_emission (gpointer instance,
982 guint signal_id,
983 GQuark detail)
984 {
985 SignalNode *node;
986
987 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
988 g_return_if_fail (signal_id > 0);
989
990 SIGNAL_LOCK ();
991 node = LOOKUP_SIGNAL_NODE (signal_id);
992 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
993 {
994 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
995 SIGNAL_UNLOCK ();
996 return;
997 }
998 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
999 {
1000 Emission *emission = emission_find (signal_id, detail, instance);
1001
1002 if (emission)
1003 {
1004 if (emission->state == EMISSION_HOOK)
1005 g_warning (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1006 node->name, instance);
1007 else if (emission->state == EMISSION_RUN)
1008 emission->state = EMISSION_STOP;
1009 }
1010 else
1011 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1012 node->name, instance);
1013 }
1014 else
1015 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
1016 SIGNAL_UNLOCK ();
1017 }
1018
1019 static void
signal_finalize_hook(GHookList * hook_list,GHook * hook)1020 signal_finalize_hook (GHookList *hook_list,
1021 GHook *hook)
1022 {
1023 GDestroyNotify destroy = hook->destroy;
1024
1025 if (destroy)
1026 {
1027 hook->destroy = NULL;
1028 SIGNAL_UNLOCK ();
1029 destroy (hook->data);
1030 SIGNAL_LOCK ();
1031 }
1032 }
1033
1034 /**
1035 * g_signal_add_emission_hook:
1036 * @signal_id: the signal identifier, as returned by g_signal_lookup().
1037 * @detail: the detail on which to call the hook.
1038 * @hook_func: (not nullable): a #GSignalEmissionHook function.
1039 * @hook_data: (nullable) (closure hook_func): user data for @hook_func.
1040 * @data_destroy: (nullable) (destroy hook_data): a #GDestroyNotify for @hook_data.
1041 *
1042 * Adds an emission hook for a signal, which will get called for any emission
1043 * of that signal, independent of the instance. This is possible only
1044 * for signals which don't have #G_SIGNAL_NO_HOOKS flag set.
1045 *
1046 * Returns: the hook id, for later use with g_signal_remove_emission_hook().
1047 */
1048 gulong
g_signal_add_emission_hook(guint signal_id,GQuark detail,GSignalEmissionHook hook_func,gpointer hook_data,GDestroyNotify data_destroy)1049 g_signal_add_emission_hook (guint signal_id,
1050 GQuark detail,
1051 GSignalEmissionHook hook_func,
1052 gpointer hook_data,
1053 GDestroyNotify data_destroy)
1054 {
1055 static gulong seq_hook_id = 1;
1056 SignalNode *node;
1057 GHook *hook;
1058 SignalHook *signal_hook;
1059
1060 g_return_val_if_fail (signal_id > 0, 0);
1061 g_return_val_if_fail (hook_func != NULL, 0);
1062
1063 SIGNAL_LOCK ();
1064 node = LOOKUP_SIGNAL_NODE (signal_id);
1065 if (!node || node->destroyed)
1066 {
1067 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1068 SIGNAL_UNLOCK ();
1069 return 0;
1070 }
1071 if (node->flags & G_SIGNAL_NO_HOOKS)
1072 {
1073 g_warning ("%s: signal id '%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id);
1074 SIGNAL_UNLOCK ();
1075 return 0;
1076 }
1077 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1078 {
1079 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1080 SIGNAL_UNLOCK ();
1081 return 0;
1082 }
1083 node->single_va_closure_is_valid = FALSE;
1084 if (!node->emission_hooks)
1085 {
1086 node->emission_hooks = g_new (GHookList, 1);
1087 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
1088 node->emission_hooks->finalize_hook = signal_finalize_hook;
1089 }
1090
1091 node_check_deprecated (node);
1092
1093 hook = g_hook_alloc (node->emission_hooks);
1094 hook->data = hook_data;
1095 hook->func = (gpointer) hook_func;
1096 hook->destroy = data_destroy;
1097 signal_hook = SIGNAL_HOOK (hook);
1098 signal_hook->detail = detail;
1099 node->emission_hooks->seq_id = seq_hook_id;
1100 g_hook_append (node->emission_hooks, hook);
1101 seq_hook_id = node->emission_hooks->seq_id;
1102
1103 SIGNAL_UNLOCK ();
1104
1105 return hook->hook_id;
1106 }
1107
1108 /**
1109 * g_signal_remove_emission_hook:
1110 * @signal_id: the id of the signal
1111 * @hook_id: the id of the emission hook, as returned by
1112 * g_signal_add_emission_hook()
1113 *
1114 * Deletes an emission hook.
1115 */
1116 void
g_signal_remove_emission_hook(guint signal_id,gulong hook_id)1117 g_signal_remove_emission_hook (guint signal_id,
1118 gulong hook_id)
1119 {
1120 SignalNode *node;
1121
1122 g_return_if_fail (signal_id > 0);
1123 g_return_if_fail (hook_id > 0);
1124
1125 SIGNAL_LOCK ();
1126 node = LOOKUP_SIGNAL_NODE (signal_id);
1127 if (!node || node->destroyed)
1128 {
1129 g_warning ("%s: invalid signal id '%u'", G_STRLOC, signal_id);
1130 goto out;
1131 }
1132 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
1133 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
1134
1135 node->single_va_closure_is_valid = FALSE;
1136
1137 out:
1138 SIGNAL_UNLOCK ();
1139 }
1140
1141 static inline guint
signal_parse_name(const gchar * name,GType itype,GQuark * detail_p,gboolean force_quark)1142 signal_parse_name (const gchar *name,
1143 GType itype,
1144 GQuark *detail_p,
1145 gboolean force_quark)
1146 {
1147 const gchar *colon = strchr (name, ':');
1148 guint signal_id;
1149
1150 if (!colon)
1151 {
1152 signal_id = signal_id_lookup (name, itype);
1153 if (signal_id && detail_p)
1154 *detail_p = 0;
1155 }
1156 else if (colon[1] == ':')
1157 {
1158 gchar buffer[32];
1159 guint l = colon - name;
1160
1161 if (colon[2] == '\0')
1162 return 0;
1163
1164 if (l < 32)
1165 {
1166 memcpy (buffer, name, l);
1167 buffer[l] = 0;
1168 signal_id = signal_id_lookup (buffer, itype);
1169 }
1170 else
1171 {
1172 gchar *signal = g_new (gchar, l + 1);
1173
1174 memcpy (signal, name, l);
1175 signal[l] = 0;
1176 signal_id = signal_id_lookup (signal, itype);
1177 g_free (signal);
1178 }
1179
1180 if (signal_id && detail_p)
1181 *detail_p = (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2);
1182 }
1183 else
1184 signal_id = 0;
1185 return signal_id;
1186 }
1187
1188 /**
1189 * g_signal_parse_name:
1190 * @detailed_signal: a string of the form "signal-name::detail".
1191 * @itype: The interface/instance type that introduced "signal-name".
1192 * @signal_id_p: (out): Location to store the signal id.
1193 * @detail_p: (out): Location to store the detail quark.
1194 * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail.
1195 *
1196 * Internal function to parse a signal name into its @signal_id
1197 * and @detail quark.
1198 *
1199 * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values.
1200 */
1201 gboolean
g_signal_parse_name(const gchar * detailed_signal,GType itype,guint * signal_id_p,GQuark * detail_p,gboolean force_detail_quark)1202 g_signal_parse_name (const gchar *detailed_signal,
1203 GType itype,
1204 guint *signal_id_p,
1205 GQuark *detail_p,
1206 gboolean force_detail_quark)
1207 {
1208 SignalNode *node;
1209 GQuark detail = 0;
1210 guint signal_id;
1211
1212 g_return_val_if_fail (detailed_signal != NULL, FALSE);
1213 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
1214
1215 SIGNAL_LOCK ();
1216 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
1217 SIGNAL_UNLOCK ();
1218
1219 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
1220 if (!node || node->destroyed ||
1221 (detail && !(node->flags & G_SIGNAL_DETAILED)))
1222 return FALSE;
1223
1224 if (signal_id_p)
1225 *signal_id_p = signal_id;
1226 if (detail_p)
1227 *detail_p = detail;
1228
1229 return TRUE;
1230 }
1231
1232 /**
1233 * g_signal_stop_emission_by_name:
1234 * @instance: (type GObject.Object): the object whose signal handlers you wish to stop.
1235 * @detailed_signal: a string of the form "signal-name::detail".
1236 *
1237 * Stops a signal's current emission.
1238 *
1239 * This is just like g_signal_stop_emission() except it will look up the
1240 * signal id for you.
1241 */
1242 void
g_signal_stop_emission_by_name(gpointer instance,const gchar * detailed_signal)1243 g_signal_stop_emission_by_name (gpointer instance,
1244 const gchar *detailed_signal)
1245 {
1246 guint signal_id;
1247 GQuark detail = 0;
1248 GType itype;
1249
1250 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1251 g_return_if_fail (detailed_signal != NULL);
1252
1253 SIGNAL_LOCK ();
1254 itype = G_TYPE_FROM_INSTANCE (instance);
1255 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1256 if (signal_id)
1257 {
1258 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1259
1260 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1261 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
1262 else if (!g_type_is_a (itype, node->itype))
1263 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1264 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1265 else
1266 {
1267 Emission *emission = emission_find (signal_id, detail, instance);
1268
1269 if (emission)
1270 {
1271 if (emission->state == EMISSION_HOOK)
1272 g_warning (G_STRLOC ": emission of signal \"%s\" for instance '%p' cannot be stopped from emission hook",
1273 node->name, instance);
1274 else if (emission->state == EMISSION_RUN)
1275 emission->state = EMISSION_STOP;
1276 }
1277 else
1278 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance '%p'",
1279 node->name, instance);
1280 }
1281 }
1282 else
1283 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
1284 G_STRLOC, detailed_signal, instance, g_type_name (itype));
1285 SIGNAL_UNLOCK ();
1286 }
1287
1288 /**
1289 * g_signal_lookup:
1290 * @name: the signal's name.
1291 * @itype: the type that the signal operates on.
1292 *
1293 * Given the name of the signal and the type of object it connects to, gets
1294 * the signal's identifying integer. Emitting the signal by number is
1295 * somewhat faster than using the name each time.
1296 *
1297 * Also tries the ancestors of the given type.
1298 *
1299 * The type class passed as @itype must already have been instantiated (for
1300 * example, using g_type_class_ref()) for this function to work, as signals are
1301 * always installed during class initialization.
1302 *
1303 * See g_signal_new() for details on allowed signal names.
1304 *
1305 * Returns: the signal's identifying number, or 0 if no signal was found.
1306 */
1307 guint
g_signal_lookup(const gchar * name,GType itype)1308 g_signal_lookup (const gchar *name,
1309 GType itype)
1310 {
1311 guint signal_id;
1312 g_return_val_if_fail (name != NULL, 0);
1313 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1314
1315 SIGNAL_LOCK ();
1316 signal_id = signal_id_lookup (name, itype);
1317 SIGNAL_UNLOCK ();
1318 if (!signal_id)
1319 {
1320 /* give elaborate warnings */
1321 if (!g_type_name (itype))
1322 g_warning (G_STRLOC ": unable to look up signal \"%s\" for invalid type id '%"G_GSIZE_FORMAT"'",
1323 name, itype);
1324 else if (!g_signal_is_valid_name (name))
1325 g_warning (G_STRLOC ": unable to look up invalid signal name \"%s\" on type '%s'",
1326 name, g_type_name (itype));
1327 }
1328
1329 return signal_id;
1330 }
1331
1332 /**
1333 * g_signal_list_ids:
1334 * @itype: Instance or interface type.
1335 * @n_ids: Location to store the number of signal ids for @itype.
1336 *
1337 * Lists the signals by id that a certain instance or interface type
1338 * created. Further information about the signals can be acquired through
1339 * g_signal_query().
1340 *
1341 * Returns: (array length=n_ids) (transfer full): Newly allocated array of signal IDs.
1342 */
1343 guint*
g_signal_list_ids(GType itype,guint * n_ids)1344 g_signal_list_ids (GType itype,
1345 guint *n_ids)
1346 {
1347 SignalKey *keys;
1348 GArray *result;
1349 guint n_nodes;
1350 guint i;
1351
1352 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1353 g_return_val_if_fail (n_ids != NULL, NULL);
1354
1355 SIGNAL_LOCK ();
1356 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1357 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1358 result = g_array_new (FALSE, FALSE, sizeof (guint));
1359
1360 for (i = 0; i < n_nodes; i++)
1361 if (keys[i].itype == itype)
1362 {
1363 g_array_append_val (result, keys[i].signal_id);
1364 }
1365 *n_ids = result->len;
1366 SIGNAL_UNLOCK ();
1367 if (!n_nodes)
1368 {
1369 /* give elaborate warnings */
1370 if (!g_type_name (itype))
1371 g_warning (G_STRLOC ": unable to list signals for invalid type id '%"G_GSIZE_FORMAT"'",
1372 itype);
1373 else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype))
1374 g_warning (G_STRLOC ": unable to list signals of non instantiatable type '%s'",
1375 g_type_name (itype));
1376 else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype))
1377 g_warning (G_STRLOC ": unable to list signals of unloaded type '%s'",
1378 g_type_name (itype));
1379 }
1380
1381 return (guint*) g_array_free (result, FALSE);
1382 }
1383
1384 /**
1385 * g_signal_name:
1386 * @signal_id: the signal's identifying number.
1387 *
1388 * Given the signal's identifier, finds its name.
1389 *
1390 * Two different signals may have the same name, if they have differing types.
1391 *
1392 * Returns: (nullable): the signal name, or %NULL if the signal number was invalid.
1393 */
1394 const gchar *
g_signal_name(guint signal_id)1395 g_signal_name (guint signal_id)
1396 {
1397 SignalNode *node;
1398 const gchar *name;
1399
1400 SIGNAL_LOCK ();
1401 node = LOOKUP_SIGNAL_NODE (signal_id);
1402 name = node ? node->name : NULL;
1403 SIGNAL_UNLOCK ();
1404
1405 return (char*) name;
1406 }
1407
1408 /**
1409 * g_signal_query:
1410 * @signal_id: The signal id of the signal to query information for.
1411 * @query: (out caller-allocates) (not optional): A user provided structure that is
1412 * filled in with constant values upon success.
1413 *
1414 * Queries the signal system for in-depth information about a
1415 * specific signal. This function will fill in a user-provided
1416 * structure to hold signal-specific information. If an invalid
1417 * signal id is passed in, the @signal_id member of the #GSignalQuery
1418 * is 0. All members filled into the #GSignalQuery structure should
1419 * be considered constant and have to be left untouched.
1420 */
1421 void
g_signal_query(guint signal_id,GSignalQuery * query)1422 g_signal_query (guint signal_id,
1423 GSignalQuery *query)
1424 {
1425 SignalNode *node;
1426
1427 g_return_if_fail (query != NULL);
1428
1429 SIGNAL_LOCK ();
1430 node = LOOKUP_SIGNAL_NODE (signal_id);
1431 if (!node || node->destroyed)
1432 query->signal_id = 0;
1433 else
1434 {
1435 query->signal_id = node->signal_id;
1436 query->signal_name = node->name;
1437 query->itype = node->itype;
1438 query->signal_flags = node->flags;
1439 query->return_type = node->return_type;
1440 query->n_params = node->n_params;
1441 query->param_types = node->param_types;
1442 }
1443 SIGNAL_UNLOCK ();
1444 }
1445
1446 /**
1447 * g_signal_new:
1448 * @signal_name: the name for the signal
1449 * @itype: the type this signal pertains to. It will also pertain to
1450 * types which are derived from this type.
1451 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1452 * the default handler is to be invoked. You should at least specify
1453 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1454 * @class_offset: The offset of the function pointer in the class structure
1455 * for this type. Used to invoke a class method generically. Pass 0 to
1456 * not associate a class method slot with this signal.
1457 * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1458 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1459 * @c_marshaller: (nullable): the function to translate arrays of parameter
1460 * values to signal emissions into C language callback invocations or %NULL.
1461 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1462 * without a return value.
1463 * @n_params: the number of parameter types to follow.
1464 * @...: a list of types, one for each parameter.
1465 *
1466 * Creates a new signal. (This is usually done in the class initializer.)
1467 *
1468 * A signal name consists of segments consisting of ASCII letters and
1469 * digits, separated by either the `-` or `_` character. The first
1470 * character of a signal name must be a letter. Names which violate these
1471 * rules lead to undefined behaviour. These are the same rules as for property
1472 * naming (see g_param_spec_internal()).
1473 *
1474 * When registering a signal and looking up a signal, either separator can
1475 * be used, but they cannot be mixed. Using `-` is considerably more efficient.
1476 * Using `_` is discouraged.
1477 *
1478 * If 0 is used for @class_offset subclasses cannot override the class handler
1479 * in their class_init method by doing super_class->signal_handler = my_signal_handler.
1480 * Instead they will have to use g_signal_override_class_handler().
1481 *
1482 * If @c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1483 * the marshaller for this signal. In some simple cases, g_signal_new()
1484 * will use a more optimized c_marshaller and va_marshaller for the signal
1485 * instead of g_cclosure_marshal_generic().
1486 *
1487 * If @c_marshaller is non-%NULL, you need to also specify a va_marshaller
1488 * using g_signal_set_va_marshaller() or the generic va_marshaller will
1489 * be used.
1490 *
1491 * Returns: the signal id
1492 */
1493 guint
g_signal_new(const gchar * signal_name,GType itype,GSignalFlags signal_flags,guint class_offset,GSignalAccumulator accumulator,gpointer accu_data,GSignalCMarshaller c_marshaller,GType return_type,guint n_params,...)1494 g_signal_new (const gchar *signal_name,
1495 GType itype,
1496 GSignalFlags signal_flags,
1497 guint class_offset,
1498 GSignalAccumulator accumulator,
1499 gpointer accu_data,
1500 GSignalCMarshaller c_marshaller,
1501 GType return_type,
1502 guint n_params,
1503 ...)
1504 {
1505 va_list args;
1506 guint signal_id;
1507
1508 g_return_val_if_fail (signal_name != NULL, 0);
1509
1510 va_start (args, n_params);
1511
1512 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1513 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1514 accumulator, accu_data, c_marshaller,
1515 return_type, n_params, args);
1516
1517 va_end (args);
1518
1519 return signal_id;
1520 }
1521
1522 /**
1523 * g_signal_new_class_handler:
1524 * @signal_name: the name for the signal
1525 * @itype: the type this signal pertains to. It will also pertain to
1526 * types which are derived from this type.
1527 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1528 * the default handler is to be invoked. You should at least specify
1529 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1530 * @class_handler: (nullable): a #GCallback which acts as class implementation of
1531 * this signal. Used to invoke a class method generically. Pass %NULL to
1532 * not associate a class method with this signal.
1533 * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1534 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1535 * @c_marshaller: (nullable): the function to translate arrays of parameter
1536 * values to signal emissions into C language callback invocations or %NULL.
1537 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1538 * without a return value.
1539 * @n_params: the number of parameter types to follow.
1540 * @...: a list of types, one for each parameter.
1541 *
1542 * Creates a new signal. (This is usually done in the class initializer.)
1543 *
1544 * This is a variant of g_signal_new() that takes a C callback instead
1545 * of a class offset for the signal's class handler. This function
1546 * doesn't need a function pointer exposed in the class structure of
1547 * an object definition, instead the function pointer is passed
1548 * directly and can be overridden by derived classes with
1549 * g_signal_override_class_closure() or
1550 * g_signal_override_class_handler()and chained to with
1551 * g_signal_chain_from_overridden() or
1552 * g_signal_chain_from_overridden_handler().
1553 *
1554 * See g_signal_new() for information about signal names.
1555 *
1556 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1557 * the marshaller for this signal.
1558 *
1559 * Returns: the signal id
1560 *
1561 * Since: 2.18
1562 */
1563 guint
g_signal_new_class_handler(const gchar * signal_name,GType itype,GSignalFlags signal_flags,GCallback class_handler,GSignalAccumulator accumulator,gpointer accu_data,GSignalCMarshaller c_marshaller,GType return_type,guint n_params,...)1564 g_signal_new_class_handler (const gchar *signal_name,
1565 GType itype,
1566 GSignalFlags signal_flags,
1567 GCallback class_handler,
1568 GSignalAccumulator accumulator,
1569 gpointer accu_data,
1570 GSignalCMarshaller c_marshaller,
1571 GType return_type,
1572 guint n_params,
1573 ...)
1574 {
1575 va_list args;
1576 guint signal_id;
1577
1578 g_return_val_if_fail (signal_name != NULL, 0);
1579
1580 va_start (args, n_params);
1581
1582 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1583 class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1584 accumulator, accu_data, c_marshaller,
1585 return_type, n_params, args);
1586
1587 va_end (args);
1588
1589 return signal_id;
1590 }
1591
1592 static inline ClassClosure*
signal_find_class_closure(SignalNode * node,GType itype)1593 signal_find_class_closure (SignalNode *node,
1594 GType itype)
1595 {
1596 GBSearchArray *bsa = node->class_closure_bsa;
1597 ClassClosure *cc;
1598
1599 if (bsa)
1600 {
1601 ClassClosure key;
1602
1603 /* cc->instance_type is 0 for default closure */
1604
1605 if (g_bsearch_array_get_n_nodes (bsa) == 1)
1606 {
1607 cc = g_bsearch_array_get_nth (bsa, &g_class_closure_bconfig, 0);
1608 if (cc && cc->instance_type == 0) /* check for default closure */
1609 return cc;
1610 }
1611
1612 key.instance_type = itype;
1613 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1614 while (!cc && key.instance_type)
1615 {
1616 key.instance_type = g_type_parent (key.instance_type);
1617 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1618 }
1619 }
1620 else
1621 cc = NULL;
1622 return cc;
1623 }
1624
1625 static inline GClosure*
signal_lookup_closure(SignalNode * node,GTypeInstance * instance)1626 signal_lookup_closure (SignalNode *node,
1627 GTypeInstance *instance)
1628 {
1629 ClassClosure *cc;
1630
1631 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1632 return cc ? cc->closure : NULL;
1633 }
1634
1635 static void
signal_add_class_closure(SignalNode * node,GType itype,GClosure * closure)1636 signal_add_class_closure (SignalNode *node,
1637 GType itype,
1638 GClosure *closure)
1639 {
1640 ClassClosure key;
1641
1642 node->single_va_closure_is_valid = FALSE;
1643
1644 if (!node->class_closure_bsa)
1645 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1646 key.instance_type = itype;
1647 key.closure = g_closure_ref (closure);
1648 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1649 &g_class_closure_bconfig,
1650 &key);
1651 g_closure_sink (closure);
1652 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1653 {
1654 g_closure_set_marshal (closure, node->c_marshaller);
1655 if (node->va_marshaller)
1656 _g_closure_set_va_marshal (closure, node->va_marshaller);
1657 }
1658 }
1659
1660 /**
1661 * g_signal_newv:
1662 * @signal_name: the name for the signal
1663 * @itype: the type this signal pertains to. It will also pertain to
1664 * types which are derived from this type
1665 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1666 * the default handler is to be invoked. You should at least specify
1667 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1668 * @class_closure: (nullable): The closure to invoke on signal emission;
1669 * may be %NULL
1670 * @accumulator: (nullable): the accumulator for this signal; may be %NULL
1671 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator
1672 * @c_marshaller: (nullable): the function to translate arrays of
1673 * parameter values to signal emissions into C language callback
1674 * invocations or %NULL
1675 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1676 * without a return value
1677 * @n_params: the length of @param_types
1678 * @param_types: (array length=n_params) (nullable): an array of types, one for
1679 * each parameter (may be %NULL if @n_params is zero)
1680 *
1681 * Creates a new signal. (This is usually done in the class initializer.)
1682 *
1683 * See g_signal_new() for details on allowed signal names.
1684 *
1685 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1686 * the marshaller for this signal.
1687 *
1688 * Returns: the signal id
1689 */
1690 guint
g_signal_newv(const gchar * signal_name,GType itype,GSignalFlags signal_flags,GClosure * class_closure,GSignalAccumulator accumulator,gpointer accu_data,GSignalCMarshaller c_marshaller,GType return_type,guint n_params,GType * param_types)1691 g_signal_newv (const gchar *signal_name,
1692 GType itype,
1693 GSignalFlags signal_flags,
1694 GClosure *class_closure,
1695 GSignalAccumulator accumulator,
1696 gpointer accu_data,
1697 GSignalCMarshaller c_marshaller,
1698 GType return_type,
1699 guint n_params,
1700 GType *param_types)
1701 {
1702 const gchar *name;
1703 gchar *signal_name_copy = NULL;
1704 guint signal_id, i;
1705 SignalNode *node;
1706 GSignalCMarshaller builtin_c_marshaller;
1707 GSignalCVaMarshaller builtin_va_marshaller;
1708 GSignalCVaMarshaller va_marshaller;
1709
1710 g_return_val_if_fail (signal_name != NULL, 0);
1711 g_return_val_if_fail (g_signal_is_valid_name (signal_name), 0);
1712 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1713 if (n_params)
1714 g_return_val_if_fail (param_types != NULL, 0);
1715 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1716 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1717 g_return_val_if_fail (accumulator == NULL, 0);
1718 if (!accumulator)
1719 g_return_val_if_fail (accu_data == NULL, 0);
1720 g_return_val_if_fail ((signal_flags & G_SIGNAL_ACCUMULATOR_FIRST_RUN) == 0, 0);
1721
1722 if (!is_canonical (signal_name))
1723 {
1724 signal_name_copy = g_strdup (signal_name);
1725 canonicalize_key (signal_name_copy);
1726 name = signal_name_copy;
1727 }
1728 else
1729 {
1730 name = signal_name;
1731 }
1732
1733 SIGNAL_LOCK ();
1734
1735 signal_id = signal_id_lookup (name, itype);
1736 node = LOOKUP_SIGNAL_NODE (signal_id);
1737 if (node && !node->destroyed)
1738 {
1739 g_warning (G_STRLOC ": signal \"%s\" already exists in the '%s' %s",
1740 name,
1741 type_debug_name (node->itype),
1742 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1743 g_free (signal_name_copy);
1744 SIGNAL_UNLOCK ();
1745 return 0;
1746 }
1747 if (node && node->itype != itype)
1748 {
1749 g_warning (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'",
1750 name,
1751 type_debug_name (itype),
1752 type_debug_name (node->itype));
1753 g_free (signal_name_copy);
1754 SIGNAL_UNLOCK ();
1755 return 0;
1756 }
1757 for (i = 0; i < n_params; i++)
1758 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1759 {
1760 g_warning (G_STRLOC ": parameter %d of type '%s' for signal \"%s::%s\" is not a value type",
1761 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1762 g_free (signal_name_copy);
1763 SIGNAL_UNLOCK ();
1764 return 0;
1765 }
1766 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1767 {
1768 g_warning (G_STRLOC ": return value of type '%s' for signal \"%s::%s\" is not a value type",
1769 type_debug_name (return_type), type_debug_name (itype), name);
1770 g_free (signal_name_copy);
1771 SIGNAL_UNLOCK ();
1772 return 0;
1773 }
1774
1775 /* setup permanent portion of signal node */
1776 if (!node)
1777 {
1778 SignalKey key;
1779
1780 signal_id = g_n_signal_nodes++;
1781 node = g_new (SignalNode, 1);
1782 node->signal_id = signal_id;
1783 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1784 g_signal_nodes[signal_id] = node;
1785 node->itype = itype;
1786 key.itype = itype;
1787 key.signal_id = signal_id;
1788 node->name = g_intern_string (name);
1789 key.quark = g_quark_from_string (name);
1790 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1791
1792 TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1793 }
1794 node->destroyed = FALSE;
1795
1796 /* setup reinitializable portion */
1797 node->single_va_closure_is_valid = FALSE;
1798 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1799 node->n_params = n_params;
1800 node->param_types = g_memdup2 (param_types, sizeof (GType) * n_params);
1801 node->return_type = return_type;
1802 node->class_closure_bsa = NULL;
1803 if (accumulator)
1804 {
1805 node->accumulator = g_new (SignalAccumulator, 1);
1806 node->accumulator->func = accumulator;
1807 node->accumulator->data = accu_data;
1808 }
1809 else
1810 node->accumulator = NULL;
1811
1812 builtin_c_marshaller = NULL;
1813 builtin_va_marshaller = NULL;
1814
1815 /* Pick up built-in va marshallers for standard types, and
1816 instead of generic marshaller if no marshaller specified */
1817 if (n_params == 0 && return_type == G_TYPE_NONE)
1818 {
1819 builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1820 builtin_va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1821 }
1822 else if (n_params == 1 && return_type == G_TYPE_NONE)
1823 {
1824 #define ADD_CHECK(__type__) \
1825 else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__)) \
1826 { \
1827 builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__; \
1828 builtin_va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v; \
1829 }
1830
1831 if (0) {}
1832 ADD_CHECK (BOOLEAN)
1833 ADD_CHECK (CHAR)
1834 ADD_CHECK (UCHAR)
1835 ADD_CHECK (INT)
1836 ADD_CHECK (UINT)
1837 ADD_CHECK (LONG)
1838 ADD_CHECK (ULONG)
1839 ADD_CHECK (ENUM)
1840 ADD_CHECK (FLAGS)
1841 ADD_CHECK (FLOAT)
1842 ADD_CHECK (DOUBLE)
1843 ADD_CHECK (STRING)
1844 ADD_CHECK (PARAM)
1845 ADD_CHECK (BOXED)
1846 ADD_CHECK (POINTER)
1847 ADD_CHECK (OBJECT)
1848 ADD_CHECK (VARIANT)
1849 }
1850
1851 if (c_marshaller == NULL)
1852 {
1853 if (builtin_c_marshaller)
1854 {
1855 c_marshaller = builtin_c_marshaller;
1856 va_marshaller = builtin_va_marshaller;
1857 }
1858 else
1859 {
1860 c_marshaller = g_cclosure_marshal_generic;
1861 va_marshaller = g_cclosure_marshal_generic_va;
1862 }
1863 }
1864 else
1865 va_marshaller = NULL;
1866
1867 node->c_marshaller = c_marshaller;
1868 node->va_marshaller = va_marshaller;
1869 node->emission_hooks = NULL;
1870 if (class_closure)
1871 signal_add_class_closure (node, 0, class_closure);
1872
1873 SIGNAL_UNLOCK ();
1874
1875 g_free (signal_name_copy);
1876
1877 return signal_id;
1878 }
1879
1880 /**
1881 * g_signal_set_va_marshaller:
1882 * @signal_id: the signal id
1883 * @instance_type: the instance type on which to set the marshaller.
1884 * @va_marshaller: the marshaller to set.
1885 *
1886 * Change the #GSignalCVaMarshaller used for a given signal. This is a
1887 * specialised form of the marshaller that can often be used for the
1888 * common case of a single connected signal handler and avoids the
1889 * overhead of #GValue. Its use is optional.
1890 *
1891 * Since: 2.32
1892 */
1893 void
g_signal_set_va_marshaller(guint signal_id,GType instance_type,GSignalCVaMarshaller va_marshaller)1894 g_signal_set_va_marshaller (guint signal_id,
1895 GType instance_type,
1896 GSignalCVaMarshaller va_marshaller)
1897 {
1898 SignalNode *node;
1899
1900 g_return_if_fail (signal_id > 0);
1901 g_return_if_fail (va_marshaller != NULL);
1902
1903 SIGNAL_LOCK ();
1904 node = LOOKUP_SIGNAL_NODE (signal_id);
1905 if (node)
1906 {
1907 node->va_marshaller = va_marshaller;
1908 if (node->class_closure_bsa)
1909 {
1910 ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1911 if (cc->closure->marshal == node->c_marshaller)
1912 _g_closure_set_va_marshal (cc->closure, va_marshaller);
1913 }
1914
1915 node->single_va_closure_is_valid = FALSE;
1916 }
1917
1918 SIGNAL_UNLOCK ();
1919 }
1920
1921
1922 /**
1923 * g_signal_new_valist:
1924 * @signal_name: the name for the signal
1925 * @itype: the type this signal pertains to. It will also pertain to
1926 * types which are derived from this type.
1927 * @signal_flags: a combination of #GSignalFlags specifying detail of when
1928 * the default handler is to be invoked. You should at least specify
1929 * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1930 * @class_closure: (nullable): The closure to invoke on signal emission; may be %NULL.
1931 * @accumulator: (nullable): the accumulator for this signal; may be %NULL.
1932 * @accu_data: (nullable) (closure accumulator): user data for the @accumulator.
1933 * @c_marshaller: (nullable): the function to translate arrays of parameter
1934 * values to signal emissions into C language callback invocations or %NULL.
1935 * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1936 * without a return value.
1937 * @n_params: the number of parameter types in @args.
1938 * @args: va_list of #GType, one for each parameter.
1939 *
1940 * Creates a new signal. (This is usually done in the class initializer.)
1941 *
1942 * See g_signal_new() for details on allowed signal names.
1943 *
1944 * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1945 * the marshaller for this signal.
1946 *
1947 * Returns: the signal id
1948 */
1949 guint
g_signal_new_valist(const gchar * signal_name,GType itype,GSignalFlags signal_flags,GClosure * class_closure,GSignalAccumulator accumulator,gpointer accu_data,GSignalCMarshaller c_marshaller,GType return_type,guint n_params,va_list args)1950 g_signal_new_valist (const gchar *signal_name,
1951 GType itype,
1952 GSignalFlags signal_flags,
1953 GClosure *class_closure,
1954 GSignalAccumulator accumulator,
1955 gpointer accu_data,
1956 GSignalCMarshaller c_marshaller,
1957 GType return_type,
1958 guint n_params,
1959 va_list args)
1960 {
1961 /* Somewhat arbitrarily reserve 200 bytes. That should cover the majority
1962 * of cases where n_params is small and still be small enough for what we
1963 * want to put on the stack. */
1964 GType param_types_stack[200 / sizeof (GType)];
1965 GType *param_types_heap = NULL;
1966 GType *param_types;
1967 guint i;
1968 guint signal_id;
1969
1970 param_types = param_types_stack;
1971 if (n_params > 0)
1972 {
1973 if (G_UNLIKELY (n_params > G_N_ELEMENTS (param_types_stack)))
1974 {
1975 param_types_heap = g_new (GType, n_params);
1976 param_types = param_types_heap;
1977 }
1978
1979 for (i = 0; i < n_params; i++)
1980 param_types[i] = va_arg (args, GType);
1981 }
1982
1983 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1984 class_closure, accumulator, accu_data, c_marshaller,
1985 return_type, n_params, param_types);
1986 g_free (param_types_heap);
1987
1988 return signal_id;
1989 }
1990
1991 static void
signal_destroy_R(SignalNode * signal_node)1992 signal_destroy_R (SignalNode *signal_node)
1993 {
1994 SignalNode node = *signal_node;
1995
1996 signal_node->destroyed = TRUE;
1997
1998 /* reentrancy caution, zero out real contents first */
1999 signal_node->single_va_closure_is_valid = FALSE;
2000 signal_node->n_params = 0;
2001 signal_node->param_types = NULL;
2002 signal_node->return_type = 0;
2003 signal_node->class_closure_bsa = NULL;
2004 signal_node->accumulator = NULL;
2005 signal_node->c_marshaller = NULL;
2006 signal_node->va_marshaller = NULL;
2007 signal_node->emission_hooks = NULL;
2008
2009 #ifdef G_ENABLE_DEBUG
2010 /* check current emissions */
2011 {
2012 Emission *emission;
2013
2014 for (emission = g_emissions; emission; emission = emission->next)
2015 if (emission->ihint.signal_id == node.signal_id)
2016 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')",
2017 node.name, emission->instance);
2018 }
2019 #endif
2020
2021 /* free contents that need to
2022 */
2023 SIGNAL_UNLOCK ();
2024 g_free (node.param_types);
2025 if (node.class_closure_bsa)
2026 {
2027 guint i;
2028
2029 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
2030 {
2031 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
2032
2033 g_closure_unref (cc->closure);
2034 }
2035 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
2036 }
2037 g_free (node.accumulator);
2038 if (node.emission_hooks)
2039 {
2040 g_hook_list_clear (node.emission_hooks);
2041 g_free (node.emission_hooks);
2042 }
2043 SIGNAL_LOCK ();
2044 }
2045
2046 /**
2047 * g_signal_override_class_closure:
2048 * @signal_id: the signal id
2049 * @instance_type: the instance type on which to override the class closure
2050 * for the signal.
2051 * @class_closure: the closure.
2052 *
2053 * Overrides the class closure (i.e. the default handler) for the given signal
2054 * for emissions on instances of @instance_type. @instance_type must be derived
2055 * from the type to which the signal belongs.
2056 *
2057 * See g_signal_chain_from_overridden() and
2058 * g_signal_chain_from_overridden_handler() for how to chain up to the
2059 * parent class closure from inside the overridden one.
2060 */
2061 void
g_signal_override_class_closure(guint signal_id,GType instance_type,GClosure * class_closure)2062 g_signal_override_class_closure (guint signal_id,
2063 GType instance_type,
2064 GClosure *class_closure)
2065 {
2066 SignalNode *node;
2067
2068 g_return_if_fail (signal_id > 0);
2069 g_return_if_fail (class_closure != NULL);
2070
2071 SIGNAL_LOCK ();
2072 node = LOOKUP_SIGNAL_NODE (signal_id);
2073 node_check_deprecated (node);
2074 if (!g_type_is_a (instance_type, node->itype))
2075 g_warning ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
2076 else
2077 {
2078 ClassClosure *cc = signal_find_class_closure (node, instance_type);
2079
2080 if (cc && cc->instance_type == instance_type)
2081 g_warning ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
2082 else
2083 signal_add_class_closure (node, instance_type, class_closure);
2084 }
2085 SIGNAL_UNLOCK ();
2086 }
2087
2088 /**
2089 * g_signal_override_class_handler:
2090 * @signal_name: the name for the signal
2091 * @instance_type: the instance type on which to override the class handler
2092 * for the signal.
2093 * @class_handler: the handler.
2094 *
2095 * Overrides the class closure (i.e. the default handler) for the
2096 * given signal for emissions on instances of @instance_type with
2097 * callback @class_handler. @instance_type must be derived from the
2098 * type to which the signal belongs.
2099 *
2100 * See g_signal_chain_from_overridden() and
2101 * g_signal_chain_from_overridden_handler() for how to chain up to the
2102 * parent class closure from inside the overridden one.
2103 *
2104 * Since: 2.18
2105 */
2106 void
g_signal_override_class_handler(const gchar * signal_name,GType instance_type,GCallback class_handler)2107 g_signal_override_class_handler (const gchar *signal_name,
2108 GType instance_type,
2109 GCallback class_handler)
2110 {
2111 guint signal_id;
2112
2113 g_return_if_fail (signal_name != NULL);
2114 g_return_if_fail (instance_type != G_TYPE_NONE);
2115 g_return_if_fail (class_handler != NULL);
2116
2117 signal_id = g_signal_lookup (signal_name, instance_type);
2118
2119 if (signal_id)
2120 g_signal_override_class_closure (signal_id, instance_type,
2121 g_cclosure_new (class_handler, NULL, NULL));
2122 else
2123 g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
2124 G_STRLOC, signal_name, instance_type);
2125
2126 }
2127
2128 /**
2129 * g_signal_chain_from_overridden:
2130 * @instance_and_params: (array) the argument list of the signal emission.
2131 * The first element in the array is a #GValue for the instance the signal
2132 * is being emitted on. The rest are any arguments to be passed to the signal.
2133 * @return_value: Location for the return value.
2134 *
2135 * Calls the original class closure of a signal. This function should only
2136 * be called from an overridden class closure; see
2137 * g_signal_override_class_closure() and
2138 * g_signal_override_class_handler().
2139 */
2140 void
g_signal_chain_from_overridden(const GValue * instance_and_params,GValue * return_value)2141 g_signal_chain_from_overridden (const GValue *instance_and_params,
2142 GValue *return_value)
2143 {
2144 GType chain_type = 0, restore_type = 0;
2145 Emission *emission = NULL;
2146 GClosure *closure = NULL;
2147 guint n_params = 0;
2148 gpointer instance;
2149
2150 g_return_if_fail (instance_and_params != NULL);
2151 instance = g_value_peek_pointer (instance_and_params);
2152 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2153
2154 SIGNAL_LOCK ();
2155 emission = emission_find_innermost (instance);
2156 if (emission)
2157 {
2158 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2159
2160 g_assert (node != NULL); /* paranoid */
2161
2162 /* we should probably do the same parameter checks as g_signal_emit() here.
2163 */
2164 if (emission->chain_type != G_TYPE_NONE)
2165 {
2166 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2167
2168 g_assert (cc != NULL); /* closure currently in call stack */
2169
2170 n_params = node->n_params;
2171 restore_type = cc->instance_type;
2172 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2173 if (cc && cc->instance_type != restore_type)
2174 {
2175 closure = cc->closure;
2176 chain_type = cc->instance_type;
2177 }
2178 }
2179 else
2180 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2181 }
2182 else
2183 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2184
2185 if (closure)
2186 {
2187 emission->chain_type = chain_type;
2188 SIGNAL_UNLOCK ();
2189 g_closure_invoke (closure,
2190 return_value,
2191 n_params + 1,
2192 instance_and_params,
2193 &emission->ihint);
2194 SIGNAL_LOCK ();
2195 emission->chain_type = restore_type;
2196 }
2197 SIGNAL_UNLOCK ();
2198 }
2199
2200 /**
2201 * g_signal_chain_from_overridden_handler: (skip)
2202 * @instance: (type GObject.TypeInstance): the instance the signal is being
2203 * emitted on.
2204 * @...: parameters to be passed to the parent class closure, followed by a
2205 * location for the return value. If the return type of the signal
2206 * is #G_TYPE_NONE, the return value location can be omitted.
2207 *
2208 * Calls the original class closure of a signal. This function should
2209 * only be called from an overridden class closure; see
2210 * g_signal_override_class_closure() and
2211 * g_signal_override_class_handler().
2212 *
2213 * Since: 2.18
2214 */
2215 void
g_signal_chain_from_overridden_handler(gpointer instance,...)2216 g_signal_chain_from_overridden_handler (gpointer instance,
2217 ...)
2218 {
2219 GType chain_type = 0, restore_type = 0;
2220 Emission *emission = NULL;
2221 GClosure *closure = NULL;
2222 SignalNode *node;
2223 guint n_params = 0;
2224
2225 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2226
2227 SIGNAL_LOCK ();
2228 emission = emission_find_innermost (instance);
2229 if (emission)
2230 {
2231 node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2232
2233 g_assert (node != NULL); /* paranoid */
2234
2235 /* we should probably do the same parameter checks as g_signal_emit() here.
2236 */
2237 if (emission->chain_type != G_TYPE_NONE)
2238 {
2239 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2240
2241 g_assert (cc != NULL); /* closure currently in call stack */
2242
2243 n_params = node->n_params;
2244 restore_type = cc->instance_type;
2245 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2246 if (cc && cc->instance_type != restore_type)
2247 {
2248 closure = cc->closure;
2249 chain_type = cc->instance_type;
2250 }
2251 }
2252 else
2253 g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2254 }
2255 else
2256 g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2257
2258 if (closure)
2259 {
2260 GValue *instance_and_params;
2261 GType signal_return_type;
2262 GValue *param_values;
2263 va_list var_args;
2264 guint i;
2265
2266 va_start (var_args, instance);
2267
2268 signal_return_type = node->return_type;
2269 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
2270 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
2271 param_values = instance_and_params + 1;
2272
2273 for (i = 0; i < node->n_params; i++)
2274 {
2275 gchar *error;
2276 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2277 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2278
2279 SIGNAL_UNLOCK ();
2280 G_VALUE_COLLECT_INIT (param_values + i, ptype,
2281 var_args,
2282 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2283 &error);
2284 if (error)
2285 {
2286 g_warning ("%s: %s", G_STRLOC, error);
2287 g_free (error);
2288
2289 /* we purposely leak the value here, it might not be
2290 * in a correct state if an error condition occurred
2291 */
2292 while (i--)
2293 g_value_unset (param_values + i);
2294
2295 va_end (var_args);
2296 return;
2297 }
2298 SIGNAL_LOCK ();
2299 }
2300
2301 SIGNAL_UNLOCK ();
2302 instance_and_params->g_type = 0;
2303 g_value_init_from_instance (instance_and_params, instance);
2304 SIGNAL_LOCK ();
2305
2306 emission->chain_type = chain_type;
2307 SIGNAL_UNLOCK ();
2308
2309 if (signal_return_type == G_TYPE_NONE)
2310 {
2311 g_closure_invoke (closure,
2312 NULL,
2313 n_params + 1,
2314 instance_and_params,
2315 &emission->ihint);
2316 }
2317 else
2318 {
2319 GValue return_value = G_VALUE_INIT;
2320 gchar *error = NULL;
2321 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2322 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2323
2324 g_value_init (&return_value, rtype);
2325
2326 g_closure_invoke (closure,
2327 &return_value,
2328 n_params + 1,
2329 instance_and_params,
2330 &emission->ihint);
2331
2332 G_VALUE_LCOPY (&return_value,
2333 var_args,
2334 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2335 &error);
2336 if (!error)
2337 {
2338 g_value_unset (&return_value);
2339 }
2340 else
2341 {
2342 g_warning ("%s: %s", G_STRLOC, error);
2343 g_free (error);
2344
2345 /* we purposely leak the value here, it might not be
2346 * in a correct state if an error condition occurred
2347 */
2348 }
2349 }
2350
2351 for (i = 0; i < n_params; i++)
2352 g_value_unset (param_values + i);
2353 g_value_unset (instance_and_params);
2354
2355 va_end (var_args);
2356
2357 SIGNAL_LOCK ();
2358 emission->chain_type = restore_type;
2359 }
2360 SIGNAL_UNLOCK ();
2361 }
2362
2363 /**
2364 * g_signal_get_invocation_hint:
2365 * @instance: (type GObject.Object): the instance to query
2366 *
2367 * Returns the invocation hint of the innermost signal emission of instance.
2368 *
2369 * Returns: (transfer none) (nullable): the invocation hint of the innermost
2370 * signal emission, or %NULL if not found.
2371 */
2372 GSignalInvocationHint*
g_signal_get_invocation_hint(gpointer instance)2373 g_signal_get_invocation_hint (gpointer instance)
2374 {
2375 Emission *emission = NULL;
2376
2377 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2378
2379 SIGNAL_LOCK ();
2380 emission = emission_find_innermost (instance);
2381 SIGNAL_UNLOCK ();
2382
2383 return emission ? &emission->ihint : NULL;
2384 }
2385
2386 /**
2387 * g_signal_connect_closure_by_id:
2388 * @instance: (type GObject.Object): the instance to connect to.
2389 * @signal_id: the id of the signal.
2390 * @detail: the detail.
2391 * @closure: (not nullable): the closure to connect.
2392 * @after: whether the handler should be called before or after the
2393 * default handler of the signal.
2394 *
2395 * Connects a closure to a signal for a particular object.
2396 *
2397 * Returns: the handler ID (always greater than 0 for successful connections)
2398 */
2399 gulong
g_signal_connect_closure_by_id(gpointer instance,guint signal_id,GQuark detail,GClosure * closure,gboolean after)2400 g_signal_connect_closure_by_id (gpointer instance,
2401 guint signal_id,
2402 GQuark detail,
2403 GClosure *closure,
2404 gboolean after)
2405 {
2406 SignalNode *node;
2407 gulong handler_seq_no = 0;
2408
2409 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2410 g_return_val_if_fail (signal_id > 0, 0);
2411 g_return_val_if_fail (closure != NULL, 0);
2412
2413 SIGNAL_LOCK ();
2414 node = LOOKUP_SIGNAL_NODE (signal_id);
2415 if (node)
2416 {
2417 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2418 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2419 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2420 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2421 else
2422 {
2423 Handler *handler = handler_new (signal_id, instance, after);
2424
2425 if (G_TYPE_IS_OBJECT (node->itype))
2426 _g_object_set_has_signal_handler ((GObject *)instance);
2427
2428 handler_seq_no = handler->sequential_number;
2429 handler->detail = detail;
2430 handler->closure = g_closure_ref (closure);
2431 g_closure_sink (closure);
2432 add_invalid_closure_notify (handler, instance);
2433 handler_insert (signal_id, instance, handler);
2434 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2435 {
2436 g_closure_set_marshal (closure, node->c_marshaller);
2437 if (node->va_marshaller)
2438 _g_closure_set_va_marshal (closure, node->va_marshaller);
2439 }
2440 }
2441 }
2442 else
2443 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2444 SIGNAL_UNLOCK ();
2445
2446 return handler_seq_no;
2447 }
2448
2449 /**
2450 * g_signal_connect_closure:
2451 * @instance: (type GObject.Object): the instance to connect to.
2452 * @detailed_signal: a string of the form "signal-name::detail".
2453 * @closure: (not nullable): the closure to connect.
2454 * @after: whether the handler should be called before or after the
2455 * default handler of the signal.
2456 *
2457 * Connects a closure to a signal for a particular object.
2458 *
2459 * Returns: the handler ID (always greater than 0 for successful connections)
2460 */
2461 gulong
g_signal_connect_closure(gpointer instance,const gchar * detailed_signal,GClosure * closure,gboolean after)2462 g_signal_connect_closure (gpointer instance,
2463 const gchar *detailed_signal,
2464 GClosure *closure,
2465 gboolean after)
2466 {
2467 guint signal_id;
2468 gulong handler_seq_no = 0;
2469 GQuark detail = 0;
2470 GType itype;
2471
2472 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2473 g_return_val_if_fail (detailed_signal != NULL, 0);
2474 g_return_val_if_fail (closure != NULL, 0);
2475
2476 SIGNAL_LOCK ();
2477 itype = G_TYPE_FROM_INSTANCE (instance);
2478 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2479 if (signal_id)
2480 {
2481 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2482
2483 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2484 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2485 else if (!g_type_is_a (itype, node->itype))
2486 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2487 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2488 else
2489 {
2490 Handler *handler = handler_new (signal_id, instance, after);
2491
2492 if (G_TYPE_IS_OBJECT (node->itype))
2493 _g_object_set_has_signal_handler ((GObject *)instance);
2494
2495 handler_seq_no = handler->sequential_number;
2496 handler->detail = detail;
2497 handler->closure = g_closure_ref (closure);
2498 g_closure_sink (closure);
2499 add_invalid_closure_notify (handler, instance);
2500 handler_insert (signal_id, instance, handler);
2501 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2502 {
2503 g_closure_set_marshal (handler->closure, node->c_marshaller);
2504 if (node->va_marshaller)
2505 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2506 }
2507 }
2508 }
2509 else
2510 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2511 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2512 SIGNAL_UNLOCK ();
2513
2514 return handler_seq_no;
2515 }
2516
2517 static void
node_check_deprecated(const SignalNode * node)2518 node_check_deprecated (const SignalNode *node)
2519 {
2520 static const gchar * g_enable_diagnostic = NULL;
2521
2522 if (G_UNLIKELY (!g_enable_diagnostic))
2523 {
2524 g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2525 if (!g_enable_diagnostic)
2526 g_enable_diagnostic = "0";
2527 }
2528
2529 if (g_enable_diagnostic[0] == '1')
2530 {
2531 if (node->flags & G_SIGNAL_DEPRECATED)
2532 {
2533 g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2534 "anymore. It will be removed in a future version.",
2535 type_debug_name (node->itype), node->name);
2536 }
2537 }
2538 }
2539
2540 /**
2541 * g_signal_connect_data:
2542 * @instance: (type GObject.Object): the instance to connect to.
2543 * @detailed_signal: a string of the form "signal-name::detail".
2544 * @c_handler: (not nullable): the #GCallback to connect.
2545 * @data: (nullable) (closure c_handler): data to pass to @c_handler calls.
2546 * @destroy_data: (nullable) (destroy data): a #GClosureNotify for @data.
2547 * @connect_flags: a combination of #GConnectFlags.
2548 *
2549 * Connects a #GCallback function to a signal for a particular object. Similar
2550 * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2551 * which will be called when the signal handler is disconnected and no longer
2552 * used. Specify @connect_flags if you need `..._after()` or
2553 * `..._swapped()` variants of this function.
2554 *
2555 * Returns: the handler ID (always greater than 0 for successful connections)
2556 */
2557 gulong
g_signal_connect_data(gpointer instance,const gchar * detailed_signal,GCallback c_handler,gpointer data,GClosureNotify destroy_data,GConnectFlags connect_flags)2558 g_signal_connect_data (gpointer instance,
2559 const gchar *detailed_signal,
2560 GCallback c_handler,
2561 gpointer data,
2562 GClosureNotify destroy_data,
2563 GConnectFlags connect_flags)
2564 {
2565 guint signal_id;
2566 gulong handler_seq_no = 0;
2567 GQuark detail = 0;
2568 GType itype;
2569 gboolean swapped, after;
2570
2571 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2572 g_return_val_if_fail (detailed_signal != NULL, 0);
2573 g_return_val_if_fail (c_handler != NULL, 0);
2574
2575 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2576 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2577
2578 SIGNAL_LOCK ();
2579 itype = G_TYPE_FROM_INSTANCE (instance);
2580 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2581 if (signal_id)
2582 {
2583 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2584
2585 node_check_deprecated (node);
2586
2587 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2588 g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2589 else if (!g_type_is_a (itype, node->itype))
2590 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2591 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2592 else
2593 {
2594 Handler *handler = handler_new (signal_id, instance, after);
2595
2596 if (G_TYPE_IS_OBJECT (node->itype))
2597 _g_object_set_has_signal_handler ((GObject *)instance);
2598
2599 handler_seq_no = handler->sequential_number;
2600 handler->detail = detail;
2601 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2602 g_closure_sink (handler->closure);
2603 handler_insert (signal_id, instance, handler);
2604 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2605 {
2606 g_closure_set_marshal (handler->closure, node->c_marshaller);
2607 if (node->va_marshaller)
2608 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2609 }
2610 }
2611 }
2612 else
2613 g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2614 G_STRLOC, detailed_signal, instance, g_type_name (itype));
2615 SIGNAL_UNLOCK ();
2616
2617 return handler_seq_no;
2618 }
2619
2620 /**
2621 * g_signal_handler_block:
2622 * @instance: (type GObject.Object): The instance to block the signal handler of.
2623 * @handler_id: Handler id of the handler to be blocked.
2624 *
2625 * Blocks a handler of an instance so it will not be called during any
2626 * signal emissions unless it is unblocked again. Thus "blocking" a
2627 * signal handler means to temporarily deactivate it, a signal handler
2628 * has to be unblocked exactly the same amount of times it has been
2629 * blocked before to become active again.
2630 *
2631 * The @handler_id has to be a valid signal handler id, connected to a
2632 * signal of @instance.
2633 */
2634 void
g_signal_handler_block(gpointer instance,gulong handler_id)2635 g_signal_handler_block (gpointer instance,
2636 gulong handler_id)
2637 {
2638 Handler *handler;
2639
2640 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2641 g_return_if_fail (handler_id > 0);
2642
2643 SIGNAL_LOCK ();
2644 handler = handler_lookup (instance, handler_id, NULL, NULL);
2645 if (handler)
2646 {
2647 #ifndef G_DISABLE_CHECKS
2648 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2649 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2650 #endif
2651 handler->block_count += 1;
2652 }
2653 else
2654 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2655 SIGNAL_UNLOCK ();
2656 }
2657
2658 /**
2659 * g_signal_handler_unblock:
2660 * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2661 * @handler_id: Handler id of the handler to be unblocked.
2662 *
2663 * Undoes the effect of a previous g_signal_handler_block() call. A
2664 * blocked handler is skipped during signal emissions and will not be
2665 * invoked, unblocking it (for exactly the amount of times it has been
2666 * blocked before) reverts its "blocked" state, so the handler will be
2667 * recognized by the signal system and is called upon future or
2668 * currently ongoing signal emissions (since the order in which
2669 * handlers are called during signal emissions is deterministic,
2670 * whether the unblocked handler in question is called as part of a
2671 * currently ongoing emission depends on how far that emission has
2672 * proceeded yet).
2673 *
2674 * The @handler_id has to be a valid id of a signal handler that is
2675 * connected to a signal of @instance and is currently blocked.
2676 */
2677 void
g_signal_handler_unblock(gpointer instance,gulong handler_id)2678 g_signal_handler_unblock (gpointer instance,
2679 gulong handler_id)
2680 {
2681 Handler *handler;
2682
2683 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2684 g_return_if_fail (handler_id > 0);
2685
2686 SIGNAL_LOCK ();
2687 handler = handler_lookup (instance, handler_id, NULL, NULL);
2688 if (handler)
2689 {
2690 if (handler->block_count)
2691 handler->block_count -= 1;
2692 else
2693 g_warning (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2694 }
2695 else
2696 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2697 SIGNAL_UNLOCK ();
2698 }
2699
2700 /**
2701 * g_signal_handler_disconnect:
2702 * @instance: (type GObject.Object): The instance to remove the signal handler from.
2703 * @handler_id: Handler id of the handler to be disconnected.
2704 *
2705 * Disconnects a handler from an instance so it will not be called during
2706 * any future or currently ongoing emissions of the signal it has been
2707 * connected to. The @handler_id becomes invalid and may be reused.
2708 *
2709 * The @handler_id has to be a valid signal handler id, connected to a
2710 * signal of @instance.
2711 */
2712 void
g_signal_handler_disconnect(gpointer instance,gulong handler_id)2713 g_signal_handler_disconnect (gpointer instance,
2714 gulong handler_id)
2715 {
2716 Handler *handler;
2717
2718 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2719 g_return_if_fail (handler_id > 0);
2720
2721 SIGNAL_LOCK ();
2722 handler = handler_lookup (instance, handler_id, 0, 0);
2723 if (handler)
2724 {
2725 g_hash_table_remove (g_handlers, handler);
2726 handler->sequential_number = 0;
2727 handler->block_count = 1;
2728 remove_invalid_closure_notify (handler, instance);
2729 handler_unref_R (handler->signal_id, instance, handler);
2730 }
2731 else
2732 g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2733 SIGNAL_UNLOCK ();
2734 }
2735
2736 /**
2737 * g_signal_handler_is_connected:
2738 * @instance: (type GObject.Object): The instance where a signal handler is sought.
2739 * @handler_id: the handler ID.
2740 *
2741 * Returns whether @handler_id is the ID of a handler connected to @instance.
2742 *
2743 * Returns: whether @handler_id identifies a handler connected to @instance.
2744 */
2745 gboolean
g_signal_handler_is_connected(gpointer instance,gulong handler_id)2746 g_signal_handler_is_connected (gpointer instance,
2747 gulong handler_id)
2748 {
2749 Handler *handler;
2750 gboolean connected;
2751
2752 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2753
2754 SIGNAL_LOCK ();
2755 handler = handler_lookup (instance, handler_id, NULL, NULL);
2756 connected = handler != NULL;
2757 SIGNAL_UNLOCK ();
2758
2759 return connected;
2760 }
2761
2762 /**
2763 * g_signal_handlers_destroy:
2764 * @instance: (type GObject.Object): The instance whose signal handlers are destroyed
2765 *
2766 * Destroy all signal handlers of a type instance. This function is
2767 * an implementation detail of the #GObject dispose implementation,
2768 * and should not be used outside of the type system.
2769 */
2770 void
g_signal_handlers_destroy(gpointer instance)2771 g_signal_handlers_destroy (gpointer instance)
2772 {
2773 GBSearchArray *hlbsa;
2774
2775 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2776
2777 SIGNAL_LOCK ();
2778 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2779 if (hlbsa)
2780 {
2781 guint i;
2782
2783 /* reentrancy caution, delete instance trace first */
2784 g_hash_table_remove (g_handler_list_bsa_ht, instance);
2785
2786 for (i = 0; i < hlbsa->n_nodes; i++)
2787 {
2788 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2789 Handler *handler = hlist->handlers;
2790
2791 while (handler)
2792 {
2793 Handler *tmp = handler;
2794
2795 handler = tmp->next;
2796 tmp->block_count = 1;
2797 /* cruel unlink, this works because _all_ handlers vanish */
2798 tmp->next = NULL;
2799 tmp->prev = tmp;
2800 if (tmp->sequential_number)
2801 {
2802 g_hash_table_remove (g_handlers, tmp);
2803 remove_invalid_closure_notify (tmp, instance);
2804 tmp->sequential_number = 0;
2805 handler_unref_R (0, NULL, tmp);
2806 }
2807 }
2808 }
2809 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2810 }
2811 SIGNAL_UNLOCK ();
2812 }
2813
2814 /**
2815 * g_signal_handler_find:
2816 * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2817 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2818 * and/or @data the handler has to match.
2819 * @signal_id: Signal the handler has to be connected to.
2820 * @detail: Signal detail the handler has to be connected to.
2821 * @closure: (nullable): The closure the handler will invoke.
2822 * @func: The C closure callback of the handler (useless for non-C closures).
2823 * @data: (nullable) (closure closure): The closure data of the handler's closure.
2824 *
2825 * Finds the first signal handler that matches certain selection criteria.
2826 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2827 * flags, and the criteria values are passed as arguments.
2828 * The match @mask has to be non-0 for successful matches.
2829 * If no handler was found, 0 is returned.
2830 *
2831 * Returns: A valid non-0 signal handler id for a successful match.
2832 */
2833 gulong
g_signal_handler_find(gpointer instance,GSignalMatchType mask,guint signal_id,GQuark detail,GClosure * closure,gpointer func,gpointer data)2834 g_signal_handler_find (gpointer instance,
2835 GSignalMatchType mask,
2836 guint signal_id,
2837 GQuark detail,
2838 GClosure *closure,
2839 gpointer func,
2840 gpointer data)
2841 {
2842 gulong handler_seq_no = 0;
2843
2844 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2845 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2846
2847 if (mask & G_SIGNAL_MATCH_MASK)
2848 {
2849 HandlerMatch *mlist;
2850
2851 SIGNAL_LOCK ();
2852 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2853 if (mlist)
2854 {
2855 handler_seq_no = mlist->handler->sequential_number;
2856 handler_match_free1_R (mlist, instance);
2857 }
2858 SIGNAL_UNLOCK ();
2859 }
2860
2861 return handler_seq_no;
2862 }
2863
2864 static guint
signal_handlers_foreach_matched_R(gpointer instance,GSignalMatchType mask,guint signal_id,GQuark detail,GClosure * closure,gpointer func,gpointer data,void (* callback)(gpointer instance,gulong handler_seq_no))2865 signal_handlers_foreach_matched_R (gpointer instance,
2866 GSignalMatchType mask,
2867 guint signal_id,
2868 GQuark detail,
2869 GClosure *closure,
2870 gpointer func,
2871 gpointer data,
2872 void (*callback) (gpointer instance,
2873 gulong handler_seq_no))
2874 {
2875 HandlerMatch *mlist;
2876 guint n_handlers = 0;
2877
2878 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2879 while (mlist)
2880 {
2881 n_handlers++;
2882 if (mlist->handler->sequential_number)
2883 {
2884 SIGNAL_UNLOCK ();
2885 callback (instance, mlist->handler->sequential_number);
2886 SIGNAL_LOCK ();
2887 }
2888 mlist = handler_match_free1_R (mlist, instance);
2889 }
2890
2891 return n_handlers;
2892 }
2893
2894 /**
2895 * g_signal_handlers_block_matched:
2896 * @instance: (type GObject.Object): The instance to block handlers from.
2897 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2898 * and/or @data the handlers have to match.
2899 * @signal_id: Signal the handlers have to be connected to.
2900 * @detail: Signal detail the handlers have to be connected to.
2901 * @closure: (nullable): The closure the handlers will invoke.
2902 * @func: The C closure callback of the handlers (useless for non-C closures).
2903 * @data: (nullable) (closure closure): The closure data of the handlers' closures.
2904 *
2905 * Blocks all handlers on an instance that match a certain selection criteria.
2906 * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2907 * flags, and the criteria values are passed as arguments.
2908 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2909 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2910 * If no handlers were found, 0 is returned, the number of blocked handlers
2911 * otherwise.
2912 *
2913 * Returns: The number of handlers that matched.
2914 */
2915 guint
g_signal_handlers_block_matched(gpointer instance,GSignalMatchType mask,guint signal_id,GQuark detail,GClosure * closure,gpointer func,gpointer data)2916 g_signal_handlers_block_matched (gpointer instance,
2917 GSignalMatchType mask,
2918 guint signal_id,
2919 GQuark detail,
2920 GClosure *closure,
2921 gpointer func,
2922 gpointer data)
2923 {
2924 guint n_handlers = 0;
2925
2926 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2927 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2928
2929 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2930 {
2931 SIGNAL_LOCK ();
2932 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2933 closure, func, data,
2934 g_signal_handler_block);
2935 SIGNAL_UNLOCK ();
2936 }
2937
2938 return n_handlers;
2939 }
2940
2941 /**
2942 * g_signal_handlers_unblock_matched:
2943 * @instance: (type GObject.Object): The instance to unblock handlers from.
2944 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2945 * and/or @data the handlers have to match.
2946 * @signal_id: Signal the handlers have to be connected to.
2947 * @detail: Signal detail the handlers have to be connected to.
2948 * @closure: (nullable): The closure the handlers will invoke.
2949 * @func: The C closure callback of the handlers (useless for non-C closures).
2950 * @data: (nullable) (closure closure): The closure data of the handlers' closures.
2951 *
2952 * Unblocks all handlers on an instance that match a certain selection
2953 * criteria. The criteria mask is passed as an OR-ed combination of
2954 * #GSignalMatchType flags, and the criteria values are passed as arguments.
2955 * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2956 * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2957 * If no handlers were found, 0 is returned, the number of unblocked handlers
2958 * otherwise. The match criteria should not apply to any handlers that are
2959 * not currently blocked.
2960 *
2961 * Returns: The number of handlers that matched.
2962 */
2963 guint
g_signal_handlers_unblock_matched(gpointer instance,GSignalMatchType mask,guint signal_id,GQuark detail,GClosure * closure,gpointer func,gpointer data)2964 g_signal_handlers_unblock_matched (gpointer instance,
2965 GSignalMatchType mask,
2966 guint signal_id,
2967 GQuark detail,
2968 GClosure *closure,
2969 gpointer func,
2970 gpointer data)
2971 {
2972 guint n_handlers = 0;
2973
2974 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2975 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2976
2977 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2978 {
2979 SIGNAL_LOCK ();
2980 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2981 closure, func, data,
2982 g_signal_handler_unblock);
2983 SIGNAL_UNLOCK ();
2984 }
2985
2986 return n_handlers;
2987 }
2988
2989 /**
2990 * g_signal_handlers_disconnect_matched:
2991 * @instance: (type GObject.Object): The instance to remove handlers from.
2992 * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2993 * and/or @data the handlers have to match.
2994 * @signal_id: Signal the handlers have to be connected to.
2995 * @detail: Signal detail the handlers have to be connected to.
2996 * @closure: (nullable): The closure the handlers will invoke.
2997 * @func: The C closure callback of the handlers (useless for non-C closures).
2998 * @data: (nullable) (closure closure): The closure data of the handlers' closures.
2999 *
3000 * Disconnects all handlers on an instance that match a certain
3001 * selection criteria. The criteria mask is passed as an OR-ed
3002 * combination of #GSignalMatchType flags, and the criteria values are
3003 * passed as arguments. Passing at least one of the
3004 * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
3005 * %G_SIGNAL_MATCH_DATA match flags is required for successful
3006 * matches. If no handlers were found, 0 is returned, the number of
3007 * disconnected handlers otherwise.
3008 *
3009 * Returns: The number of handlers that matched.
3010 */
3011 guint
g_signal_handlers_disconnect_matched(gpointer instance,GSignalMatchType mask,guint signal_id,GQuark detail,GClosure * closure,gpointer func,gpointer data)3012 g_signal_handlers_disconnect_matched (gpointer instance,
3013 GSignalMatchType mask,
3014 guint signal_id,
3015 GQuark detail,
3016 GClosure *closure,
3017 gpointer func,
3018 gpointer data)
3019 {
3020 guint n_handlers = 0;
3021
3022 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
3023 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
3024
3025 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
3026 {
3027 SIGNAL_LOCK ();
3028 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
3029 closure, func, data,
3030 g_signal_handler_disconnect);
3031 SIGNAL_UNLOCK ();
3032 }
3033
3034 return n_handlers;
3035 }
3036
3037 /**
3038 * g_signal_has_handler_pending:
3039 * @instance: (type GObject.Object): the object whose signal handlers are sought.
3040 * @signal_id: the signal id.
3041 * @detail: the detail.
3042 * @may_be_blocked: whether blocked handlers should count as match.
3043 *
3044 * Returns whether there are any handlers connected to @instance for the
3045 * given signal id and detail.
3046 *
3047 * If @detail is 0 then it will only match handlers that were connected
3048 * without detail. If @detail is non-zero then it will match handlers
3049 * connected both without detail and with the given detail. This is
3050 * consistent with how a signal emitted with @detail would be delivered
3051 * to those handlers.
3052 *
3053 * Since 2.46 this also checks for a non-default class closure being
3054 * installed, as this is basically always what you want.
3055 *
3056 * One example of when you might use this is when the arguments to the
3057 * signal are difficult to compute. A class implementor may opt to not
3058 * emit the signal if no one is attached anyway, thus saving the cost
3059 * of building the arguments.
3060 *
3061 * Returns: %TRUE if a handler is connected to the signal, %FALSE
3062 * otherwise.
3063 */
3064 gboolean
g_signal_has_handler_pending(gpointer instance,guint signal_id,GQuark detail,gboolean may_be_blocked)3065 g_signal_has_handler_pending (gpointer instance,
3066 guint signal_id,
3067 GQuark detail,
3068 gboolean may_be_blocked)
3069 {
3070 HandlerMatch *mlist;
3071 gboolean has_pending;
3072 SignalNode *node;
3073
3074 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
3075 g_return_val_if_fail (signal_id > 0, FALSE);
3076
3077 SIGNAL_LOCK ();
3078
3079 node = LOOKUP_SIGNAL_NODE (signal_id);
3080 if (detail)
3081 {
3082 if (!(node->flags & G_SIGNAL_DETAILED))
3083 {
3084 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3085 SIGNAL_UNLOCK ();
3086 return FALSE;
3087 }
3088 }
3089 mlist = handlers_find (instance,
3090 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
3091 signal_id, detail, NULL, NULL, NULL, TRUE);
3092 if (mlist)
3093 {
3094 has_pending = TRUE;
3095 handler_match_free1_R (mlist, instance);
3096 }
3097 else
3098 {
3099 ClassClosure *class_closure = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
3100 if (class_closure != NULL && class_closure->instance_type != 0)
3101 has_pending = TRUE;
3102 else
3103 has_pending = FALSE;
3104 }
3105 SIGNAL_UNLOCK ();
3106
3107 return has_pending;
3108 }
3109
3110 /**
3111 * g_signal_emitv:
3112 * @instance_and_params: (array): argument list for the signal emission.
3113 * The first element in the array is a #GValue for the instance the signal
3114 * is being emitted on. The rest are any arguments to be passed to the signal.
3115 * @signal_id: the signal id
3116 * @detail: the detail
3117 * @return_value: (inout) (optional): Location to
3118 * store the return value of the signal emission. This must be provided if the
3119 * specified signal returns a value, but may be ignored otherwise.
3120 *
3121 * Emits a signal.
3122 *
3123 * Note that g_signal_emitv() doesn't change @return_value if no handlers are
3124 * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
3125 */
3126 void
g_signal_emitv(const GValue * instance_and_params,guint signal_id,GQuark detail,GValue * return_value)3127 g_signal_emitv (const GValue *instance_and_params,
3128 guint signal_id,
3129 GQuark detail,
3130 GValue *return_value)
3131 {
3132 gpointer instance;
3133 SignalNode *node;
3134 #ifdef G_ENABLE_DEBUG
3135 const GValue *param_values;
3136 guint i;
3137 #endif
3138
3139 g_return_if_fail (instance_and_params != NULL);
3140 instance = g_value_peek_pointer (instance_and_params);
3141 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3142 g_return_if_fail (signal_id > 0);
3143
3144 #ifdef G_ENABLE_DEBUG
3145 param_values = instance_and_params + 1;
3146 #endif
3147
3148 SIGNAL_LOCK ();
3149 node = LOOKUP_SIGNAL_NODE (signal_id);
3150 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3151 {
3152 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3153 SIGNAL_UNLOCK ();
3154 return;
3155 }
3156 #ifdef G_ENABLE_DEBUG
3157 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3158 {
3159 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3160 SIGNAL_UNLOCK ();
3161 return;
3162 }
3163 for (i = 0; i < node->n_params; i++)
3164 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3165 {
3166 g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
3167 G_STRLOC,
3168 type_debug_name (node->param_types[i]),
3169 i,
3170 node->name,
3171 G_VALUE_TYPE_NAME (param_values + i));
3172 SIGNAL_UNLOCK ();
3173 return;
3174 }
3175 if (node->return_type != G_TYPE_NONE)
3176 {
3177 if (!return_value)
3178 {
3179 g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3180 G_STRLOC,
3181 type_debug_name (node->return_type),
3182 node->name);
3183 SIGNAL_UNLOCK ();
3184 return;
3185 }
3186 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3187 {
3188 g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3189 G_STRLOC,
3190 type_debug_name (node->return_type),
3191 node->name,
3192 G_VALUE_TYPE_NAME (return_value));
3193 SIGNAL_UNLOCK ();
3194 return;
3195 }
3196 }
3197 else
3198 return_value = NULL;
3199 #endif /* G_ENABLE_DEBUG */
3200
3201 /* optimize NOP emissions */
3202 if (!node->single_va_closure_is_valid)
3203 node_update_single_va_closure (node);
3204
3205 if (node->single_va_closure != NULL &&
3206 (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3207 _g_closure_is_void (node->single_va_closure, instance)))
3208 {
3209 HandlerList* hlist;
3210
3211 /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */
3212 if (_g_object_has_signal_handler ((GObject *)instance))
3213 hlist = handler_list_lookup (node->signal_id, instance);
3214 else
3215 hlist = NULL;
3216
3217 if (hlist == NULL || hlist->handlers == NULL)
3218 {
3219 /* nothing to do to emit this signal */
3220 SIGNAL_UNLOCK ();
3221 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3222 return;
3223 }
3224 }
3225
3226 SIGNAL_UNLOCK ();
3227 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
3228 }
3229
3230 static inline gboolean
accumulate(GSignalInvocationHint * ihint,GValue * return_accu,GValue * handler_return,SignalAccumulator * accumulator)3231 accumulate (GSignalInvocationHint *ihint,
3232 GValue *return_accu,
3233 GValue *handler_return,
3234 SignalAccumulator *accumulator)
3235 {
3236 gboolean continue_emission;
3237
3238 if (!accumulator)
3239 return TRUE;
3240
3241 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3242 g_value_reset (handler_return);
3243
3244 ihint->run_type &= ~G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3245
3246 return continue_emission;
3247 }
3248
3249 /**
3250 * g_signal_emit_valist: (skip)
3251 * @instance: (type GObject.TypeInstance): the instance the signal is being
3252 * emitted on.
3253 * @signal_id: the signal id
3254 * @detail: the detail
3255 * @var_args: a list of parameters to be passed to the signal, followed by a
3256 * location for the return value. If the return type of the signal
3257 * is #G_TYPE_NONE, the return value location can be omitted.
3258 *
3259 * Emits a signal.
3260 *
3261 * Note that g_signal_emit_valist() resets the return value to the default
3262 * if no handlers are connected, in contrast to g_signal_emitv().
3263 */
3264 void
g_signal_emit_valist(gpointer instance,guint signal_id,GQuark detail,va_list var_args)3265 g_signal_emit_valist (gpointer instance,
3266 guint signal_id,
3267 GQuark detail,
3268 va_list var_args)
3269 {
3270 GValue *instance_and_params;
3271 GType signal_return_type;
3272 GValue *param_values;
3273 SignalNode *node;
3274 guint i, n_params;
3275
3276 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3277 g_return_if_fail (signal_id > 0);
3278
3279 SIGNAL_LOCK ();
3280 node = LOOKUP_SIGNAL_NODE (signal_id);
3281 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3282 {
3283 g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3284 SIGNAL_UNLOCK ();
3285 return;
3286 }
3287 #ifndef G_DISABLE_CHECKS
3288 if (detail && !(node->flags & G_SIGNAL_DETAILED))
3289 {
3290 g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3291 SIGNAL_UNLOCK ();
3292 return;
3293 }
3294 #endif /* !G_DISABLE_CHECKS */
3295
3296 if (!node->single_va_closure_is_valid)
3297 node_update_single_va_closure (node);
3298
3299 if (node->single_va_closure != NULL)
3300 {
3301 HandlerList* hlist;
3302 Handler *fastpath_handler = NULL;
3303 Handler *l;
3304 GClosure *closure = NULL;
3305 gboolean fastpath = TRUE;
3306 GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3307
3308 if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3309 !_g_closure_is_void (node->single_va_closure, instance))
3310 {
3311 if (_g_closure_supports_invoke_va (node->single_va_closure))
3312 {
3313 closure = node->single_va_closure;
3314 if (node->single_va_closure_is_after)
3315 run_type = G_SIGNAL_RUN_LAST;
3316 else
3317 run_type = G_SIGNAL_RUN_FIRST;
3318 }
3319 else
3320 fastpath = FALSE;
3321 }
3322
3323 /* single_va_closure is only true for GObjects, so fast path if no handler ever connected to the signal */
3324 if (_g_object_has_signal_handler ((GObject *)instance))
3325 hlist = handler_list_lookup (node->signal_id, instance);
3326 else
3327 hlist = NULL;
3328
3329 for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3330 {
3331 if (!l->block_count &&
3332 (!l->detail || l->detail == detail))
3333 {
3334 if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3335 {
3336 fastpath = FALSE;
3337 break;
3338 }
3339 else
3340 {
3341 fastpath_handler = l;
3342 closure = l->closure;
3343 if (l->after)
3344 run_type = G_SIGNAL_RUN_LAST;
3345 else
3346 run_type = G_SIGNAL_RUN_FIRST;
3347 }
3348 }
3349 }
3350
3351 if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3352 {
3353 SIGNAL_UNLOCK ();
3354 return;
3355 }
3356
3357 /* Don't allow no-recurse emission as we might have to restart, which means
3358 we will run multiple handlers and thus must ref all arguments */
3359 if (closure != NULL && (node->flags & (G_SIGNAL_NO_RECURSE)) != 0)
3360 fastpath = FALSE;
3361
3362 if (fastpath)
3363 {
3364 SignalAccumulator *accumulator;
3365 Emission emission;
3366 GValue *return_accu, accu = G_VALUE_INIT;
3367 guint signal_id;
3368 GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3369 GValue emission_return = G_VALUE_INIT;
3370 GType rtype = node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3371 gboolean static_scope = node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3372
3373 signal_id = node->signal_id;
3374 accumulator = node->accumulator;
3375 if (rtype == G_TYPE_NONE)
3376 return_accu = NULL;
3377 else if (accumulator)
3378 return_accu = &accu;
3379 else
3380 return_accu = &emission_return;
3381
3382 emission.instance = instance;
3383 emission.ihint.signal_id = signal_id;
3384 emission.ihint.detail = detail;
3385 emission.ihint.run_type = run_type | G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3386 emission.state = EMISSION_RUN;
3387 emission.chain_type = instance_type;
3388 emission_push (&emission);
3389
3390 if (fastpath_handler)
3391 handler_ref (fastpath_handler);
3392
3393 SIGNAL_UNLOCK ();
3394
3395 TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3396
3397 if (rtype != G_TYPE_NONE)
3398 g_value_init (&emission_return, rtype);
3399
3400 if (accumulator)
3401 g_value_init (&accu, rtype);
3402
3403 if (closure != NULL)
3404 {
3405 g_object_ref (instance);
3406 _g_closure_invoke_va (closure,
3407 return_accu,
3408 instance,
3409 var_args,
3410 node->n_params,
3411 node->param_types);
3412 accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3413 }
3414
3415 SIGNAL_LOCK ();
3416
3417 emission.chain_type = G_TYPE_NONE;
3418 emission_pop (&emission);
3419
3420 if (fastpath_handler)
3421 handler_unref_R (signal_id, instance, fastpath_handler);
3422
3423 SIGNAL_UNLOCK ();
3424
3425 if (accumulator)
3426 g_value_unset (&accu);
3427
3428 if (rtype != G_TYPE_NONE)
3429 {
3430 gchar *error = NULL;
3431 for (i = 0; i < node->n_params; i++)
3432 {
3433 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3434 G_VALUE_COLLECT_SKIP (ptype, var_args);
3435 }
3436
3437 G_VALUE_LCOPY (&emission_return,
3438 var_args,
3439 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3440 &error);
3441 if (!error)
3442 g_value_unset (&emission_return);
3443 else
3444 {
3445 g_warning ("%s: %s", G_STRLOC, error);
3446 g_free (error);
3447 /* we purposely leak the value here, it might not be
3448 * in a correct state if an error condition occurred
3449 */
3450 }
3451 }
3452
3453 TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3454
3455 if (closure != NULL)
3456 g_object_unref (instance);
3457
3458 return;
3459 }
3460 }
3461 SIGNAL_UNLOCK ();
3462
3463 n_params = node->n_params;
3464 signal_return_type = node->return_type;
3465 instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
3466 memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
3467 param_values = instance_and_params + 1;
3468
3469 for (i = 0; i < node->n_params; i++)
3470 {
3471 gchar *error;
3472 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3473 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3474
3475 G_VALUE_COLLECT_INIT (param_values + i, ptype,
3476 var_args,
3477 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3478 &error);
3479 if (error)
3480 {
3481 g_warning ("%s: %s", G_STRLOC, error);
3482 g_free (error);
3483
3484 /* we purposely leak the value here, it might not be
3485 * in a correct state if an error condition occurred
3486 */
3487 while (i--)
3488 g_value_unset (param_values + i);
3489
3490 return;
3491 }
3492 }
3493
3494 instance_and_params->g_type = 0;
3495 g_value_init_from_instance (instance_and_params, instance);
3496 if (signal_return_type == G_TYPE_NONE)
3497 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
3498 else
3499 {
3500 GValue return_value = G_VALUE_INIT;
3501 gchar *error = NULL;
3502 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3503 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3504
3505 g_value_init (&return_value, rtype);
3506
3507 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3508
3509 G_VALUE_LCOPY (&return_value,
3510 var_args,
3511 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3512 &error);
3513 if (!error)
3514 g_value_unset (&return_value);
3515 else
3516 {
3517 g_warning ("%s: %s", G_STRLOC, error);
3518 g_free (error);
3519
3520 /* we purposely leak the value here, it might not be
3521 * in a correct state if an error condition occurred
3522 */
3523 }
3524 }
3525 for (i = 0; i < n_params; i++)
3526 g_value_unset (param_values + i);
3527 g_value_unset (instance_and_params);
3528 }
3529
3530 /**
3531 * g_signal_emit:
3532 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3533 * @signal_id: the signal id
3534 * @detail: the detail
3535 * @...: parameters to be passed to the signal, followed by a
3536 * location for the return value. If the return type of the signal
3537 * is #G_TYPE_NONE, the return value location can be omitted.
3538 *
3539 * Emits a signal.
3540 *
3541 * Note that g_signal_emit() resets the return value to the default
3542 * if no handlers are connected, in contrast to g_signal_emitv().
3543 */
3544 void
g_signal_emit(gpointer instance,guint signal_id,GQuark detail,...)3545 g_signal_emit (gpointer instance,
3546 guint signal_id,
3547 GQuark detail,
3548 ...)
3549 {
3550 va_list var_args;
3551
3552 va_start (var_args, detail);
3553 g_signal_emit_valist (instance, signal_id, detail, var_args);
3554 va_end (var_args);
3555 }
3556
3557 /**
3558 * g_signal_emit_by_name:
3559 * @instance: (type GObject.Object): the instance the signal is being emitted on.
3560 * @detailed_signal: a string of the form "signal-name::detail".
3561 * @...: parameters to be passed to the signal, followed by a
3562 * location for the return value. If the return type of the signal
3563 * is #G_TYPE_NONE, the return value location can be omitted.
3564 *
3565 * Emits a signal.
3566 *
3567 * Note that g_signal_emit_by_name() resets the return value to the default
3568 * if no handlers are connected, in contrast to g_signal_emitv().
3569 */
3570 void
g_signal_emit_by_name(gpointer instance,const gchar * detailed_signal,...)3571 g_signal_emit_by_name (gpointer instance,
3572 const gchar *detailed_signal,
3573 ...)
3574 {
3575 GQuark detail = 0;
3576 guint signal_id;
3577 GType itype;
3578
3579 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3580 g_return_if_fail (detailed_signal != NULL);
3581
3582 itype = G_TYPE_FROM_INSTANCE (instance);
3583
3584 SIGNAL_LOCK ();
3585 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3586 SIGNAL_UNLOCK ();
3587
3588 if (signal_id)
3589 {
3590 va_list var_args;
3591
3592 va_start (var_args, detailed_signal);
3593 g_signal_emit_valist (instance, signal_id, detail, var_args);
3594 va_end (var_args);
3595 }
3596 else
3597 g_warning ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3598 G_STRLOC, detailed_signal, instance, g_type_name (itype));
3599 }
3600
3601 static gboolean
signal_emit_unlocked_R(SignalNode * node,GQuark detail,gpointer instance,GValue * emission_return,const GValue * instance_and_params)3602 signal_emit_unlocked_R (SignalNode *node,
3603 GQuark detail,
3604 gpointer instance,
3605 GValue *emission_return,
3606 const GValue *instance_and_params)
3607 {
3608 SignalAccumulator *accumulator;
3609 Emission emission;
3610 GClosure *class_closure;
3611 HandlerList *hlist;
3612 Handler *handler_list = NULL;
3613 GValue *return_accu, accu = G_VALUE_INIT;
3614 guint signal_id;
3615 gulong max_sequential_handler_number;
3616 gboolean return_value_altered = FALSE;
3617
3618 TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3619
3620 SIGNAL_LOCK ();
3621 signal_id = node->signal_id;
3622
3623 if (node->flags & G_SIGNAL_NO_RECURSE)
3624 {
3625 Emission *node = emission_find (signal_id, detail, instance);
3626
3627 if (node)
3628 {
3629 node->state = EMISSION_RESTART;
3630 SIGNAL_UNLOCK ();
3631 return return_value_altered;
3632 }
3633 }
3634 accumulator = node->accumulator;
3635 if (accumulator)
3636 {
3637 SIGNAL_UNLOCK ();
3638 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3639 return_accu = &accu;
3640 SIGNAL_LOCK ();
3641 }
3642 else
3643 return_accu = emission_return;
3644 emission.instance = instance;
3645 emission.ihint.signal_id = node->signal_id;
3646 emission.ihint.detail = detail;
3647 emission.ihint.run_type = 0;
3648 emission.state = 0;
3649 emission.chain_type = G_TYPE_NONE;
3650 emission_push (&emission);
3651 class_closure = signal_lookup_closure (node, instance);
3652
3653 EMIT_RESTART:
3654
3655 if (handler_list)
3656 handler_unref_R (signal_id, instance, handler_list);
3657 max_sequential_handler_number = g_handler_sequential_number;
3658 hlist = handler_list_lookup (signal_id, instance);
3659 handler_list = hlist ? hlist->handlers : NULL;
3660 if (handler_list)
3661 handler_ref (handler_list);
3662
3663 emission.ihint.run_type = G_SIGNAL_RUN_FIRST | G_SIGNAL_ACCUMULATOR_FIRST_RUN;
3664
3665 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3666 {
3667 emission.state = EMISSION_RUN;
3668
3669 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3670 SIGNAL_UNLOCK ();
3671 g_closure_invoke (class_closure,
3672 return_accu,
3673 node->n_params + 1,
3674 instance_and_params,
3675 &emission.ihint);
3676 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3677 emission.state == EMISSION_RUN)
3678 emission.state = EMISSION_STOP;
3679 SIGNAL_LOCK ();
3680 emission.chain_type = G_TYPE_NONE;
3681 return_value_altered = TRUE;
3682
3683 if (emission.state == EMISSION_STOP)
3684 goto EMIT_CLEANUP;
3685 else if (emission.state == EMISSION_RESTART)
3686 goto EMIT_RESTART;
3687 }
3688
3689 if (node->emission_hooks)
3690 {
3691 gboolean need_destroy, was_in_call, may_recurse = TRUE;
3692 GHook *hook;
3693
3694 emission.state = EMISSION_HOOK;
3695 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3696 while (hook)
3697 {
3698 SignalHook *signal_hook = SIGNAL_HOOK (hook);
3699
3700 if (!signal_hook->detail || signal_hook->detail == detail)
3701 {
3702 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3703
3704 was_in_call = G_HOOK_IN_CALL (hook);
3705 hook->flags |= G_HOOK_FLAG_IN_CALL;
3706 SIGNAL_UNLOCK ();
3707 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3708 SIGNAL_LOCK ();
3709 if (!was_in_call)
3710 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3711 if (need_destroy)
3712 g_hook_destroy_link (node->emission_hooks, hook);
3713 }
3714 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3715 }
3716
3717 if (emission.state == EMISSION_RESTART)
3718 goto EMIT_RESTART;
3719 }
3720
3721 if (handler_list)
3722 {
3723 Handler *handler = handler_list;
3724
3725 emission.state = EMISSION_RUN;
3726 handler_ref (handler);
3727 do
3728 {
3729 Handler *tmp;
3730
3731 if (handler->after)
3732 {
3733 handler_unref_R (signal_id, instance, handler_list);
3734 handler_list = handler;
3735 break;
3736 }
3737 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3738 handler->sequential_number < max_sequential_handler_number)
3739 {
3740 SIGNAL_UNLOCK ();
3741 g_closure_invoke (handler->closure,
3742 return_accu,
3743 node->n_params + 1,
3744 instance_and_params,
3745 &emission.ihint);
3746 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3747 emission.state == EMISSION_RUN)
3748 emission.state = EMISSION_STOP;
3749 SIGNAL_LOCK ();
3750 return_value_altered = TRUE;
3751
3752 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3753 }
3754 else
3755 tmp = handler->next;
3756
3757 if (tmp)
3758 handler_ref (tmp);
3759 handler_unref_R (signal_id, instance, handler_list);
3760 handler_list = handler;
3761 handler = tmp;
3762 }
3763 while (handler);
3764
3765 if (emission.state == EMISSION_STOP)
3766 goto EMIT_CLEANUP;
3767 else if (emission.state == EMISSION_RESTART)
3768 goto EMIT_RESTART;
3769 }
3770
3771 emission.ihint.run_type &= ~G_SIGNAL_RUN_FIRST;
3772 emission.ihint.run_type |= G_SIGNAL_RUN_LAST;
3773
3774 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3775 {
3776 emission.state = EMISSION_RUN;
3777
3778 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3779 SIGNAL_UNLOCK ();
3780 g_closure_invoke (class_closure,
3781 return_accu,
3782 node->n_params + 1,
3783 instance_and_params,
3784 &emission.ihint);
3785 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3786 emission.state == EMISSION_RUN)
3787 emission.state = EMISSION_STOP;
3788 SIGNAL_LOCK ();
3789 emission.chain_type = G_TYPE_NONE;
3790 return_value_altered = TRUE;
3791
3792 if (emission.state == EMISSION_STOP)
3793 goto EMIT_CLEANUP;
3794 else if (emission.state == EMISSION_RESTART)
3795 goto EMIT_RESTART;
3796 }
3797
3798 if (handler_list)
3799 {
3800 Handler *handler = handler_list;
3801
3802 emission.state = EMISSION_RUN;
3803 handler_ref (handler);
3804 do
3805 {
3806 Handler *tmp;
3807
3808 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3809 handler->sequential_number < max_sequential_handler_number)
3810 {
3811 SIGNAL_UNLOCK ();
3812 g_closure_invoke (handler->closure,
3813 return_accu,
3814 node->n_params + 1,
3815 instance_and_params,
3816 &emission.ihint);
3817 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3818 emission.state == EMISSION_RUN)
3819 emission.state = EMISSION_STOP;
3820 SIGNAL_LOCK ();
3821 return_value_altered = TRUE;
3822
3823 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3824 }
3825 else
3826 tmp = handler->next;
3827
3828 if (tmp)
3829 handler_ref (tmp);
3830 handler_unref_R (signal_id, instance, handler);
3831 handler = tmp;
3832 }
3833 while (handler);
3834
3835 if (emission.state == EMISSION_STOP)
3836 goto EMIT_CLEANUP;
3837 else if (emission.state == EMISSION_RESTART)
3838 goto EMIT_RESTART;
3839 }
3840
3841 EMIT_CLEANUP:
3842
3843 emission.ihint.run_type &= ~G_SIGNAL_RUN_LAST;
3844 emission.ihint.run_type |= G_SIGNAL_RUN_CLEANUP;
3845
3846 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3847 {
3848 gboolean need_unset = FALSE;
3849
3850 emission.state = EMISSION_STOP;
3851
3852 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3853 SIGNAL_UNLOCK ();
3854 if (node->return_type != G_TYPE_NONE && !accumulator)
3855 {
3856 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3857 need_unset = TRUE;
3858 }
3859 g_closure_invoke (class_closure,
3860 node->return_type != G_TYPE_NONE ? &accu : NULL,
3861 node->n_params + 1,
3862 instance_and_params,
3863 &emission.ihint);
3864 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3865 emission.state == EMISSION_RUN)
3866 emission.state = EMISSION_STOP;
3867 if (need_unset)
3868 g_value_unset (&accu);
3869 SIGNAL_LOCK ();
3870 return_value_altered = TRUE;
3871
3872 emission.chain_type = G_TYPE_NONE;
3873
3874 if (emission.state == EMISSION_RESTART)
3875 goto EMIT_RESTART;
3876 }
3877
3878 if (handler_list)
3879 handler_unref_R (signal_id, instance, handler_list);
3880
3881 emission_pop (&emission);
3882 SIGNAL_UNLOCK ();
3883 if (accumulator)
3884 g_value_unset (&accu);
3885
3886 TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3887
3888 return return_value_altered;
3889 }
3890
3891 static void
add_invalid_closure_notify(Handler * handler,gpointer instance)3892 add_invalid_closure_notify (Handler *handler,
3893 gpointer instance)
3894 {
3895 g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3896 handler->has_invalid_closure_notify = 1;
3897 }
3898
3899 static void
remove_invalid_closure_notify(Handler * handler,gpointer instance)3900 remove_invalid_closure_notify (Handler *handler,
3901 gpointer instance)
3902 {
3903 if (handler->has_invalid_closure_notify)
3904 {
3905 g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3906 handler->has_invalid_closure_notify = 0;
3907 }
3908 }
3909
3910 static void
invalid_closure_notify(gpointer instance,GClosure * closure)3911 invalid_closure_notify (gpointer instance,
3912 GClosure *closure)
3913 {
3914 Handler *handler;
3915 guint signal_id;
3916
3917 SIGNAL_LOCK ();
3918
3919 handler = handler_lookup (instance, 0, closure, &signal_id);
3920 /* See https://bugzilla.gnome.org/show_bug.cgi?id=730296 for discussion about this... */
3921 g_assert (handler != NULL);
3922 g_assert (handler->closure == closure);
3923
3924 g_hash_table_remove (g_handlers, handler);
3925 handler->sequential_number = 0;
3926 handler->block_count = 1;
3927 handler_unref_R (signal_id, instance, handler);
3928
3929 SIGNAL_UNLOCK ();
3930 }
3931
3932 static const gchar*
type_debug_name(GType type)3933 type_debug_name (GType type)
3934 {
3935 if (type)
3936 {
3937 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3938 return name ? name : "<unknown>";
3939 }
3940 else
3941 return "<invalid>";
3942 }
3943
3944 /**
3945 * g_signal_accumulator_true_handled:
3946 * @ihint: standard #GSignalAccumulator parameter
3947 * @return_accu: standard #GSignalAccumulator parameter
3948 * @handler_return: standard #GSignalAccumulator parameter
3949 * @dummy: standard #GSignalAccumulator parameter
3950 *
3951 * A predefined #GSignalAccumulator for signals that return a
3952 * boolean values. The behavior that this accumulator gives is
3953 * that a return of %TRUE stops the signal emission: no further
3954 * callbacks will be invoked, while a return of %FALSE allows
3955 * the emission to continue. The idea here is that a %TRUE return
3956 * indicates that the callback handled the signal, and no further
3957 * handling is needed.
3958 *
3959 * Since: 2.4
3960 *
3961 * Returns: standard #GSignalAccumulator result
3962 */
3963 gboolean
g_signal_accumulator_true_handled(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)3964 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3965 GValue *return_accu,
3966 const GValue *handler_return,
3967 gpointer dummy)
3968 {
3969 gboolean continue_emission;
3970 gboolean signal_handled;
3971
3972 signal_handled = g_value_get_boolean (handler_return);
3973 g_value_set_boolean (return_accu, signal_handled);
3974 continue_emission = !signal_handled;
3975
3976 return continue_emission;
3977 }
3978
3979 /**
3980 * g_signal_accumulator_first_wins:
3981 * @ihint: standard #GSignalAccumulator parameter
3982 * @return_accu: standard #GSignalAccumulator parameter
3983 * @handler_return: standard #GSignalAccumulator parameter
3984 * @dummy: standard #GSignalAccumulator parameter
3985 *
3986 * A predefined #GSignalAccumulator for signals intended to be used as a
3987 * hook for application code to provide a particular value. Usually
3988 * only one such value is desired and multiple handlers for the same
3989 * signal don't make much sense (except for the case of the default
3990 * handler defined in the class structure, in which case you will
3991 * usually want the signal connection to override the class handler).
3992 *
3993 * This accumulator will use the return value from the first signal
3994 * handler that is run as the return value for the signal and not run
3995 * any further handlers (ie: the first handler "wins").
3996 *
3997 * Returns: standard #GSignalAccumulator result
3998 *
3999 * Since: 2.28
4000 **/
4001 gboolean
g_signal_accumulator_first_wins(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer dummy)4002 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
4003 GValue *return_accu,
4004 const GValue *handler_return,
4005 gpointer dummy)
4006 {
4007 g_value_copy (handler_return, return_accu);
4008 return FALSE;
4009 }
4010
4011 /**
4012 * g_clear_signal_handler:
4013 * @handler_id_ptr: A pointer to a handler ID (of type #gulong) of the handler to be disconnected.
4014 * @instance: (type GObject.Object): The instance to remove the signal handler from.
4015 * This pointer may be %NULL or invalid, if the handler ID is zero.
4016 *
4017 * Disconnects a handler from @instance so it will not be called during
4018 * any future or currently ongoing emissions of the signal it has been
4019 * connected to. The @handler_id_ptr is then set to zero, which is never a valid handler ID value (see g_signal_connect()).
4020 *
4021 * If the handler ID is 0 then this function does nothing.
4022 *
4023 * There is also a macro version of this function so that the code
4024 * will be inlined.
4025 *
4026 * Since: 2.62
4027 */
4028 void
4029 (g_clear_signal_handler) (gulong *handler_id_ptr,
4030 gpointer instance)
4031 {
4032 g_return_if_fail (handler_id_ptr != NULL);
4033
4034 #ifndef g_clear_signal_handler
4035 #error g_clear_signal_handler() macro is not defined
4036 #endif
4037
4038 g_clear_signal_handler (handler_id_ptr, instance);
4039 }
4040