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