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