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