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