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