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