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