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