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