Eradicate links and xrefs
[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 emission
47  * of a signal. Signals are introduced per-type and are identified
48  * through strings. Signals introduced for a parent type are available
49  * in derived types as well, so basically they are a per-type facility
50  * that is inherited.
51  *
52  * A signal emission mainly involves invocation of a certain set of
53  * callbacks in precisely defined manner. There are two main categories
54  * of such callbacks, per-object ones and user provided ones.
55  * (Although signals can deal with any kind of instantiatable type, I'm
56  * referring to those types as "object types" in the following, simply
57  * because that is the context most users will encounter signals in.)
58  * The per-object callbacks are most often referred to as "object method
59  * handler" or "default (signal) handler", while user provided callbacks are
60  * usually just called "signal handler".
61  *
62  * The object method handler is provided at signal creation time (this most
63  * frequently happens at the end of an object class' creation), while user
64  * provided handlers are frequently connected and disconnected to/from a
65  * certain signal on certain object instances.
66  *
67  * A signal emission consists of five stages, unless prematurely stopped:
68  *
69  * 1. Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals
70  *
71  * 2. Invocation of normal user-provided signal handlers (where the @after
72  *    flag is not set)
73  *
74  * 3. Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals
75  *
76  * 4. Invocation of user provided signal handlers (where the @after flag is set)
77  *
78  * 5. Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals
79  
80  * The user-provided signal handlers are called in the order they were
81  * connected in.
82  *
83  * All handlers may prematurely stop a signal emission, and any number of
84  * handlers may be connected, disconnected, blocked or unblocked during
85  * a signal emission.
86  *
87  * There are certain criteria for skipping user handlers in stages 2 and 4
88  * of a signal emission.
89  *
90  * First, user handlers may be blocked. Blocked handlers are omitted during
91  * callback invocation, to return from the blocked state, a handler has to
92  * get unblocked exactly the same amount of times it has been blocked before.
93  *
94  * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional
95  * @detail argument passed in to g_signal_emit() has to match the detail
96  * argument of the signal handler currently subject to invocation.
97  * Specification of no detail argument for signal handlers (omission of the
98  * detail part of the signal specification upon connection) serves as a
99  * wildcard and matches any detail argument passed in to emission.
100  */
101
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 class_init method by doing super_class->signal_handler = my_signal_handler.
1369  * Instead they will have to use g_signal_override_class_handler().
1370  *
1371  * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1372  * the marshaller for this signal.
1373  *
1374  * Returns: the signal id
1375  */
1376 guint
1377 g_signal_new (const gchar        *signal_name,
1378               GType               itype,
1379               GSignalFlags        signal_flags,
1380               guint               class_offset,
1381               GSignalAccumulator  accumulator,
1382               gpointer            accu_data,
1383               GSignalCMarshaller  c_marshaller,
1384               GType               return_type,
1385               guint               n_params,
1386               ...)
1387 {
1388   va_list args;
1389   guint signal_id;
1390
1391   g_return_val_if_fail (signal_name != NULL, 0);
1392   
1393   va_start (args, n_params);
1394
1395   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1396                                    class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1397                                    accumulator, accu_data, c_marshaller,
1398                                    return_type, n_params, args);
1399
1400   va_end (args);
1401
1402   return signal_id;
1403 }
1404
1405 /**
1406  * g_signal_new_class_handler:
1407  * @signal_name: the name for the signal
1408  * @itype: the type this signal pertains to. It will also pertain to
1409  *  types which are derived from this type.
1410  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1411  *  the default handler is to be invoked. You should at least specify
1412  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1413  * @class_handler: a #GCallback which acts as class implementation of
1414  *  this signal. Used to invoke a class method generically. Pass %NULL to
1415  *  not associate a class method with this signal.
1416  * @accumulator: the accumulator for this signal; may be %NULL.
1417  * @accu_data: user data for the @accumulator.
1418  * @c_marshaller: (allow-none): the function to translate arrays of parameter
1419  *  values to signal emissions into C language callback invocations or %NULL.
1420  * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1421  *  without a return value.
1422  * @n_params: the number of parameter types to follow.
1423  * @...: a list of types, one for each parameter.
1424  *
1425  * Creates a new signal. (This is usually done in the class initializer.)
1426  *
1427  * This is a variant of g_signal_new() that takes a C callback instead
1428  * off a class offset for the signal's class handler. This function
1429  * doesn't need a function pointer exposed in the class structure of
1430  * an object definition, instead the function pointer is passed
1431  * directly and can be overriden by derived classes with
1432  * g_signal_override_class_closure() or
1433  * g_signal_override_class_handler()and chained to with
1434  * g_signal_chain_from_overridden() or
1435  * g_signal_chain_from_overridden_handler().
1436  *
1437  * See g_signal_new() for information about signal names.
1438  *
1439  * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1440  * the marshaller for this signal.
1441  *
1442  * Returns: the signal id
1443  *
1444  * Since: 2.18
1445  */
1446 guint
1447 g_signal_new_class_handler (const gchar        *signal_name,
1448                             GType               itype,
1449                             GSignalFlags        signal_flags,
1450                             GCallback           class_handler,
1451                             GSignalAccumulator  accumulator,
1452                             gpointer            accu_data,
1453                             GSignalCMarshaller  c_marshaller,
1454                             GType               return_type,
1455                             guint               n_params,
1456                             ...)
1457 {
1458   va_list args;
1459   guint signal_id;
1460
1461   g_return_val_if_fail (signal_name != NULL, 0);
1462
1463   va_start (args, n_params);
1464
1465   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1466                                    class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1467                                    accumulator, accu_data, c_marshaller,
1468                                    return_type, n_params, args);
1469
1470   va_end (args);
1471
1472   return signal_id;
1473 }
1474
1475 static inline ClassClosure*
1476 signal_find_class_closure (SignalNode *node,
1477                            GType       itype)
1478 {
1479   GBSearchArray *bsa = node->class_closure_bsa;
1480   ClassClosure *cc;
1481
1482   if (bsa)
1483     {
1484       ClassClosure key;
1485
1486       /* cc->instance_type is 0 for default closure */
1487       
1488       key.instance_type = itype;
1489       cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1490       while (!cc && key.instance_type)
1491         {
1492           key.instance_type = g_type_parent (key.instance_type);
1493           cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1494         }
1495     }
1496   else
1497     cc = NULL;
1498   return cc;
1499 }
1500
1501 static inline GClosure*
1502 signal_lookup_closure (SignalNode    *node,
1503                        GTypeInstance *instance)
1504 {
1505   ClassClosure *cc;
1506
1507   if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1508     {
1509       cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1510       if (cc && cc->instance_type == 0) /* check for default closure */
1511         return cc->closure;
1512     }
1513   cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1514   return cc ? cc->closure : NULL;
1515 }
1516
1517 static void
1518 signal_add_class_closure (SignalNode *node,
1519                           GType       itype,
1520                           GClosure   *closure)
1521 {
1522   ClassClosure key;
1523
1524   node->single_va_closure_is_valid = FALSE;
1525
1526   if (!node->class_closure_bsa)
1527     node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1528   key.instance_type = itype;
1529   key.closure = g_closure_ref (closure);
1530   node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1531                                                     &g_class_closure_bconfig,
1532                                                     &key);
1533   g_closure_sink (closure);
1534   if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1535     {
1536       g_closure_set_marshal (closure, node->c_marshaller);
1537       if (node->va_marshaller)
1538         _g_closure_set_va_marshal (closure, node->va_marshaller);
1539     }
1540 }
1541
1542 /**
1543  * g_signal_newv:
1544  * @signal_name: the name for the signal
1545  * @itype: the type this signal pertains to. It will also pertain to
1546  *     types which are derived from this type
1547  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1548  *     the default handler is to be invoked. You should at least specify
1549  *     %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST
1550  * @class_closure: (allow-none): The closure to invoke on signal emission;
1551  *     may be %NULL
1552  * @accumulator: (allow-none): the accumulator for this signal; may be %NULL
1553  * @accu_data: user data for the @accumulator
1554  * @c_marshaller: (allow-none): the function to translate arrays of
1555  *     parameter values to signal emissions into C language callback
1556  *     invocations or %NULL
1557  * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1558  *     without a return value
1559  * @n_params: the length of @param_types
1560  * @param_types: (array length=n_params): an array of types, one for
1561  *     each parameter
1562  *
1563  * Creates a new signal. (This is usually done in the class initializer.)
1564  *
1565  * See g_signal_new() for details on allowed signal names.
1566  *
1567  * If c_marshaller is %NULL @g_cclosure_marshal_generic will be used as
1568  * the marshaller for this signal.
1569  *
1570  * Returns: the signal id
1571  */
1572 guint
1573 g_signal_newv (const gchar       *signal_name,
1574                GType              itype,
1575                GSignalFlags       signal_flags,
1576                GClosure          *class_closure,
1577                GSignalAccumulator accumulator,
1578                gpointer           accu_data,
1579                GSignalCMarshaller c_marshaller,
1580                GType              return_type,
1581                guint              n_params,
1582                GType             *param_types)
1583 {
1584   gchar *name;
1585   guint signal_id, i;
1586   SignalNode *node;
1587   GSignalCMarshaller builtin_c_marshaller;
1588   GSignalCVaMarshaller va_marshaller;
1589   
1590   g_return_val_if_fail (signal_name != NULL, 0);
1591   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1592   if (n_params)
1593     g_return_val_if_fail (param_types != NULL, 0);
1594   g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1595   if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1596     g_return_val_if_fail (accumulator == NULL, 0);
1597   if (!accumulator)
1598     g_return_val_if_fail (accu_data == NULL, 0);
1599
1600   name = g_strdup (signal_name);
1601   g_strdelimit (name, G_STR_DELIMITERS ":^", '_');  /* FIXME do character checks like for types */
1602   
1603   SIGNAL_LOCK ();
1604   
1605   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1606   node = LOOKUP_SIGNAL_NODE (signal_id);
1607   if (node && !node->destroyed)
1608     {
1609       g_warning (G_STRLOC ": signal \"%s\" already exists in the '%s' %s",
1610                  name,
1611                  type_debug_name (node->itype),
1612                  G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1613       g_free (name);
1614       SIGNAL_UNLOCK ();
1615       return 0;
1616     }
1617   if (node && node->itype != itype)
1618     {
1619       g_warning (G_STRLOC ": signal \"%s\" for type '%s' was previously created for type '%s'",
1620                  name,
1621                  type_debug_name (itype),
1622                  type_debug_name (node->itype));
1623       g_free (name);
1624       SIGNAL_UNLOCK ();
1625       return 0;
1626     }
1627   for (i = 0; i < n_params; i++)
1628     if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1629       {
1630         g_warning (G_STRLOC ": parameter %d of type '%s' for signal \"%s::%s\" is not a value type",
1631                    i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1632         g_free (name);
1633         SIGNAL_UNLOCK ();
1634         return 0;
1635       }
1636   if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1637     {
1638       g_warning (G_STRLOC ": return value of type '%s' for signal \"%s::%s\" is not a value type",
1639                  type_debug_name (return_type), type_debug_name (itype), name);
1640       g_free (name);
1641       SIGNAL_UNLOCK ();
1642       return 0;
1643     }
1644   if (return_type != G_TYPE_NONE &&
1645       (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1646     {
1647       g_warning (G_STRLOC ": signal \"%s::%s\" has return type '%s' and is only G_SIGNAL_RUN_FIRST",
1648                  type_debug_name (itype), name, type_debug_name (return_type));
1649       g_free (name);
1650       SIGNAL_UNLOCK ();
1651       return 0;
1652     }
1653   
1654   /* setup permanent portion of signal node */
1655   if (!node)
1656     {
1657       SignalKey key;
1658       
1659       signal_id = g_n_signal_nodes++;
1660       node = g_new (SignalNode, 1);
1661       node->signal_id = signal_id;
1662       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1663       g_signal_nodes[signal_id] = node;
1664       node->itype = itype;
1665       node->name = name;
1666       key.itype = itype;
1667       key.quark = g_quark_from_string (node->name);
1668       key.signal_id = signal_id;
1669       g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1670       g_strdelimit (name, "_", '-');
1671       node->name = g_intern_string (name);
1672       key.quark = g_quark_from_string (name);
1673       g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1674
1675       TRACE(GOBJECT_SIGNAL_NEW(signal_id, name, itype));
1676     }
1677   node->destroyed = FALSE;
1678
1679   /* setup reinitializable portion */
1680   node->single_va_closure_is_valid = FALSE;
1681   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1682   node->n_params = n_params;
1683   node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1684   node->return_type = return_type;
1685   node->class_closure_bsa = NULL;
1686   if (accumulator)
1687     {
1688       node->accumulator = g_new (SignalAccumulator, 1);
1689       node->accumulator->func = accumulator;
1690       node->accumulator->data = accu_data;
1691     }
1692   else
1693     node->accumulator = NULL;
1694
1695   builtin_c_marshaller = NULL;
1696   va_marshaller = NULL;
1697
1698   /* Pick up built-in va marshallers for standard types, and
1699      instead of generic marshaller if no marshaller specified */
1700   if (n_params == 0 && return_type == G_TYPE_NONE)
1701     {
1702       builtin_c_marshaller = g_cclosure_marshal_VOID__VOID;
1703       va_marshaller = g_cclosure_marshal_VOID__VOIDv;
1704     }
1705   else if (n_params == 1 && return_type == G_TYPE_NONE)
1706     {
1707 #define ADD_CHECK(__type__) \
1708       else if (g_type_is_a (param_types[0] & ~G_SIGNAL_TYPE_STATIC_SCOPE, G_TYPE_ ##__type__))         \
1709         {                                                                \
1710           builtin_c_marshaller = g_cclosure_marshal_VOID__ ## __type__;  \
1711           va_marshaller = g_cclosure_marshal_VOID__ ## __type__ ##v;     \
1712         }
1713
1714       if (0) {}
1715       ADD_CHECK (BOOLEAN)
1716       ADD_CHECK (CHAR)
1717       ADD_CHECK (UCHAR)
1718       ADD_CHECK (INT)
1719       ADD_CHECK (UINT)
1720       ADD_CHECK (LONG)
1721       ADD_CHECK (ULONG)
1722       ADD_CHECK (ENUM)
1723       ADD_CHECK (FLAGS)
1724       ADD_CHECK (FLOAT)
1725       ADD_CHECK (DOUBLE)
1726       ADD_CHECK (STRING)
1727       ADD_CHECK (PARAM)
1728       ADD_CHECK (BOXED)
1729       ADD_CHECK (POINTER)
1730       ADD_CHECK (OBJECT)
1731       ADD_CHECK (VARIANT)
1732     }
1733
1734   if (c_marshaller == NULL)
1735     {
1736       if (builtin_c_marshaller)
1737         c_marshaller = builtin_c_marshaller;
1738       else
1739         {
1740           c_marshaller = g_cclosure_marshal_generic;
1741           va_marshaller = g_cclosure_marshal_generic_va;
1742         }
1743     }
1744
1745   node->c_marshaller = c_marshaller;
1746   node->va_marshaller = va_marshaller;
1747   node->emission_hooks = NULL;
1748   if (class_closure)
1749     signal_add_class_closure (node, 0, class_closure);
1750
1751   SIGNAL_UNLOCK ();
1752
1753   g_free (name);
1754
1755   return signal_id;
1756 }
1757
1758 void
1759 g_signal_set_va_marshaller (guint              signal_id,
1760                             GType              instance_type,
1761                             GSignalCVaMarshaller va_marshaller)
1762 {
1763   SignalNode *node;
1764   
1765   g_return_if_fail (signal_id > 0);
1766   g_return_if_fail (va_marshaller != NULL);
1767   
1768   SIGNAL_LOCK ();
1769   node = LOOKUP_SIGNAL_NODE (signal_id);
1770   if (node)
1771     {
1772       node->va_marshaller = va_marshaller;
1773       if (node->class_closure_bsa)
1774         {
1775           ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1776           if (cc->closure->marshal == node->c_marshaller)
1777             _g_closure_set_va_marshal (cc->closure, va_marshaller);
1778         }
1779
1780       node->single_va_closure_is_valid = FALSE;
1781     }
1782
1783   SIGNAL_UNLOCK ();
1784 }
1785
1786
1787 /**
1788  * g_signal_new_valist:
1789  * @signal_name: the name for the signal
1790  * @itype: the type this signal pertains to. It will also pertain to
1791  *  types which are derived from this type.
1792  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1793  *  the default handler is to be invoked. You should at least specify
1794  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1795  * @class_closure: The closure to invoke on signal emission; may be %NULL.
1796  * @accumulator: the accumulator for this signal; may be %NULL.
1797  * @accu_data: user data for the @accumulator.
1798  * @c_marshaller: (allow-none): the function to translate arrays of parameter
1799  *  values to signal emissions into C language callback invocations or %NULL.
1800  * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1801  *  without a return value.
1802  * @n_params: the number of parameter types in @args.
1803  * @args: va_list of #GType, one for each parameter.
1804  *
1805  * Creates a new signal. (This is usually done in the class initializer.)
1806  *
1807  * See g_signal_new() for details on allowed signal names.
1808  *
1809  * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1810  * the marshaller for this signal.
1811  *
1812  * Returns: the signal id
1813  */
1814 guint
1815 g_signal_new_valist (const gchar       *signal_name,
1816                      GType              itype,
1817                      GSignalFlags       signal_flags,
1818                      GClosure          *class_closure,
1819                      GSignalAccumulator accumulator,
1820                      gpointer           accu_data,
1821                      GSignalCMarshaller c_marshaller,
1822                      GType              return_type,
1823                      guint              n_params,
1824                      va_list            args)
1825 {
1826   GType *param_types;
1827   guint i;
1828   guint signal_id;
1829
1830   if (n_params > 0)
1831     {
1832       param_types = g_new (GType, n_params);
1833
1834       for (i = 0; i < n_params; i++)
1835         param_types[i] = va_arg (args, GType);
1836     }
1837   else
1838     param_types = NULL;
1839
1840   signal_id = g_signal_newv (signal_name, itype, signal_flags,
1841                              class_closure, accumulator, accu_data, c_marshaller,
1842                              return_type, n_params, param_types);
1843   g_free (param_types);
1844
1845   return signal_id;
1846 }
1847
1848 static void
1849 signal_destroy_R (SignalNode *signal_node)
1850 {
1851   SignalNode node = *signal_node;
1852
1853   signal_node->destroyed = TRUE;
1854   
1855   /* reentrancy caution, zero out real contents first */
1856   signal_node->single_va_closure_is_valid = FALSE;
1857   signal_node->n_params = 0;
1858   signal_node->param_types = NULL;
1859   signal_node->return_type = 0;
1860   signal_node->class_closure_bsa = NULL;
1861   signal_node->accumulator = NULL;
1862   signal_node->c_marshaller = NULL;
1863   signal_node->va_marshaller = NULL;
1864   signal_node->emission_hooks = NULL;
1865   
1866 #ifdef  G_ENABLE_DEBUG
1867   /* check current emissions */
1868   {
1869     Emission *emission;
1870     
1871     for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1872          emission; emission = emission->next)
1873       if (emission->ihint.signal_id == node.signal_id)
1874         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')",
1875                     node.name, emission->instance);
1876   }
1877 #endif
1878   
1879   /* free contents that need to
1880    */
1881   SIGNAL_UNLOCK ();
1882   g_free (node.param_types);
1883   if (node.class_closure_bsa)
1884     {
1885       guint i;
1886
1887       for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1888         {
1889           ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1890
1891           g_closure_unref (cc->closure);
1892         }
1893       g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1894     }
1895   g_free (node.accumulator);
1896   if (node.emission_hooks)
1897     {
1898       g_hook_list_clear (node.emission_hooks);
1899       g_free (node.emission_hooks);
1900     }
1901   SIGNAL_LOCK ();
1902 }
1903
1904 /**
1905  * g_signal_override_class_closure:
1906  * @signal_id: the signal id
1907  * @instance_type: the instance type on which to override the class closure
1908  *  for the signal.
1909  * @class_closure: the closure.
1910  *
1911  * Overrides the class closure (i.e. the default handler) for the given signal
1912  * for emissions on instances of @instance_type. @instance_type must be derived
1913  * from the type to which the signal belongs.
1914  *
1915  * See g_signal_chain_from_overridden() and
1916  * g_signal_chain_from_overridden_handler() for how to chain up to the
1917  * parent class closure from inside the overridden one.
1918  */
1919 void
1920 g_signal_override_class_closure (guint     signal_id,
1921                                  GType     instance_type,
1922                                  GClosure *class_closure)
1923 {
1924   SignalNode *node;
1925   
1926   g_return_if_fail (signal_id > 0);
1927   g_return_if_fail (class_closure != NULL);
1928   
1929   SIGNAL_LOCK ();
1930   node = LOOKUP_SIGNAL_NODE (signal_id);
1931   node_check_deprecated (node);
1932   if (!g_type_is_a (instance_type, node->itype))
1933     g_warning ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1934   else
1935     {
1936       ClassClosure *cc = signal_find_class_closure (node, instance_type);
1937       
1938       if (cc && cc->instance_type == instance_type)
1939         g_warning ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1940       else
1941         signal_add_class_closure (node, instance_type, class_closure);
1942     }
1943   SIGNAL_UNLOCK ();
1944 }
1945
1946 /**
1947  * g_signal_override_class_handler:
1948  * @signal_name: the name for the signal
1949  * @instance_type: the instance type on which to override the class handler
1950  *  for the signal.
1951  * @class_handler: the handler.
1952  *
1953  * Overrides the class closure (i.e. the default handler) for the
1954  * given signal for emissions on instances of @instance_type with
1955  * callback @class_handler. @instance_type must be derived from the
1956  * type to which the signal belongs.
1957  *
1958  * See g_signal_chain_from_overridden() and
1959  * g_signal_chain_from_overridden_handler() for how to chain up to the
1960  * parent class closure from inside the overridden one.
1961  *
1962  * Since: 2.18
1963  */
1964 void
1965 g_signal_override_class_handler (const gchar *signal_name,
1966                                  GType        instance_type,
1967                                  GCallback    class_handler)
1968 {
1969   guint signal_id;
1970
1971   g_return_if_fail (signal_name != NULL);
1972   g_return_if_fail (instance_type != G_TYPE_NONE);
1973   g_return_if_fail (class_handler != NULL);
1974
1975   signal_id = g_signal_lookup (signal_name, instance_type);
1976
1977   if (signal_id)
1978     g_signal_override_class_closure (signal_id, instance_type,
1979                                      g_cclosure_new (class_handler, NULL, NULL));
1980   else
1981     g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1982                G_STRLOC, signal_name, instance_type);
1983
1984 }
1985
1986 /**
1987  * g_signal_chain_from_overridden:
1988  * @instance_and_params: (array) the argument list of the signal emission.
1989  *  The first element in the array is a #GValue for the instance the signal
1990  *  is being emitted on. The rest are any arguments to be passed to the signal.
1991  * @return_value: Location for the return value.
1992  *
1993  * Calls the original class closure of a signal. This function should only
1994  * be called from an overridden class closure; see
1995  * g_signal_override_class_closure() and
1996  * g_signal_override_class_handler().
1997  */
1998 void
1999 g_signal_chain_from_overridden (const GValue *instance_and_params,
2000                                 GValue       *return_value)
2001 {
2002   GType chain_type = 0, restore_type = 0;
2003   Emission *emission = NULL;
2004   GClosure *closure = NULL;
2005   guint n_params = 0;
2006   gpointer instance;
2007   
2008   g_return_if_fail (instance_and_params != NULL);
2009   instance = g_value_peek_pointer (instance_and_params);
2010   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2011   
2012   SIGNAL_LOCK ();
2013   emission = emission_find_innermost (instance);
2014   if (emission)
2015     {
2016       SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2017       
2018       g_assert (node != NULL);  /* paranoid */
2019       
2020       /* we should probably do the same parameter checks as g_signal_emit() here.
2021        */
2022       if (emission->chain_type != G_TYPE_NONE)
2023         {
2024           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2025           
2026           g_assert (cc != NULL);        /* closure currently in call stack */
2027
2028           n_params = node->n_params;
2029           restore_type = cc->instance_type;
2030           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2031           if (cc && cc->instance_type != restore_type)
2032             {
2033               closure = cc->closure;
2034               chain_type = cc->instance_type;
2035             }
2036         }
2037       else
2038         g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2039     }
2040   else
2041     g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2042
2043   if (closure)
2044     {
2045       emission->chain_type = chain_type;
2046       SIGNAL_UNLOCK ();
2047       g_closure_invoke (closure,
2048                         return_value,
2049                         n_params + 1,
2050                         instance_and_params,
2051                         &emission->ihint);
2052       SIGNAL_LOCK ();
2053       emission->chain_type = restore_type;
2054     }
2055   SIGNAL_UNLOCK ();
2056 }
2057
2058 /**
2059  * g_signal_chain_from_overridden_handler: (skip)
2060  * @instance: the instance the signal is being emitted on.
2061  * @...: parameters to be passed to the parent class closure, followed by a
2062  *  location for the return value. If the return type of the signal
2063  *  is #G_TYPE_NONE, the return value location can be omitted.
2064  *
2065  * Calls the original class closure of a signal. This function should
2066  * only be called from an overridden class closure; see
2067  * g_signal_override_class_closure() and
2068  * g_signal_override_class_handler().
2069  *
2070  * Since: 2.18
2071  */
2072 void
2073 g_signal_chain_from_overridden_handler (gpointer instance,
2074                                         ...)
2075 {
2076   GType chain_type = 0, restore_type = 0;
2077   Emission *emission = NULL;
2078   GClosure *closure = NULL;
2079   SignalNode *node;
2080   guint n_params = 0;
2081
2082   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2083
2084   SIGNAL_LOCK ();
2085   emission = emission_find_innermost (instance);
2086   if (emission)
2087     {
2088       node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2089
2090       g_assert (node != NULL);  /* paranoid */
2091
2092       /* we should probably do the same parameter checks as g_signal_emit() here.
2093        */
2094       if (emission->chain_type != G_TYPE_NONE)
2095         {
2096           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2097
2098           g_assert (cc != NULL);        /* closure currently in call stack */
2099
2100           n_params = node->n_params;
2101           restore_type = cc->instance_type;
2102           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2103           if (cc && cc->instance_type != restore_type)
2104             {
2105               closure = cc->closure;
2106               chain_type = cc->instance_type;
2107             }
2108         }
2109       else
2110         g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2111     }
2112   else
2113     g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2114
2115   if (closure)
2116     {
2117       GValue *instance_and_params;
2118       GType signal_return_type;
2119       GValue *param_values;
2120       va_list var_args;
2121       guint i;
2122
2123       va_start (var_args, instance);
2124
2125       signal_return_type = node->return_type;
2126       instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
2127       memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
2128       param_values = instance_and_params + 1;
2129
2130       for (i = 0; i < node->n_params; i++)
2131         {
2132           gchar *error;
2133           GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2134           gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2135
2136           SIGNAL_UNLOCK ();
2137           G_VALUE_COLLECT_INIT (param_values + i, ptype,
2138                                 var_args,
2139                                 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2140                                 &error);
2141           if (error)
2142             {
2143               g_warning ("%s: %s", G_STRLOC, error);
2144               g_free (error);
2145
2146               /* we purposely leak the value here, it might not be
2147                * in a sane state if an error condition occoured
2148                */
2149               while (i--)
2150                 g_value_unset (param_values + i);
2151
2152               va_end (var_args);
2153               return;
2154             }
2155           SIGNAL_LOCK ();
2156         }
2157
2158       SIGNAL_UNLOCK ();
2159       instance_and_params->g_type = 0;
2160       g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2161       g_value_set_instance (instance_and_params, instance);
2162       SIGNAL_LOCK ();
2163
2164       emission->chain_type = chain_type;
2165       SIGNAL_UNLOCK ();
2166
2167       if (signal_return_type == G_TYPE_NONE)
2168         {
2169           g_closure_invoke (closure,
2170                             NULL,
2171                             n_params + 1,
2172                             instance_and_params,
2173                             &emission->ihint);
2174         }
2175       else
2176         {
2177           GValue return_value = G_VALUE_INIT;
2178           gchar *error = NULL;
2179           GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2180           gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2181
2182           g_value_init (&return_value, rtype);
2183
2184           g_closure_invoke (closure,
2185                             &return_value,
2186                             n_params + 1,
2187                             instance_and_params,
2188                             &emission->ihint);
2189
2190           G_VALUE_LCOPY (&return_value,
2191                          var_args,
2192                          static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2193                          &error);
2194           if (!error)
2195             {
2196               g_value_unset (&return_value);
2197             }
2198           else
2199             {
2200               g_warning ("%s: %s", G_STRLOC, error);
2201               g_free (error);
2202
2203               /* we purposely leak the value here, it might not be
2204                * in a sane state if an error condition occurred
2205                */
2206             }
2207         }
2208
2209       for (i = 0; i < n_params; i++)
2210         g_value_unset (param_values + i);
2211       g_value_unset (instance_and_params);
2212
2213       va_end (var_args);
2214
2215       SIGNAL_LOCK ();
2216       emission->chain_type = restore_type;
2217     }
2218   SIGNAL_UNLOCK ();
2219 }
2220
2221 /**
2222  * g_signal_get_invocation_hint:
2223  * @instance: (type GObject.Object): the instance to query
2224  *
2225  * Returns the invocation hint of the innermost signal emission of instance.
2226  *
2227  * Returns: (transfer none): the invocation hint of the innermost signal  emission.
2228  */
2229 GSignalInvocationHint*
2230 g_signal_get_invocation_hint (gpointer instance)
2231 {
2232   Emission *emission = NULL;
2233   
2234   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2235
2236   SIGNAL_LOCK ();
2237   emission = emission_find_innermost (instance);
2238   SIGNAL_UNLOCK ();
2239   
2240   return emission ? &emission->ihint : NULL;
2241 }
2242
2243 /**
2244  * g_signal_connect_closure_by_id:
2245  * @instance: (type GObject.Object): the instance to connect to.
2246  * @signal_id: the id of the signal.
2247  * @detail: the detail.
2248  * @closure: the closure to connect.
2249  * @after: whether the handler should be called before or after the
2250  *  default handler of the signal.
2251  *
2252  * Connects a closure to a signal for a particular object.
2253  *
2254  * Returns: the handler id (always greater than 0 for successful connections)
2255  */
2256 gulong
2257 g_signal_connect_closure_by_id (gpointer  instance,
2258                                 guint     signal_id,
2259                                 GQuark    detail,
2260                                 GClosure *closure,
2261                                 gboolean  after)
2262 {
2263   SignalNode *node;
2264   gulong handler_seq_no = 0;
2265   
2266   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2267   g_return_val_if_fail (signal_id > 0, 0);
2268   g_return_val_if_fail (closure != NULL, 0);
2269   
2270   SIGNAL_LOCK ();
2271   node = LOOKUP_SIGNAL_NODE (signal_id);
2272   if (node)
2273     {
2274       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2275         g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2276       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2277         g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2278       else
2279         {
2280           Handler *handler = handler_new (after);
2281           
2282           handler_seq_no = handler->sequential_number;
2283           handler->detail = detail;
2284           handler->closure = g_closure_ref (closure);
2285           g_closure_sink (closure);
2286           add_invalid_closure_notify (handler, instance);
2287           handler_insert (signal_id, instance, handler);
2288           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2289             {
2290               g_closure_set_marshal (closure, node->c_marshaller);
2291               if (node->va_marshaller)
2292                 _g_closure_set_va_marshal (closure, node->va_marshaller);
2293             }
2294         }
2295     }
2296   else
2297     g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2298   SIGNAL_UNLOCK ();
2299   
2300   return handler_seq_no;
2301 }
2302
2303 /**
2304  * g_signal_connect_closure:
2305  * @instance: (type GObject.Object): the instance to connect to.
2306  * @detailed_signal: a string of the form "signal-name::detail".
2307  * @closure: the closure to connect.
2308  * @after: whether the handler should be called before or after the
2309  *  default handler of the signal.
2310  *
2311  * Connects a closure to a signal for a particular object.
2312  *
2313  * Returns: the handler id (always greater than 0 for successful connections)
2314  */
2315 gulong
2316 g_signal_connect_closure (gpointer     instance,
2317                           const gchar *detailed_signal,
2318                           GClosure    *closure,
2319                           gboolean     after)
2320 {
2321   guint signal_id;
2322   gulong handler_seq_no = 0;
2323   GQuark detail = 0;
2324   GType itype;
2325
2326   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2327   g_return_val_if_fail (detailed_signal != NULL, 0);
2328   g_return_val_if_fail (closure != NULL, 0);
2329
2330   SIGNAL_LOCK ();
2331   itype = G_TYPE_FROM_INSTANCE (instance);
2332   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2333   if (signal_id)
2334     {
2335       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2336
2337       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2338         g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2339       else if (!g_type_is_a (itype, node->itype))
2340         g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2341                    G_STRLOC, detailed_signal, instance, g_type_name (itype));
2342       else
2343         {
2344           Handler *handler = handler_new (after);
2345
2346           handler_seq_no = handler->sequential_number;
2347           handler->detail = detail;
2348           handler->closure = g_closure_ref (closure);
2349           g_closure_sink (closure);
2350           add_invalid_closure_notify (handler, instance);
2351           handler_insert (signal_id, instance, handler);
2352           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2353             {
2354               g_closure_set_marshal (handler->closure, node->c_marshaller);
2355               if (node->va_marshaller)
2356                 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2357             }
2358         }
2359     }
2360   else
2361     g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2362                G_STRLOC, detailed_signal, instance, g_type_name (itype));
2363   SIGNAL_UNLOCK ();
2364
2365   return handler_seq_no;
2366 }
2367
2368 static void
2369 node_check_deprecated (const SignalNode *node)
2370 {
2371   static const gchar * g_enable_diagnostic = NULL;
2372
2373   if (G_UNLIKELY (!g_enable_diagnostic))
2374     {
2375       g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2376       if (!g_enable_diagnostic)
2377         g_enable_diagnostic = "0";
2378     }
2379
2380   if (g_enable_diagnostic[0] == '1')
2381     {
2382       if (node->flags & G_SIGNAL_DEPRECATED)
2383         {
2384           g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2385                      "anymore. It will be removed in a future version.",
2386                      type_debug_name (node->itype), node->name);
2387         }
2388     }
2389 }
2390
2391 /**
2392  * g_signal_connect_data:
2393  * @instance: (type GObject.Object): the instance to connect to.
2394  * @detailed_signal: a string of the form "signal-name::detail".
2395  * @c_handler: the #GCallback to connect.
2396  * @data: data to pass to @c_handler calls.
2397  * @destroy_data: a #GClosureNotify for @data.
2398  * @connect_flags: a combination of #GConnectFlags.
2399  *
2400  * Connects a #GCallback function to a signal for a particular object. Similar
2401  * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2402  * which will be called when the signal handler is disconnected and no longer
2403  * used. Specify @connect_flags if you need `..._after()` or
2404  * `..._swapped()` variants of this function.
2405  *
2406  * Returns: the handler id (always greater than 0 for successful connections)
2407  */
2408 gulong
2409 g_signal_connect_data (gpointer       instance,
2410                        const gchar   *detailed_signal,
2411                        GCallback      c_handler,
2412                        gpointer       data,
2413                        GClosureNotify destroy_data,
2414                        GConnectFlags  connect_flags)
2415 {
2416   guint signal_id;
2417   gulong handler_seq_no = 0;
2418   GQuark detail = 0;
2419   GType itype;
2420   gboolean swapped, after;
2421   
2422   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2423   g_return_val_if_fail (detailed_signal != NULL, 0);
2424   g_return_val_if_fail (c_handler != NULL, 0);
2425
2426   swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2427   after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2428
2429   SIGNAL_LOCK ();
2430   itype = G_TYPE_FROM_INSTANCE (instance);
2431   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2432   if (signal_id)
2433     {
2434       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2435
2436       node_check_deprecated (node);
2437
2438       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2439         g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2440       else if (!g_type_is_a (itype, node->itype))
2441         g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2442                    G_STRLOC, detailed_signal, instance, g_type_name (itype));
2443       else
2444         {
2445           Handler *handler = handler_new (after);
2446
2447           handler_seq_no = handler->sequential_number;
2448           handler->detail = detail;
2449           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2450           g_closure_sink (handler->closure);
2451           handler_insert (signal_id, instance, handler);
2452           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2453             {
2454               g_closure_set_marshal (handler->closure, node->c_marshaller);
2455               if (node->va_marshaller)
2456                 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2457             }
2458         }
2459     }
2460   else
2461     g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2462                G_STRLOC, detailed_signal, instance, g_type_name (itype));
2463   SIGNAL_UNLOCK ();
2464
2465   return handler_seq_no;
2466 }
2467
2468 /**
2469  * g_signal_handler_block:
2470  * @instance: (type GObject.Object): The instance to block the signal handler of.
2471  * @handler_id: Handler id of the handler to be blocked.
2472  *
2473  * Blocks a handler of an instance so it will not be called during any
2474  * signal emissions unless it is unblocked again. Thus "blocking" a
2475  * signal handler means to temporarily deactive it, a signal handler
2476  * has to be unblocked exactly the same amount of times it has been
2477  * blocked before to become active again.
2478  *
2479  * The @handler_id has to be a valid signal handler id, connected to a
2480  * signal of @instance.
2481  */
2482 void
2483 g_signal_handler_block (gpointer instance,
2484                         gulong   handler_id)
2485 {
2486   Handler *handler;
2487   
2488   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2489   g_return_if_fail (handler_id > 0);
2490   
2491   SIGNAL_LOCK ();
2492   handler = handler_lookup (instance, handler_id, NULL, NULL);
2493   if (handler)
2494     {
2495 #ifndef G_DISABLE_CHECKS
2496       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2497         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2498 #endif
2499       handler->block_count += 1;
2500     }
2501   else
2502     g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2503   SIGNAL_UNLOCK ();
2504 }
2505
2506 /**
2507  * g_signal_handler_unblock:
2508  * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2509  * @handler_id: Handler id of the handler to be unblocked.
2510  *
2511  * Undoes the effect of a previous g_signal_handler_block() call.  A
2512  * blocked handler is skipped during signal emissions and will not be
2513  * invoked, unblocking it (for exactly the amount of times it has been
2514  * blocked before) reverts its "blocked" state, so the handler will be
2515  * recognized by the signal system and is called upon future or
2516  * currently ongoing signal emissions (since the order in which
2517  * handlers are called during signal emissions is deterministic,
2518  * whether the unblocked handler in question is called as part of a
2519  * currently ongoing emission depends on how far that emission has
2520  * proceeded yet).
2521  *
2522  * The @handler_id has to be a valid id of a signal handler that is
2523  * connected to a signal of @instance and is currently blocked.
2524  */
2525 void
2526 g_signal_handler_unblock (gpointer instance,
2527                           gulong   handler_id)
2528 {
2529   Handler *handler;
2530   
2531   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2532   g_return_if_fail (handler_id > 0);
2533   
2534   SIGNAL_LOCK ();
2535   handler = handler_lookup (instance, handler_id, NULL, NULL);
2536   if (handler)
2537     {
2538       if (handler->block_count)
2539         handler->block_count -= 1;
2540       else
2541         g_warning (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2542     }
2543   else
2544     g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2545   SIGNAL_UNLOCK ();
2546 }
2547
2548 /**
2549  * g_signal_handler_disconnect:
2550  * @instance: (type GObject.Object): The instance to remove the signal handler from.
2551  * @handler_id: Handler id of the handler to be disconnected.
2552  *
2553  * Disconnects a handler from an instance so it will not be called during
2554  * any future or currently ongoing emissions of the signal it has been
2555  * connected to. The @handler_id becomes invalid and may be reused.
2556  *
2557  * The @handler_id has to be a valid signal handler id, connected to a
2558  * signal of @instance.
2559  */
2560 void
2561 g_signal_handler_disconnect (gpointer instance,
2562                              gulong   handler_id)
2563 {
2564   Handler *handler;
2565   guint signal_id;
2566   
2567   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2568   g_return_if_fail (handler_id > 0);
2569   
2570   SIGNAL_LOCK ();
2571   handler = handler_lookup (instance, handler_id, NULL, &signal_id);
2572   if (handler)
2573     {
2574       handler->sequential_number = 0;
2575       handler->block_count = 1;
2576       remove_invalid_closure_notify (handler, instance);
2577       handler_unref_R (signal_id, instance, handler);
2578     }
2579   else
2580     g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2581   SIGNAL_UNLOCK ();
2582 }
2583
2584 /**
2585  * g_signal_handler_is_connected:
2586  * @instance: (type GObject.Object): The instance where a signal handler is sought.
2587  * @handler_id: the handler id.
2588  *
2589  * Returns whether @handler_id is the id of a handler connected to @instance.
2590  *
2591  * Returns: whether @handler_id identifies a handler connected to @instance.
2592  */
2593 gboolean
2594 g_signal_handler_is_connected (gpointer instance,
2595                                gulong   handler_id)
2596 {
2597   Handler *handler;
2598   gboolean connected;
2599
2600   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2601
2602   SIGNAL_LOCK ();
2603   handler = handler_lookup (instance, handler_id, NULL, NULL);
2604   connected = handler != NULL;
2605   SIGNAL_UNLOCK ();
2606
2607   return connected;
2608 }
2609
2610 /**
2611  * g_signal_handlers_destroy:
2612  * @instance: (type GObject.Object): The instance where a signal handler is sought.
2613  */
2614 void
2615 g_signal_handlers_destroy (gpointer instance)
2616 {
2617   GBSearchArray *hlbsa;
2618   
2619   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2620   
2621   SIGNAL_LOCK ();
2622   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2623   if (hlbsa)
2624     {
2625       guint i;
2626       
2627       /* reentrancy caution, delete instance trace first */
2628       g_hash_table_remove (g_handler_list_bsa_ht, instance);
2629       
2630       for (i = 0; i < hlbsa->n_nodes; i++)
2631         {
2632           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2633           Handler *handler = hlist->handlers;
2634           
2635           while (handler)
2636             {
2637               Handler *tmp = handler;
2638               
2639               handler = tmp->next;
2640               tmp->block_count = 1;
2641               /* cruel unlink, this works because _all_ handlers vanish */
2642               tmp->next = NULL;
2643               tmp->prev = tmp;
2644               if (tmp->sequential_number)
2645                 {
2646                   remove_invalid_closure_notify (tmp, instance);
2647                   tmp->sequential_number = 0;
2648                   handler_unref_R (0, NULL, tmp);
2649                 }
2650             }
2651         }
2652       g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2653     }
2654   SIGNAL_UNLOCK ();
2655 }
2656
2657 /**
2658  * g_signal_handler_find:
2659  * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2660  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2661  *  and/or @data the handler has to match.
2662  * @signal_id: Signal the handler has to be connected to.
2663  * @detail: Signal detail the handler has to be connected to.
2664  * @closure: (allow-none): The closure the handler will invoke.
2665  * @func: The C closure callback of the handler (useless for non-C closures).
2666  * @data: The closure data of the handler's closure.
2667  *
2668  * Finds the first signal handler that matches certain selection criteria.
2669  * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2670  * flags, and the criteria values are passed as arguments.
2671  * The match @mask has to be non-0 for successful matches.
2672  * If no handler was found, 0 is returned.
2673  *
2674  * Returns: A valid non-0 signal handler id for a successful match.
2675  */
2676 gulong
2677 g_signal_handler_find (gpointer         instance,
2678                        GSignalMatchType mask,
2679                        guint            signal_id,
2680                        GQuark           detail,
2681                        GClosure        *closure,
2682                        gpointer         func,
2683                        gpointer         data)
2684 {
2685   gulong handler_seq_no = 0;
2686   
2687   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2688   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2689   
2690   if (mask & G_SIGNAL_MATCH_MASK)
2691     {
2692       HandlerMatch *mlist;
2693       
2694       SIGNAL_LOCK ();
2695       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2696       if (mlist)
2697         {
2698           handler_seq_no = mlist->handler->sequential_number;
2699           handler_match_free1_R (mlist, instance);
2700         }
2701       SIGNAL_UNLOCK ();
2702     }
2703   
2704   return handler_seq_no;
2705 }
2706
2707 static guint
2708 signal_handlers_foreach_matched_R (gpointer         instance,
2709                                    GSignalMatchType mask,
2710                                    guint            signal_id,
2711                                    GQuark           detail,
2712                                    GClosure        *closure,
2713                                    gpointer         func,
2714                                    gpointer         data,
2715                                    void           (*callback) (gpointer instance,
2716                                                                gulong   handler_seq_no))
2717 {
2718   HandlerMatch *mlist;
2719   guint n_handlers = 0;
2720   
2721   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2722   while (mlist)
2723     {
2724       n_handlers++;
2725       if (mlist->handler->sequential_number)
2726         {
2727           SIGNAL_UNLOCK ();
2728           callback (instance, mlist->handler->sequential_number);
2729           SIGNAL_LOCK ();
2730         }
2731       mlist = handler_match_free1_R (mlist, instance);
2732     }
2733   
2734   return n_handlers;
2735 }
2736
2737 /**
2738  * g_signal_handlers_block_matched:
2739  * @instance: (type GObject.Object): The instance to block handlers from.
2740  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2741  *  and/or @data the handlers have to match.
2742  * @signal_id: Signal the handlers have to be connected to.
2743  * @detail: Signal detail the handlers have to be connected to.
2744  * @closure: (allow-none): The closure the handlers will invoke.
2745  * @func: The C closure callback of the handlers (useless for non-C closures).
2746  * @data: The closure data of the handlers' closures.
2747  *
2748  * Blocks all handlers on an instance that match a certain selection criteria.
2749  * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2750  * flags, and the criteria values are passed as arguments.
2751  * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2752  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2753  * If no handlers were found, 0 is returned, the number of blocked handlers
2754  * otherwise.
2755  *
2756  * Returns: The number of handlers that matched.
2757  */
2758 guint
2759 g_signal_handlers_block_matched (gpointer         instance,
2760                                  GSignalMatchType mask,
2761                                  guint            signal_id,
2762                                  GQuark           detail,
2763                                  GClosure        *closure,
2764                                  gpointer         func,
2765                                  gpointer         data)
2766 {
2767   guint n_handlers = 0;
2768   
2769   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2770   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2771   
2772   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2773     {
2774       SIGNAL_LOCK ();
2775       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2776                                                       closure, func, data,
2777                                                       g_signal_handler_block);
2778       SIGNAL_UNLOCK ();
2779     }
2780   
2781   return n_handlers;
2782 }
2783
2784 /**
2785  * g_signal_handlers_unblock_matched:
2786  * @instance: (type GObject.Object): The instance to unblock handlers from.
2787  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2788  *  and/or @data the handlers have to match.
2789  * @signal_id: Signal the handlers have to be connected to.
2790  * @detail: Signal detail the handlers have to be connected to.
2791  * @closure: (allow-none): The closure the handlers will invoke.
2792  * @func: The C closure callback of the handlers (useless for non-C closures).
2793  * @data: The closure data of the handlers' closures.
2794  *
2795  * Unblocks all handlers on an instance that match a certain selection
2796  * criteria. The criteria mask is passed as an OR-ed combination of
2797  * #GSignalMatchType flags, and the criteria values are passed as arguments.
2798  * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2799  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2800  * If no handlers were found, 0 is returned, the number of unblocked handlers
2801  * otherwise. The match criteria should not apply to any handlers that are
2802  * not currently blocked.
2803  *
2804  * Returns: The number of handlers that matched.
2805  */
2806 guint
2807 g_signal_handlers_unblock_matched (gpointer         instance,
2808                                    GSignalMatchType mask,
2809                                    guint            signal_id,
2810                                    GQuark           detail,
2811                                    GClosure        *closure,
2812                                    gpointer         func,
2813                                    gpointer         data)
2814 {
2815   guint n_handlers = 0;
2816   
2817   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2818   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2819   
2820   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2821     {
2822       SIGNAL_LOCK ();
2823       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2824                                                       closure, func, data,
2825                                                       g_signal_handler_unblock);
2826       SIGNAL_UNLOCK ();
2827     }
2828   
2829   return n_handlers;
2830 }
2831
2832 /**
2833  * g_signal_handlers_disconnect_matched:
2834  * @instance: (type GObject.Object): The instance to remove handlers from.
2835  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2836  *  and/or @data the handlers have to match.
2837  * @signal_id: Signal the handlers have to be connected to.
2838  * @detail: Signal detail the handlers have to be connected to.
2839  * @closure: (allow-none): The closure the handlers will invoke.
2840  * @func: The C closure callback of the handlers (useless for non-C closures).
2841  * @data: The closure data of the handlers' closures.
2842  *
2843  * Disconnects all handlers on an instance that match a certain
2844  * selection criteria. The criteria mask is passed as an OR-ed
2845  * combination of #GSignalMatchType flags, and the criteria values are
2846  * passed as arguments.  Passing at least one of the
2847  * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2848  * %G_SIGNAL_MATCH_DATA match flags is required for successful
2849  * matches.  If no handlers were found, 0 is returned, the number of
2850  * disconnected handlers otherwise.
2851  *
2852  * Returns: The number of handlers that matched.
2853  */
2854 guint
2855 g_signal_handlers_disconnect_matched (gpointer         instance,
2856                                       GSignalMatchType mask,
2857                                       guint            signal_id,
2858                                       GQuark           detail,
2859                                       GClosure        *closure,
2860                                       gpointer         func,
2861                                       gpointer         data)
2862 {
2863   guint n_handlers = 0;
2864   
2865   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2866   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2867   
2868   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2869     {
2870       SIGNAL_LOCK ();
2871       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2872                                                       closure, func, data,
2873                                                       g_signal_handler_disconnect);
2874       SIGNAL_UNLOCK ();
2875     }
2876   
2877   return n_handlers;
2878 }
2879
2880 /**
2881  * g_signal_has_handler_pending:
2882  * @instance: (type GObject.Object): the object whose signal handlers are sought.
2883  * @signal_id: the signal id.
2884  * @detail: the detail.
2885  * @may_be_blocked: whether blocked handlers should count as match.
2886  *
2887  * Returns whether there are any handlers connected to @instance for the
2888  * given signal id and detail.
2889  *
2890  * One example of when you might use this is when the arguments to the
2891  * signal are difficult to compute. A class implementor may opt to not
2892  * emit the signal if no one is attached anyway, thus saving the cost
2893  * of building the arguments.
2894  *
2895  * Returns: %TRUE if a handler is connected to the signal, %FALSE
2896  *          otherwise.
2897  */
2898 gboolean
2899 g_signal_has_handler_pending (gpointer instance,
2900                               guint    signal_id,
2901                               GQuark   detail,
2902                               gboolean may_be_blocked)
2903 {
2904   HandlerMatch *mlist;
2905   gboolean has_pending;
2906   
2907   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2908   g_return_val_if_fail (signal_id > 0, FALSE);
2909   
2910   SIGNAL_LOCK ();
2911   if (detail)
2912     {
2913       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2914       
2915       if (!(node->flags & G_SIGNAL_DETAILED))
2916         {
2917           g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2918           SIGNAL_UNLOCK ();
2919           return FALSE;
2920         }
2921     }
2922   mlist = handlers_find (instance,
2923                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2924                          signal_id, detail, NULL, NULL, NULL, TRUE);
2925   if (mlist)
2926     {
2927       has_pending = TRUE;
2928       handler_match_free1_R (mlist, instance);
2929     }
2930   else
2931     has_pending = FALSE;
2932   SIGNAL_UNLOCK ();
2933   
2934   return has_pending;
2935 }
2936
2937 /**
2938  * g_signal_emitv:
2939  * @instance_and_params: (array): argument list for the signal emission.
2940  *  The first element in the array is a #GValue for the instance the signal
2941  *  is being emitted on. The rest are any arguments to be passed to the signal.
2942  * @signal_id: the signal id
2943  * @detail: the detail
2944  * @return_value: Location to store the return value of the signal emission.
2945  *
2946  * Emits a signal.
2947  *
2948  * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2949  * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2950  */
2951 void
2952 g_signal_emitv (const GValue *instance_and_params,
2953                 guint         signal_id,
2954                 GQuark        detail,
2955                 GValue       *return_value)
2956 {
2957   gpointer instance;
2958   SignalNode *node;
2959 #ifdef G_ENABLE_DEBUG
2960   const GValue *param_values;
2961   guint i;
2962 #endif
2963   
2964   g_return_if_fail (instance_and_params != NULL);
2965   instance = g_value_peek_pointer (instance_and_params);
2966   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2967   g_return_if_fail (signal_id > 0);
2968
2969 #ifdef G_ENABLE_DEBUG
2970   param_values = instance_and_params + 1;
2971 #endif
2972
2973   SIGNAL_LOCK ();
2974   node = LOOKUP_SIGNAL_NODE (signal_id);
2975   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2976     {
2977       g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2978       SIGNAL_UNLOCK ();
2979       return;
2980     }
2981 #ifdef G_ENABLE_DEBUG
2982   if (detail && !(node->flags & G_SIGNAL_DETAILED))
2983     {
2984       g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2985       SIGNAL_UNLOCK ();
2986       return;
2987     }
2988   for (i = 0; i < node->n_params; i++)
2989     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2990       {
2991         g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
2992                     G_STRLOC,
2993                     type_debug_name (node->param_types[i]),
2994                     i,
2995                     node->name,
2996                     G_VALUE_TYPE_NAME (param_values + i));
2997         SIGNAL_UNLOCK ();
2998         return;
2999       }
3000   if (node->return_type != G_TYPE_NONE)
3001     {
3002       if (!return_value)
3003         {
3004           g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3005                       G_STRLOC,
3006                       type_debug_name (node->return_type),
3007                       node->name);
3008           SIGNAL_UNLOCK ();
3009           return;
3010         }
3011       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3012         {
3013           g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3014                       G_STRLOC,
3015                       type_debug_name (node->return_type),
3016                       node->name,
3017                       G_VALUE_TYPE_NAME (return_value));
3018           SIGNAL_UNLOCK ();
3019           return;
3020         }
3021     }
3022   else
3023     return_value = NULL;
3024 #endif  /* G_ENABLE_DEBUG */
3025
3026   /* optimize NOP emissions */
3027   if (!node->single_va_closure_is_valid)
3028     node_update_single_va_closure (node);
3029
3030   if (node->single_va_closure != NULL &&
3031       (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3032        _g_closure_is_void (node->single_va_closure, instance)))
3033     {
3034       HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3035       if (hlist == NULL || hlist->handlers == NULL)
3036         {
3037           /* nothing to do to emit this signal */
3038           SIGNAL_UNLOCK ();
3039           /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3040           return;
3041         }
3042     }
3043
3044   SIGNAL_UNLOCK ();
3045   signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
3046 }
3047
3048 static inline gboolean
3049 accumulate (GSignalInvocationHint *ihint,
3050             GValue                *return_accu,
3051             GValue                *handler_return,
3052             SignalAccumulator     *accumulator)
3053 {
3054   gboolean continue_emission;
3055
3056   if (!accumulator)
3057     return TRUE;
3058
3059   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3060   g_value_reset (handler_return);
3061
3062   return continue_emission;
3063 }
3064
3065 /**
3066  * g_signal_emit_valist: (skip)
3067  * @instance: the instance the signal is being emitted on.
3068  * @signal_id: the signal id
3069  * @detail: the detail
3070  * @var_args: a list of parameters to be passed to the signal, followed by a
3071  *  location for the return value. If the return type of the signal
3072  *  is #G_TYPE_NONE, the return value location can be omitted.
3073  *
3074  * Emits a signal.
3075  *
3076  * Note that g_signal_emit_valist() resets the return value to the default
3077  * if no handlers are connected, in contrast to g_signal_emitv().
3078  */
3079 void
3080 g_signal_emit_valist (gpointer instance,
3081                       guint    signal_id,
3082                       GQuark   detail,
3083                       va_list  var_args)
3084 {
3085   GValue *instance_and_params;
3086   GType signal_return_type;
3087   GValue *param_values;
3088   SignalNode *node;
3089   guint i, n_params;
3090
3091   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3092   g_return_if_fail (signal_id > 0);
3093
3094   SIGNAL_LOCK ();
3095   node = LOOKUP_SIGNAL_NODE (signal_id);
3096   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3097     {
3098       g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3099       SIGNAL_UNLOCK ();
3100       return;
3101     }
3102 #ifndef G_DISABLE_CHECKS
3103   if (detail && !(node->flags & G_SIGNAL_DETAILED))
3104     {
3105       g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3106       SIGNAL_UNLOCK ();
3107       return;
3108     }
3109 #endif  /* !G_DISABLE_CHECKS */
3110
3111   if (!node->single_va_closure_is_valid)
3112     node_update_single_va_closure (node);
3113
3114   if (node->single_va_closure != NULL)
3115     {
3116       HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3117       Handler *fastpath_handler = NULL;
3118       Handler *l;
3119       GClosure *closure = NULL;
3120       gboolean fastpath = TRUE;
3121       GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3122
3123       if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3124           !_g_closure_is_void (node->single_va_closure, instance))
3125         {
3126           if (_g_closure_supports_invoke_va (node->single_va_closure))
3127             {
3128               closure = node->single_va_closure;
3129               if (node->single_va_closure_is_after)
3130                 run_type = G_SIGNAL_RUN_LAST;
3131               else
3132                 run_type = G_SIGNAL_RUN_FIRST;
3133             }
3134           else
3135             fastpath = FALSE;
3136         }
3137
3138       for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3139         {
3140           if (!l->block_count &&
3141               (!l->detail || l->detail == detail))
3142             {
3143               if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3144                 {
3145                   fastpath = FALSE;
3146                   break;
3147                 }
3148               else
3149                 {
3150                   fastpath_handler = l;
3151                   closure = l->closure;
3152                   if (l->after)
3153                     run_type = G_SIGNAL_RUN_LAST;
3154                   else
3155                     run_type = G_SIGNAL_RUN_FIRST;
3156                 }
3157             }
3158         }
3159
3160       if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3161         {
3162           SIGNAL_UNLOCK ();
3163           return;
3164         }
3165
3166       /* Don't allow no-recurse emission as we might have to restart, which means
3167          we will run multiple handlers and thus must ref all arguments */
3168       if (closure != NULL && (node->flags & (G_SIGNAL_NO_RECURSE)) != 0)
3169         fastpath = FALSE;
3170       
3171       if (fastpath)
3172         {
3173           SignalAccumulator *accumulator;
3174           Emission emission;
3175           GValue *return_accu, accu = G_VALUE_INIT;
3176           guint signal_id;
3177           GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3178           GValue emission_return = G_VALUE_INIT;
3179           GType rtype = node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3180           gboolean static_scope = node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3181
3182           signal_id = node->signal_id;
3183           accumulator = node->accumulator;
3184           if (rtype == G_TYPE_NONE)
3185             return_accu = NULL;
3186           else if (accumulator)
3187             return_accu = &accu;
3188           else
3189             return_accu = &emission_return;
3190
3191           emission.instance = instance;
3192           emission.ihint.signal_id = signal_id;
3193           emission.ihint.detail = detail;
3194           emission.ihint.run_type = run_type;
3195           emission.state = EMISSION_RUN;
3196           emission.chain_type = instance_type;
3197           emission_push (&g_recursive_emissions, &emission);
3198
3199           if (fastpath_handler)
3200             handler_ref (fastpath_handler);
3201
3202           SIGNAL_UNLOCK ();
3203
3204           TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3205
3206           if (rtype != G_TYPE_NONE)
3207             g_value_init (&emission_return, rtype);
3208
3209           if (accumulator)
3210             g_value_init (&accu, rtype);
3211
3212           if (closure != NULL)
3213             {
3214               g_object_ref (instance);
3215               _g_closure_invoke_va (closure,
3216                                     return_accu,
3217                                     instance,
3218                                     var_args,
3219                                     node->n_params,
3220                                     node->param_types);
3221               accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3222             }
3223
3224           SIGNAL_LOCK ();
3225
3226           emission.chain_type = G_TYPE_NONE;
3227           emission_pop (&g_recursive_emissions, &emission);
3228
3229           if (fastpath_handler)
3230             handler_unref_R (signal_id, instance, fastpath_handler);
3231
3232           SIGNAL_UNLOCK ();
3233
3234           if (accumulator)
3235             g_value_unset (&accu);
3236
3237           if (rtype != G_TYPE_NONE)
3238             {
3239               gchar *error = NULL;
3240               for (i = 0; i < node->n_params; i++)
3241                 {
3242                   GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3243                   G_VALUE_COLLECT_SKIP (ptype, var_args);
3244                 }
3245
3246               G_VALUE_LCOPY (&emission_return,
3247                              var_args,
3248                              static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3249                              &error);
3250               if (!error)
3251                 g_value_unset (&emission_return);
3252               else
3253                 {
3254                   g_warning ("%s: %s", G_STRLOC, error);
3255                   g_free (error);
3256                   /* we purposely leak the value here, it might not be
3257                    * in a sane state if an error condition occurred
3258                    */
3259                 }
3260             }
3261           
3262           TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3263
3264           if (closure != NULL)
3265             g_object_unref (instance);
3266
3267           return;
3268         }
3269     }
3270   SIGNAL_UNLOCK ();
3271
3272   n_params = node->n_params;
3273   signal_return_type = node->return_type;
3274   instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
3275   memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
3276   param_values = instance_and_params + 1;
3277
3278   for (i = 0; i < node->n_params; i++)
3279     {
3280       gchar *error;
3281       GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3282       gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3283
3284       G_VALUE_COLLECT_INIT (param_values + i, ptype,
3285                             var_args,
3286                             static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3287                             &error);
3288       if (error)
3289         {
3290           g_warning ("%s: %s", G_STRLOC, error);
3291           g_free (error);
3292
3293           /* we purposely leak the value here, it might not be
3294            * in a sane state if an error condition occoured
3295            */
3296           while (i--)
3297             g_value_unset (param_values + i);
3298
3299           return;
3300         }
3301     }
3302
3303   instance_and_params->g_type = 0;
3304   g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
3305   g_value_set_instance (instance_and_params, instance);
3306   if (signal_return_type == G_TYPE_NONE)
3307     signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
3308   else
3309     {
3310       GValue return_value = G_VALUE_INIT;
3311       gchar *error = NULL;
3312       GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3313       gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3314       
3315       g_value_init (&return_value, rtype);
3316
3317       signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3318
3319       G_VALUE_LCOPY (&return_value,
3320                      var_args,
3321                      static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3322                      &error);
3323       if (!error)
3324         g_value_unset (&return_value);
3325       else
3326         {
3327           g_warning ("%s: %s", G_STRLOC, error);
3328           g_free (error);
3329           
3330           /* we purposely leak the value here, it might not be
3331            * in a sane state if an error condition occurred
3332            */
3333         }
3334     }
3335   for (i = 0; i < n_params; i++)
3336     g_value_unset (param_values + i);
3337   g_value_unset (instance_and_params);
3338 }
3339
3340 /**
3341  * g_signal_emit:
3342  * @instance: (type GObject.Object): the instance the signal is being emitted on.
3343  * @signal_id: the signal id
3344  * @detail: the detail
3345  * @...: parameters to be passed to the signal, followed by a
3346  *  location for the return value. If the return type of the signal
3347  *  is #G_TYPE_NONE, the return value location can be omitted.
3348  *
3349  * Emits a signal.
3350  *
3351  * Note that g_signal_emit() resets the return value to the default
3352  * if no handlers are connected, in contrast to g_signal_emitv().
3353  */
3354 void
3355 g_signal_emit (gpointer instance,
3356                guint    signal_id,
3357                GQuark   detail,
3358                ...)
3359 {
3360   va_list var_args;
3361
3362   va_start (var_args, detail);
3363   g_signal_emit_valist (instance, signal_id, detail, var_args);
3364   va_end (var_args);
3365 }
3366
3367 /**
3368  * g_signal_emit_by_name:
3369  * @instance: (type GObject.Object): the instance the signal is being emitted on.
3370  * @detailed_signal: a string of the form "signal-name::detail".
3371  * @...: parameters to be passed to the signal, followed by a
3372  *  location for the return value. If the return type of the signal
3373  *  is #G_TYPE_NONE, the return value location can be omitted.
3374  *
3375  * Emits a signal.
3376  *
3377  * Note that g_signal_emit_by_name() resets the return value to the default
3378  * if no handlers are connected, in contrast to g_signal_emitv().
3379  */
3380 void
3381 g_signal_emit_by_name (gpointer     instance,
3382                        const gchar *detailed_signal,
3383                        ...)
3384 {
3385   GQuark detail = 0;
3386   guint signal_id;
3387   GType itype;
3388
3389   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3390   g_return_if_fail (detailed_signal != NULL);
3391
3392   itype = G_TYPE_FROM_INSTANCE (instance);
3393
3394   SIGNAL_LOCK ();
3395   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3396   SIGNAL_UNLOCK ();
3397
3398   if (signal_id)
3399     {
3400       va_list var_args;
3401
3402       va_start (var_args, detailed_signal);
3403       g_signal_emit_valist (instance, signal_id, detail, var_args);
3404       va_end (var_args);
3405     }
3406   else
3407     g_warning ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3408                G_STRLOC, detailed_signal, instance, g_type_name (itype));
3409 }
3410
3411 static gboolean
3412 signal_emit_unlocked_R (SignalNode   *node,
3413                         GQuark        detail,
3414                         gpointer      instance,
3415                         GValue       *emission_return,
3416                         const GValue *instance_and_params)
3417 {
3418   SignalAccumulator *accumulator;
3419   Emission emission;
3420   GClosure *class_closure;
3421   HandlerList *hlist;
3422   Handler *handler_list = NULL;
3423   GValue *return_accu, accu = G_VALUE_INIT;
3424   guint signal_id;
3425   gulong max_sequential_handler_number;
3426   gboolean return_value_altered = FALSE;
3427   
3428   TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3429
3430   SIGNAL_LOCK ();
3431   signal_id = node->signal_id;
3432
3433   if (node->flags & G_SIGNAL_NO_RECURSE)
3434     {
3435       Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3436       
3437       if (node)
3438         {
3439           node->state = EMISSION_RESTART;
3440           SIGNAL_UNLOCK ();
3441           return return_value_altered;
3442         }
3443     }
3444   accumulator = node->accumulator;
3445   if (accumulator)
3446     {
3447       SIGNAL_UNLOCK ();
3448       g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3449       return_accu = &accu;
3450       SIGNAL_LOCK ();
3451     }
3452   else
3453     return_accu = emission_return;
3454   emission.instance = instance;
3455   emission.ihint.signal_id = node->signal_id;
3456   emission.ihint.detail = detail;
3457   emission.ihint.run_type = 0;
3458   emission.state = 0;
3459   emission.chain_type = G_TYPE_NONE;
3460   emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3461   class_closure = signal_lookup_closure (node, instance);
3462   
3463  EMIT_RESTART:
3464   
3465   if (handler_list)
3466     handler_unref_R (signal_id, instance, handler_list);
3467   max_sequential_handler_number = g_handler_sequential_number;
3468   hlist = handler_list_lookup (signal_id, instance);
3469   handler_list = hlist ? hlist->handlers : NULL;
3470   if (handler_list)
3471     handler_ref (handler_list);
3472   
3473   emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3474   
3475   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3476     {
3477       emission.state = EMISSION_RUN;
3478
3479       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3480       SIGNAL_UNLOCK ();
3481       g_closure_invoke (class_closure,
3482                         return_accu,
3483                         node->n_params + 1,
3484                         instance_and_params,
3485                         &emission.ihint);
3486       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3487           emission.state == EMISSION_RUN)
3488         emission.state = EMISSION_STOP;
3489       SIGNAL_LOCK ();
3490       emission.chain_type = G_TYPE_NONE;
3491       return_value_altered = TRUE;
3492       
3493       if (emission.state == EMISSION_STOP)
3494         goto EMIT_CLEANUP;
3495       else if (emission.state == EMISSION_RESTART)
3496         goto EMIT_RESTART;
3497     }
3498   
3499   if (node->emission_hooks)
3500     {
3501       gboolean need_destroy, was_in_call, may_recurse = TRUE;
3502       GHook *hook;
3503
3504       emission.state = EMISSION_HOOK;
3505       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3506       while (hook)
3507         {
3508           SignalHook *signal_hook = SIGNAL_HOOK (hook);
3509           
3510           if (!signal_hook->detail || signal_hook->detail == detail)
3511             {
3512               GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3513               
3514               was_in_call = G_HOOK_IN_CALL (hook);
3515               hook->flags |= G_HOOK_FLAG_IN_CALL;
3516               SIGNAL_UNLOCK ();
3517               need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3518               SIGNAL_LOCK ();
3519               if (!was_in_call)
3520                 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3521               if (need_destroy)
3522                 g_hook_destroy_link (node->emission_hooks, hook);
3523             }
3524           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3525         }
3526       
3527       if (emission.state == EMISSION_RESTART)
3528         goto EMIT_RESTART;
3529     }
3530   
3531   if (handler_list)
3532     {
3533       Handler *handler = handler_list;
3534       
3535       emission.state = EMISSION_RUN;
3536       handler_ref (handler);
3537       do
3538         {
3539           Handler *tmp;
3540           
3541           if (handler->after)
3542             {
3543               handler_unref_R (signal_id, instance, handler_list);
3544               handler_list = handler;
3545               break;
3546             }
3547           else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3548                    handler->sequential_number < max_sequential_handler_number)
3549             {
3550               SIGNAL_UNLOCK ();
3551               g_closure_invoke (handler->closure,
3552                                 return_accu,
3553                                 node->n_params + 1,
3554                                 instance_and_params,
3555                                 &emission.ihint);
3556               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3557                   emission.state == EMISSION_RUN)
3558                 emission.state = EMISSION_STOP;
3559               SIGNAL_LOCK ();
3560               return_value_altered = TRUE;
3561               
3562               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3563             }
3564           else
3565             tmp = handler->next;
3566           
3567           if (tmp)
3568             handler_ref (tmp);
3569           handler_unref_R (signal_id, instance, handler_list);
3570           handler_list = handler;
3571           handler = tmp;
3572         }
3573       while (handler);
3574       
3575       if (emission.state == EMISSION_STOP)
3576         goto EMIT_CLEANUP;
3577       else if (emission.state == EMISSION_RESTART)
3578         goto EMIT_RESTART;
3579     }
3580   
3581   emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3582   
3583   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3584     {
3585       emission.state = EMISSION_RUN;
3586       
3587       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3588       SIGNAL_UNLOCK ();
3589       g_closure_invoke (class_closure,
3590                         return_accu,
3591                         node->n_params + 1,
3592                         instance_and_params,
3593                         &emission.ihint);
3594       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3595           emission.state == EMISSION_RUN)
3596         emission.state = EMISSION_STOP;
3597       SIGNAL_LOCK ();
3598       emission.chain_type = G_TYPE_NONE;
3599       return_value_altered = TRUE;
3600       
3601       if (emission.state == EMISSION_STOP)
3602         goto EMIT_CLEANUP;
3603       else if (emission.state == EMISSION_RESTART)
3604         goto EMIT_RESTART;
3605     }
3606   
3607   if (handler_list)
3608     {
3609       Handler *handler = handler_list;
3610       
3611       emission.state = EMISSION_RUN;
3612       handler_ref (handler);
3613       do
3614         {
3615           Handler *tmp;
3616           
3617           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3618               handler->sequential_number < max_sequential_handler_number)
3619             {
3620               SIGNAL_UNLOCK ();
3621               g_closure_invoke (handler->closure,
3622                                 return_accu,
3623                                 node->n_params + 1,
3624                                 instance_and_params,
3625                                 &emission.ihint);
3626               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3627                   emission.state == EMISSION_RUN)
3628                 emission.state = EMISSION_STOP;
3629               SIGNAL_LOCK ();
3630               return_value_altered = TRUE;
3631               
3632               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3633             }
3634           else
3635             tmp = handler->next;
3636           
3637           if (tmp)
3638             handler_ref (tmp);
3639           handler_unref_R (signal_id, instance, handler);
3640           handler = tmp;
3641         }
3642       while (handler);
3643       
3644       if (emission.state == EMISSION_STOP)
3645         goto EMIT_CLEANUP;
3646       else if (emission.state == EMISSION_RESTART)
3647         goto EMIT_RESTART;
3648     }
3649   
3650  EMIT_CLEANUP:
3651   
3652   emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3653   
3654   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3655     {
3656       gboolean need_unset = FALSE;
3657       
3658       emission.state = EMISSION_STOP;
3659       
3660       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3661       SIGNAL_UNLOCK ();
3662       if (node->return_type != G_TYPE_NONE && !accumulator)
3663         {
3664           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3665           need_unset = TRUE;
3666         }
3667       g_closure_invoke (class_closure,
3668                         node->return_type != G_TYPE_NONE ? &accu : NULL,
3669                         node->n_params + 1,
3670                         instance_and_params,
3671                         &emission.ihint);
3672       if (need_unset)
3673         g_value_unset (&accu);
3674       SIGNAL_LOCK ();
3675       emission.chain_type = G_TYPE_NONE;
3676       
3677       if (emission.state == EMISSION_RESTART)
3678         goto EMIT_RESTART;
3679     }
3680   
3681   if (handler_list)
3682     handler_unref_R (signal_id, instance, handler_list);
3683   
3684   emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3685   SIGNAL_UNLOCK ();
3686   if (accumulator)
3687     g_value_unset (&accu);
3688
3689   TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3690
3691   return return_value_altered;
3692 }
3693
3694 static void
3695 add_invalid_closure_notify (Handler  *handler,
3696                             gpointer  instance)
3697 {
3698   g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3699   handler->has_invalid_closure_notify = 1;
3700 }
3701
3702 static void
3703 remove_invalid_closure_notify (Handler  *handler,
3704                                gpointer  instance)
3705 {
3706   if (handler->has_invalid_closure_notify)
3707     {
3708       g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3709       handler->has_invalid_closure_notify = 0;
3710     }
3711 }
3712
3713 static void
3714 invalid_closure_notify (gpointer  instance,
3715                         GClosure *closure)
3716 {
3717   Handler *handler;
3718   guint signal_id;
3719
3720   SIGNAL_LOCK ();
3721
3722   handler = handler_lookup (instance, 0, closure, &signal_id);
3723   g_assert (handler->closure == closure);
3724
3725   handler->sequential_number = 0;
3726   handler->block_count = 1;
3727   handler_unref_R (signal_id, instance, handler);
3728
3729   SIGNAL_UNLOCK ();
3730 }
3731
3732 static const gchar*
3733 type_debug_name (GType type)
3734 {
3735   if (type)
3736     {
3737       const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3738       return name ? name : "<unknown>";
3739     }
3740   else
3741     return "<invalid>";
3742 }
3743
3744 /**
3745  * g_signal_accumulator_true_handled:
3746  * @ihint: standard #GSignalAccumulator parameter
3747  * @return_accu: standard #GSignalAccumulator parameter
3748  * @handler_return: standard #GSignalAccumulator parameter
3749  * @dummy: standard #GSignalAccumulator parameter
3750  *
3751  * A predefined #GSignalAccumulator for signals that return a
3752  * boolean values. The behavior that this accumulator gives is
3753  * that a return of %TRUE stops the signal emission: no further
3754  * callbacks will be invoked, while a return of %FALSE allows
3755  * the emission to continue. The idea here is that a %TRUE return
3756  * indicates that the callback handled the signal, and no further
3757  * handling is needed.
3758  *
3759  * Since: 2.4
3760  *
3761  * Returns: standard #GSignalAccumulator result
3762  */
3763 gboolean
3764 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3765                                    GValue                *return_accu,
3766                                    const GValue          *handler_return,
3767                                    gpointer               dummy)
3768 {
3769   gboolean continue_emission;
3770   gboolean signal_handled;
3771   
3772   signal_handled = g_value_get_boolean (handler_return);
3773   g_value_set_boolean (return_accu, signal_handled);
3774   continue_emission = !signal_handled;
3775   
3776   return continue_emission;
3777 }
3778
3779 /**
3780  * g_signal_accumulator_first_wins:
3781  * @ihint: standard #GSignalAccumulator parameter
3782  * @return_accu: standard #GSignalAccumulator parameter
3783  * @handler_return: standard #GSignalAccumulator parameter
3784  * @dummy: standard #GSignalAccumulator parameter
3785  *
3786  * A predefined #GSignalAccumulator for signals intended to be used as a
3787  * hook for application code to provide a particular value.  Usually
3788  * only one such value is desired and multiple handlers for the same
3789  * signal don't make much sense (except for the case of the default
3790  * handler defined in the class structure, in which case you will
3791  * usually want the signal connection to override the class handler).
3792  *
3793  * This accumulator will use the return value from the first signal
3794  * handler that is run as the return value for the signal and not run
3795  * any further handlers (ie: the first handler "wins").
3796  *
3797  * Returns: standard #GSignalAccumulator result
3798  *
3799  * Since: 2.28
3800  **/
3801 gboolean
3802 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
3803                                  GValue                *return_accu,
3804                                  const GValue          *handler_return,
3805                                  gpointer               dummy)
3806 {
3807   g_value_copy (handler_return, return_accu);
3808   return FALSE;
3809 }