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