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