docs: fix many documentation issues in gobject/
[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 /**
1737  * g_signal_set_va_marshaller:
1738  * @signal_id: the signal id
1739  * @instance_type: the instance type on which to set the marshaller.
1740  * @va_marshaller: the marshaller to set.
1741  *
1742  * Change the #GSignalCVaMarshaller used for a given signal.  This is a
1743  * specialised form of the marshaller that can often be used for the
1744  * common case of a single connected signal handler and avoids the
1745  * overhead of #GValue.  Its use is optional.
1746  *
1747  * Since: 2.32
1748  */
1749 void
1750 g_signal_set_va_marshaller (guint              signal_id,
1751                             GType              instance_type,
1752                             GSignalCVaMarshaller va_marshaller)
1753 {
1754   SignalNode *node;
1755   
1756   g_return_if_fail (signal_id > 0);
1757   g_return_if_fail (va_marshaller != NULL);
1758   
1759   SIGNAL_LOCK ();
1760   node = LOOKUP_SIGNAL_NODE (signal_id);
1761   if (node)
1762     {
1763       node->va_marshaller = va_marshaller;
1764       if (node->class_closure_bsa)
1765         {
1766           ClassClosure *cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1767           if (cc->closure->marshal == node->c_marshaller)
1768             _g_closure_set_va_marshal (cc->closure, va_marshaller);
1769         }
1770
1771       node->single_va_closure_is_valid = FALSE;
1772     }
1773
1774   SIGNAL_UNLOCK ();
1775 }
1776
1777
1778 /**
1779  * g_signal_new_valist:
1780  * @signal_name: the name for the signal
1781  * @itype: the type this signal pertains to. It will also pertain to
1782  *  types which are derived from this type.
1783  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1784  *  the default handler is to be invoked. You should at least specify
1785  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1786  * @class_closure: The closure to invoke on signal emission; may be %NULL.
1787  * @accumulator: the accumulator for this signal; may be %NULL.
1788  * @accu_data: user data for the @accumulator.
1789  * @c_marshaller: (allow-none): the function to translate arrays of parameter
1790  *  values to signal emissions into C language callback invocations or %NULL.
1791  * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1792  *  without a return value.
1793  * @n_params: the number of parameter types in @args.
1794  * @args: va_list of #GType, one for each parameter.
1795  *
1796  * Creates a new signal. (This is usually done in the class initializer.)
1797  *
1798  * See g_signal_new() for details on allowed signal names.
1799  *
1800  * If c_marshaller is %NULL, g_cclosure_marshal_generic() will be used as
1801  * the marshaller for this signal.
1802  *
1803  * Returns: the signal id
1804  */
1805 guint
1806 g_signal_new_valist (const gchar       *signal_name,
1807                      GType              itype,
1808                      GSignalFlags       signal_flags,
1809                      GClosure          *class_closure,
1810                      GSignalAccumulator accumulator,
1811                      gpointer           accu_data,
1812                      GSignalCMarshaller c_marshaller,
1813                      GType              return_type,
1814                      guint              n_params,
1815                      va_list            args)
1816 {
1817   GType *param_types;
1818   guint i;
1819   guint signal_id;
1820
1821   if (n_params > 0)
1822     {
1823       param_types = g_new (GType, n_params);
1824
1825       for (i = 0; i < n_params; i++)
1826         param_types[i] = va_arg (args, GType);
1827     }
1828   else
1829     param_types = NULL;
1830
1831   signal_id = g_signal_newv (signal_name, itype, signal_flags,
1832                              class_closure, accumulator, accu_data, c_marshaller,
1833                              return_type, n_params, param_types);
1834   g_free (param_types);
1835
1836   return signal_id;
1837 }
1838
1839 static void
1840 signal_destroy_R (SignalNode *signal_node)
1841 {
1842   SignalNode node = *signal_node;
1843
1844   signal_node->destroyed = TRUE;
1845   
1846   /* reentrancy caution, zero out real contents first */
1847   signal_node->single_va_closure_is_valid = FALSE;
1848   signal_node->n_params = 0;
1849   signal_node->param_types = NULL;
1850   signal_node->return_type = 0;
1851   signal_node->class_closure_bsa = NULL;
1852   signal_node->accumulator = NULL;
1853   signal_node->c_marshaller = NULL;
1854   signal_node->va_marshaller = NULL;
1855   signal_node->emission_hooks = NULL;
1856   
1857 #ifdef  G_ENABLE_DEBUG
1858   /* check current emissions */
1859   {
1860     Emission *emission;
1861     
1862     for (emission = g_emissions; emission; emission = emission->next)
1863       if (emission->ihint.signal_id == node.signal_id)
1864         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance '%p')",
1865                     node.name, emission->instance);
1866   }
1867 #endif
1868   
1869   /* free contents that need to
1870    */
1871   SIGNAL_UNLOCK ();
1872   g_free (node.param_types);
1873   if (node.class_closure_bsa)
1874     {
1875       guint i;
1876
1877       for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1878         {
1879           ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1880
1881           g_closure_unref (cc->closure);
1882         }
1883       g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1884     }
1885   g_free (node.accumulator);
1886   if (node.emission_hooks)
1887     {
1888       g_hook_list_clear (node.emission_hooks);
1889       g_free (node.emission_hooks);
1890     }
1891   SIGNAL_LOCK ();
1892 }
1893
1894 /**
1895  * g_signal_override_class_closure:
1896  * @signal_id: the signal id
1897  * @instance_type: the instance type on which to override the class closure
1898  *  for the signal.
1899  * @class_closure: the closure.
1900  *
1901  * Overrides the class closure (i.e. the default handler) for the given signal
1902  * for emissions on instances of @instance_type. @instance_type must be derived
1903  * from the type to which the signal belongs.
1904  *
1905  * See g_signal_chain_from_overridden() and
1906  * g_signal_chain_from_overridden_handler() for how to chain up to the
1907  * parent class closure from inside the overridden one.
1908  */
1909 void
1910 g_signal_override_class_closure (guint     signal_id,
1911                                  GType     instance_type,
1912                                  GClosure *class_closure)
1913 {
1914   SignalNode *node;
1915   
1916   g_return_if_fail (signal_id > 0);
1917   g_return_if_fail (class_closure != NULL);
1918   
1919   SIGNAL_LOCK ();
1920   node = LOOKUP_SIGNAL_NODE (signal_id);
1921   node_check_deprecated (node);
1922   if (!g_type_is_a (instance_type, node->itype))
1923     g_warning ("%s: type '%s' cannot be overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1924   else
1925     {
1926       ClassClosure *cc = signal_find_class_closure (node, instance_type);
1927       
1928       if (cc && cc->instance_type == instance_type)
1929         g_warning ("%s: type '%s' is already overridden for signal id '%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1930       else
1931         signal_add_class_closure (node, instance_type, class_closure);
1932     }
1933   SIGNAL_UNLOCK ();
1934 }
1935
1936 /**
1937  * g_signal_override_class_handler:
1938  * @signal_name: the name for the signal
1939  * @instance_type: the instance type on which to override the class handler
1940  *  for the signal.
1941  * @class_handler: the handler.
1942  *
1943  * Overrides the class closure (i.e. the default handler) for the
1944  * given signal for emissions on instances of @instance_type with
1945  * callback @class_handler. @instance_type must be derived from the
1946  * type to which the signal belongs.
1947  *
1948  * See g_signal_chain_from_overridden() and
1949  * g_signal_chain_from_overridden_handler() for how to chain up to the
1950  * parent class closure from inside the overridden one.
1951  *
1952  * Since: 2.18
1953  */
1954 void
1955 g_signal_override_class_handler (const gchar *signal_name,
1956                                  GType        instance_type,
1957                                  GCallback    class_handler)
1958 {
1959   guint signal_id;
1960
1961   g_return_if_fail (signal_name != NULL);
1962   g_return_if_fail (instance_type != G_TYPE_NONE);
1963   g_return_if_fail (class_handler != NULL);
1964
1965   signal_id = g_signal_lookup (signal_name, instance_type);
1966
1967   if (signal_id)
1968     g_signal_override_class_closure (signal_id, instance_type,
1969                                      g_cclosure_new (class_handler, NULL, NULL));
1970   else
1971     g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1972                G_STRLOC, signal_name, instance_type);
1973
1974 }
1975
1976 /**
1977  * g_signal_chain_from_overridden:
1978  * @instance_and_params: (array) the argument list of the signal emission.
1979  *  The first element in the array is a #GValue for the instance the signal
1980  *  is being emitted on. The rest are any arguments to be passed to the signal.
1981  * @return_value: Location for the return value.
1982  *
1983  * Calls the original class closure of a signal. This function should only
1984  * be called from an overridden class closure; see
1985  * g_signal_override_class_closure() and
1986  * g_signal_override_class_handler().
1987  */
1988 void
1989 g_signal_chain_from_overridden (const GValue *instance_and_params,
1990                                 GValue       *return_value)
1991 {
1992   GType chain_type = 0, restore_type = 0;
1993   Emission *emission = NULL;
1994   GClosure *closure = NULL;
1995   guint n_params = 0;
1996   gpointer instance;
1997   
1998   g_return_if_fail (instance_and_params != NULL);
1999   instance = g_value_peek_pointer (instance_and_params);
2000   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2001   
2002   SIGNAL_LOCK ();
2003   emission = emission_find_innermost (instance);
2004   if (emission)
2005     {
2006       SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2007       
2008       g_assert (node != NULL);  /* paranoid */
2009       
2010       /* we should probably do the same parameter checks as g_signal_emit() here.
2011        */
2012       if (emission->chain_type != G_TYPE_NONE)
2013         {
2014           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2015           
2016           g_assert (cc != NULL);        /* closure currently in call stack */
2017
2018           n_params = node->n_params;
2019           restore_type = cc->instance_type;
2020           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2021           if (cc && cc->instance_type != restore_type)
2022             {
2023               closure = cc->closure;
2024               chain_type = cc->instance_type;
2025             }
2026         }
2027       else
2028         g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2029     }
2030   else
2031     g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2032
2033   if (closure)
2034     {
2035       emission->chain_type = chain_type;
2036       SIGNAL_UNLOCK ();
2037       g_closure_invoke (closure,
2038                         return_value,
2039                         n_params + 1,
2040                         instance_and_params,
2041                         &emission->ihint);
2042       SIGNAL_LOCK ();
2043       emission->chain_type = restore_type;
2044     }
2045   SIGNAL_UNLOCK ();
2046 }
2047
2048 /**
2049  * g_signal_chain_from_overridden_handler: (skip)
2050  * @instance: the instance the signal is being emitted on.
2051  * @...: parameters to be passed to the parent class closure, followed by a
2052  *  location for the return value. If the return type of the signal
2053  *  is #G_TYPE_NONE, the return value location can be omitted.
2054  *
2055  * Calls the original class closure of a signal. This function should
2056  * only be called from an overridden class closure; see
2057  * g_signal_override_class_closure() and
2058  * g_signal_override_class_handler().
2059  *
2060  * Since: 2.18
2061  */
2062 void
2063 g_signal_chain_from_overridden_handler (gpointer instance,
2064                                         ...)
2065 {
2066   GType chain_type = 0, restore_type = 0;
2067   Emission *emission = NULL;
2068   GClosure *closure = NULL;
2069   SignalNode *node;
2070   guint n_params = 0;
2071
2072   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2073
2074   SIGNAL_LOCK ();
2075   emission = emission_find_innermost (instance);
2076   if (emission)
2077     {
2078       node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
2079
2080       g_assert (node != NULL);  /* paranoid */
2081
2082       /* we should probably do the same parameter checks as g_signal_emit() here.
2083        */
2084       if (emission->chain_type != G_TYPE_NONE)
2085         {
2086           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
2087
2088           g_assert (cc != NULL);        /* closure currently in call stack */
2089
2090           n_params = node->n_params;
2091           restore_type = cc->instance_type;
2092           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
2093           if (cc && cc->instance_type != restore_type)
2094             {
2095               closure = cc->closure;
2096               chain_type = cc->instance_type;
2097             }
2098         }
2099       else
2100         g_warning ("%s: signal id '%u' cannot be chained from current emission stage for instance '%p'", G_STRLOC, node->signal_id, instance);
2101     }
2102   else
2103     g_warning ("%s: no signal is currently being emitted for instance '%p'", G_STRLOC, instance);
2104
2105   if (closure)
2106     {
2107       GValue *instance_and_params;
2108       GType signal_return_type;
2109       GValue *param_values;
2110       va_list var_args;
2111       guint i;
2112
2113       va_start (var_args, instance);
2114
2115       signal_return_type = node->return_type;
2116       instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
2117       memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
2118       param_values = instance_and_params + 1;
2119
2120       for (i = 0; i < node->n_params; i++)
2121         {
2122           gchar *error;
2123           GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2124           gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2125
2126           SIGNAL_UNLOCK ();
2127           G_VALUE_COLLECT_INIT (param_values + i, ptype,
2128                                 var_args,
2129                                 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2130                                 &error);
2131           if (error)
2132             {
2133               g_warning ("%s: %s", G_STRLOC, error);
2134               g_free (error);
2135
2136               /* we purposely leak the value here, it might not be
2137                * in a sane state if an error condition occoured
2138                */
2139               while (i--)
2140                 g_value_unset (param_values + i);
2141
2142               va_end (var_args);
2143               return;
2144             }
2145           SIGNAL_LOCK ();
2146         }
2147
2148       SIGNAL_UNLOCK ();
2149       instance_and_params->g_type = 0;
2150       g_value_init_from_instance (instance_and_params, instance);
2151       SIGNAL_LOCK ();
2152
2153       emission->chain_type = chain_type;
2154       SIGNAL_UNLOCK ();
2155
2156       if (signal_return_type == G_TYPE_NONE)
2157         {
2158           g_closure_invoke (closure,
2159                             NULL,
2160                             n_params + 1,
2161                             instance_and_params,
2162                             &emission->ihint);
2163         }
2164       else
2165         {
2166           GValue return_value = G_VALUE_INIT;
2167           gchar *error = NULL;
2168           GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2169           gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2170
2171           g_value_init (&return_value, rtype);
2172
2173           g_closure_invoke (closure,
2174                             &return_value,
2175                             n_params + 1,
2176                             instance_and_params,
2177                             &emission->ihint);
2178
2179           G_VALUE_LCOPY (&return_value,
2180                          var_args,
2181                          static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2182                          &error);
2183           if (!error)
2184             {
2185               g_value_unset (&return_value);
2186             }
2187           else
2188             {
2189               g_warning ("%s: %s", G_STRLOC, error);
2190               g_free (error);
2191
2192               /* we purposely leak the value here, it might not be
2193                * in a sane state if an error condition occurred
2194                */
2195             }
2196         }
2197
2198       for (i = 0; i < n_params; i++)
2199         g_value_unset (param_values + i);
2200       g_value_unset (instance_and_params);
2201
2202       va_end (var_args);
2203
2204       SIGNAL_LOCK ();
2205       emission->chain_type = restore_type;
2206     }
2207   SIGNAL_UNLOCK ();
2208 }
2209
2210 /**
2211  * g_signal_get_invocation_hint:
2212  * @instance: (type GObject.Object): the instance to query
2213  *
2214  * Returns the invocation hint of the innermost signal emission of instance.
2215  *
2216  * Returns: (transfer none): the invocation hint of the innermost signal  emission.
2217  */
2218 GSignalInvocationHint*
2219 g_signal_get_invocation_hint (gpointer instance)
2220 {
2221   Emission *emission = NULL;
2222   
2223   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2224
2225   SIGNAL_LOCK ();
2226   emission = emission_find_innermost (instance);
2227   SIGNAL_UNLOCK ();
2228   
2229   return emission ? &emission->ihint : NULL;
2230 }
2231
2232 /**
2233  * g_signal_connect_closure_by_id:
2234  * @instance: (type GObject.Object): the instance to connect to.
2235  * @signal_id: the id of the signal.
2236  * @detail: the detail.
2237  * @closure: the closure to connect.
2238  * @after: whether the handler should be called before or after the
2239  *  default handler of the signal.
2240  *
2241  * Connects a closure to a signal for a particular object.
2242  *
2243  * Returns: the handler id (always greater than 0 for successful connections)
2244  */
2245 gulong
2246 g_signal_connect_closure_by_id (gpointer  instance,
2247                                 guint     signal_id,
2248                                 GQuark    detail,
2249                                 GClosure *closure,
2250                                 gboolean  after)
2251 {
2252   SignalNode *node;
2253   gulong handler_seq_no = 0;
2254   
2255   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2256   g_return_val_if_fail (signal_id > 0, 0);
2257   g_return_val_if_fail (closure != NULL, 0);
2258   
2259   SIGNAL_LOCK ();
2260   node = LOOKUP_SIGNAL_NODE (signal_id);
2261   if (node)
2262     {
2263       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2264         g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2265       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2266         g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2267       else
2268         {
2269           Handler *handler = handler_new (after);
2270           
2271           handler_seq_no = handler->sequential_number;
2272           handler->detail = detail;
2273           handler->closure = g_closure_ref (closure);
2274           g_closure_sink (closure);
2275           add_invalid_closure_notify (handler, instance);
2276           handler_insert (signal_id, instance, handler);
2277           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2278             {
2279               g_closure_set_marshal (closure, node->c_marshaller);
2280               if (node->va_marshaller)
2281                 _g_closure_set_va_marshal (closure, node->va_marshaller);
2282             }
2283         }
2284     }
2285   else
2286     g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2287   SIGNAL_UNLOCK ();
2288   
2289   return handler_seq_no;
2290 }
2291
2292 /**
2293  * g_signal_connect_closure:
2294  * @instance: (type GObject.Object): the instance to connect to.
2295  * @detailed_signal: a string of the form "signal-name::detail".
2296  * @closure: the closure to connect.
2297  * @after: whether the handler should be called before or after the
2298  *  default handler of the signal.
2299  *
2300  * Connects a closure to a signal for a particular object.
2301  *
2302  * Returns: the handler id (always greater than 0 for successful connections)
2303  */
2304 gulong
2305 g_signal_connect_closure (gpointer     instance,
2306                           const gchar *detailed_signal,
2307                           GClosure    *closure,
2308                           gboolean     after)
2309 {
2310   guint signal_id;
2311   gulong handler_seq_no = 0;
2312   GQuark detail = 0;
2313   GType itype;
2314
2315   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2316   g_return_val_if_fail (detailed_signal != NULL, 0);
2317   g_return_val_if_fail (closure != NULL, 0);
2318
2319   SIGNAL_LOCK ();
2320   itype = G_TYPE_FROM_INSTANCE (instance);
2321   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2322   if (signal_id)
2323     {
2324       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2325
2326       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2327         g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2328       else if (!g_type_is_a (itype, node->itype))
2329         g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2330                    G_STRLOC, detailed_signal, instance, g_type_name (itype));
2331       else
2332         {
2333           Handler *handler = handler_new (after);
2334
2335           handler_seq_no = handler->sequential_number;
2336           handler->detail = detail;
2337           handler->closure = g_closure_ref (closure);
2338           g_closure_sink (closure);
2339           add_invalid_closure_notify (handler, instance);
2340           handler_insert (signal_id, instance, handler);
2341           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2342             {
2343               g_closure_set_marshal (handler->closure, node->c_marshaller);
2344               if (node->va_marshaller)
2345                 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2346             }
2347         }
2348     }
2349   else
2350     g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2351                G_STRLOC, detailed_signal, instance, g_type_name (itype));
2352   SIGNAL_UNLOCK ();
2353
2354   return handler_seq_no;
2355 }
2356
2357 static void
2358 node_check_deprecated (const SignalNode *node)
2359 {
2360   static const gchar * g_enable_diagnostic = NULL;
2361
2362   if (G_UNLIKELY (!g_enable_diagnostic))
2363     {
2364       g_enable_diagnostic = g_getenv ("G_ENABLE_DIAGNOSTIC");
2365       if (!g_enable_diagnostic)
2366         g_enable_diagnostic = "0";
2367     }
2368
2369   if (g_enable_diagnostic[0] == '1')
2370     {
2371       if (node->flags & G_SIGNAL_DEPRECATED)
2372         {
2373           g_warning ("The signal %s::%s is deprecated and shouldn't be used "
2374                      "anymore. It will be removed in a future version.",
2375                      type_debug_name (node->itype), node->name);
2376         }
2377     }
2378 }
2379
2380 /**
2381  * g_signal_connect_data:
2382  * @instance: (type GObject.Object): the instance to connect to.
2383  * @detailed_signal: a string of the form "signal-name::detail".
2384  * @c_handler: the #GCallback to connect.
2385  * @data: data to pass to @c_handler calls.
2386  * @destroy_data: a #GClosureNotify for @data.
2387  * @connect_flags: a combination of #GConnectFlags.
2388  *
2389  * Connects a #GCallback function to a signal for a particular object. Similar
2390  * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2391  * which will be called when the signal handler is disconnected and no longer
2392  * used. Specify @connect_flags if you need `..._after()` or
2393  * `..._swapped()` variants of this function.
2394  *
2395  * Returns: the handler id (always greater than 0 for successful connections)
2396  */
2397 gulong
2398 g_signal_connect_data (gpointer       instance,
2399                        const gchar   *detailed_signal,
2400                        GCallback      c_handler,
2401                        gpointer       data,
2402                        GClosureNotify destroy_data,
2403                        GConnectFlags  connect_flags)
2404 {
2405   guint signal_id;
2406   gulong handler_seq_no = 0;
2407   GQuark detail = 0;
2408   GType itype;
2409   gboolean swapped, after;
2410   
2411   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2412   g_return_val_if_fail (detailed_signal != NULL, 0);
2413   g_return_val_if_fail (c_handler != NULL, 0);
2414
2415   swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2416   after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2417
2418   SIGNAL_LOCK ();
2419   itype = G_TYPE_FROM_INSTANCE (instance);
2420   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2421   if (signal_id)
2422     {
2423       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2424
2425       node_check_deprecated (node);
2426
2427       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2428         g_warning ("%s: signal '%s' does not support details", G_STRLOC, detailed_signal);
2429       else if (!g_type_is_a (itype, node->itype))
2430         g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2431                    G_STRLOC, detailed_signal, instance, g_type_name (itype));
2432       else
2433         {
2434           Handler *handler = handler_new (after);
2435
2436           handler_seq_no = handler->sequential_number;
2437           handler->detail = detail;
2438           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2439           g_closure_sink (handler->closure);
2440           handler_insert (signal_id, instance, handler);
2441           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2442             {
2443               g_closure_set_marshal (handler->closure, node->c_marshaller);
2444               if (node->va_marshaller)
2445                 _g_closure_set_va_marshal (handler->closure, node->va_marshaller);
2446             }
2447         }
2448     }
2449   else
2450     g_warning ("%s: signal '%s' is invalid for instance '%p' of type '%s'",
2451                G_STRLOC, detailed_signal, instance, g_type_name (itype));
2452   SIGNAL_UNLOCK ();
2453
2454   return handler_seq_no;
2455 }
2456
2457 /**
2458  * g_signal_handler_block:
2459  * @instance: (type GObject.Object): The instance to block the signal handler of.
2460  * @handler_id: Handler id of the handler to be blocked.
2461  *
2462  * Blocks a handler of an instance so it will not be called during any
2463  * signal emissions unless it is unblocked again. Thus "blocking" a
2464  * signal handler means to temporarily deactive it, a signal handler
2465  * has to be unblocked exactly the same amount of times it has been
2466  * blocked before to become active again.
2467  *
2468  * The @handler_id has to be a valid signal handler id, connected to a
2469  * signal of @instance.
2470  */
2471 void
2472 g_signal_handler_block (gpointer instance,
2473                         gulong   handler_id)
2474 {
2475   Handler *handler;
2476   
2477   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2478   g_return_if_fail (handler_id > 0);
2479   
2480   SIGNAL_LOCK ();
2481   handler = handler_lookup (instance, handler_id, NULL, NULL);
2482   if (handler)
2483     {
2484 #ifndef G_DISABLE_CHECKS
2485       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2486         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2487 #endif
2488       handler->block_count += 1;
2489     }
2490   else
2491     g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2492   SIGNAL_UNLOCK ();
2493 }
2494
2495 /**
2496  * g_signal_handler_unblock:
2497  * @instance: (type GObject.Object): The instance to unblock the signal handler of.
2498  * @handler_id: Handler id of the handler to be unblocked.
2499  *
2500  * Undoes the effect of a previous g_signal_handler_block() call.  A
2501  * blocked handler is skipped during signal emissions and will not be
2502  * invoked, unblocking it (for exactly the amount of times it has been
2503  * blocked before) reverts its "blocked" state, so the handler will be
2504  * recognized by the signal system and is called upon future or
2505  * currently ongoing signal emissions (since the order in which
2506  * handlers are called during signal emissions is deterministic,
2507  * whether the unblocked handler in question is called as part of a
2508  * currently ongoing emission depends on how far that emission has
2509  * proceeded yet).
2510  *
2511  * The @handler_id has to be a valid id of a signal handler that is
2512  * connected to a signal of @instance and is currently blocked.
2513  */
2514 void
2515 g_signal_handler_unblock (gpointer instance,
2516                           gulong   handler_id)
2517 {
2518   Handler *handler;
2519   
2520   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2521   g_return_if_fail (handler_id > 0);
2522   
2523   SIGNAL_LOCK ();
2524   handler = handler_lookup (instance, handler_id, NULL, NULL);
2525   if (handler)
2526     {
2527       if (handler->block_count)
2528         handler->block_count -= 1;
2529       else
2530         g_warning (G_STRLOC ": handler '%lu' of instance '%p' is not blocked", handler_id, instance);
2531     }
2532   else
2533     g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2534   SIGNAL_UNLOCK ();
2535 }
2536
2537 /**
2538  * g_signal_handler_disconnect:
2539  * @instance: (type GObject.Object): The instance to remove the signal handler from.
2540  * @handler_id: Handler id of the handler to be disconnected.
2541  *
2542  * Disconnects a handler from an instance so it will not be called during
2543  * any future or currently ongoing emissions of the signal it has been
2544  * connected to. The @handler_id becomes invalid and may be reused.
2545  *
2546  * The @handler_id has to be a valid signal handler id, connected to a
2547  * signal of @instance.
2548  */
2549 void
2550 g_signal_handler_disconnect (gpointer instance,
2551                              gulong   handler_id)
2552 {
2553   Handler *handler;
2554   guint signal_id;
2555   
2556   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2557   g_return_if_fail (handler_id > 0);
2558   
2559   SIGNAL_LOCK ();
2560   handler = handler_lookup (instance, handler_id, NULL, &signal_id);
2561   if (handler)
2562     {
2563       handler->sequential_number = 0;
2564       handler->block_count = 1;
2565       remove_invalid_closure_notify (handler, instance);
2566       handler_unref_R (signal_id, instance, handler);
2567     }
2568   else
2569     g_warning ("%s: instance '%p' has no handler with id '%lu'", G_STRLOC, instance, handler_id);
2570   SIGNAL_UNLOCK ();
2571 }
2572
2573 /**
2574  * g_signal_handler_is_connected:
2575  * @instance: (type GObject.Object): The instance where a signal handler is sought.
2576  * @handler_id: the handler id.
2577  *
2578  * Returns whether @handler_id is the id of a handler connected to @instance.
2579  *
2580  * Returns: whether @handler_id identifies a handler connected to @instance.
2581  */
2582 gboolean
2583 g_signal_handler_is_connected (gpointer instance,
2584                                gulong   handler_id)
2585 {
2586   Handler *handler;
2587   gboolean connected;
2588
2589   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2590
2591   SIGNAL_LOCK ();
2592   handler = handler_lookup (instance, handler_id, NULL, NULL);
2593   connected = handler != NULL;
2594   SIGNAL_UNLOCK ();
2595
2596   return connected;
2597 }
2598
2599 /**
2600  * g_signal_handlers_destroy:
2601  * @instance: (type GObject.Object): The instance whose signal handlers are destroyed
2602  *
2603  * Destroy all signal handlers of a type instance. This function is
2604  * an implementation detail of the #GObject dispose implementation,
2605  * and should not be used outside of the type system.
2606  */
2607 void
2608 g_signal_handlers_destroy (gpointer instance)
2609 {
2610   GBSearchArray *hlbsa;
2611   
2612   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2613   
2614   SIGNAL_LOCK ();
2615   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2616   if (hlbsa)
2617     {
2618       guint i;
2619       
2620       /* reentrancy caution, delete instance trace first */
2621       g_hash_table_remove (g_handler_list_bsa_ht, instance);
2622       
2623       for (i = 0; i < hlbsa->n_nodes; i++)
2624         {
2625           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2626           Handler *handler = hlist->handlers;
2627           
2628           while (handler)
2629             {
2630               Handler *tmp = handler;
2631               
2632               handler = tmp->next;
2633               tmp->block_count = 1;
2634               /* cruel unlink, this works because _all_ handlers vanish */
2635               tmp->next = NULL;
2636               tmp->prev = tmp;
2637               if (tmp->sequential_number)
2638                 {
2639                   remove_invalid_closure_notify (tmp, instance);
2640                   tmp->sequential_number = 0;
2641                   handler_unref_R (0, NULL, tmp);
2642                 }
2643             }
2644         }
2645       g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2646     }
2647   SIGNAL_UNLOCK ();
2648 }
2649
2650 /**
2651  * g_signal_handler_find:
2652  * @instance: (type GObject.Object): The instance owning the signal handler to be found.
2653  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2654  *  and/or @data the handler has to match.
2655  * @signal_id: Signal the handler has to be connected to.
2656  * @detail: Signal detail the handler has to be connected to.
2657  * @closure: (allow-none): The closure the handler will invoke.
2658  * @func: The C closure callback of the handler (useless for non-C closures).
2659  * @data: The closure data of the handler's closure.
2660  *
2661  * Finds the first signal handler that matches certain selection criteria.
2662  * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2663  * flags, and the criteria values are passed as arguments.
2664  * The match @mask has to be non-0 for successful matches.
2665  * If no handler was found, 0 is returned.
2666  *
2667  * Returns: A valid non-0 signal handler id for a successful match.
2668  */
2669 gulong
2670 g_signal_handler_find (gpointer         instance,
2671                        GSignalMatchType mask,
2672                        guint            signal_id,
2673                        GQuark           detail,
2674                        GClosure        *closure,
2675                        gpointer         func,
2676                        gpointer         data)
2677 {
2678   gulong handler_seq_no = 0;
2679   
2680   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2681   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2682   
2683   if (mask & G_SIGNAL_MATCH_MASK)
2684     {
2685       HandlerMatch *mlist;
2686       
2687       SIGNAL_LOCK ();
2688       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2689       if (mlist)
2690         {
2691           handler_seq_no = mlist->handler->sequential_number;
2692           handler_match_free1_R (mlist, instance);
2693         }
2694       SIGNAL_UNLOCK ();
2695     }
2696   
2697   return handler_seq_no;
2698 }
2699
2700 static guint
2701 signal_handlers_foreach_matched_R (gpointer         instance,
2702                                    GSignalMatchType mask,
2703                                    guint            signal_id,
2704                                    GQuark           detail,
2705                                    GClosure        *closure,
2706                                    gpointer         func,
2707                                    gpointer         data,
2708                                    void           (*callback) (gpointer instance,
2709                                                                gulong   handler_seq_no))
2710 {
2711   HandlerMatch *mlist;
2712   guint n_handlers = 0;
2713   
2714   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2715   while (mlist)
2716     {
2717       n_handlers++;
2718       if (mlist->handler->sequential_number)
2719         {
2720           SIGNAL_UNLOCK ();
2721           callback (instance, mlist->handler->sequential_number);
2722           SIGNAL_LOCK ();
2723         }
2724       mlist = handler_match_free1_R (mlist, instance);
2725     }
2726   
2727   return n_handlers;
2728 }
2729
2730 /**
2731  * g_signal_handlers_block_matched:
2732  * @instance: (type GObject.Object): The instance to block handlers from.
2733  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2734  *  and/or @data the handlers have to match.
2735  * @signal_id: Signal the handlers have to be connected to.
2736  * @detail: Signal detail the handlers have to be connected to.
2737  * @closure: (allow-none): The closure the handlers will invoke.
2738  * @func: The C closure callback of the handlers (useless for non-C closures).
2739  * @data: The closure data of the handlers' closures.
2740  *
2741  * Blocks all handlers on an instance that match a certain selection criteria.
2742  * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2743  * flags, and the criteria values are passed as arguments.
2744  * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2745  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2746  * If no handlers were found, 0 is returned, the number of blocked handlers
2747  * otherwise.
2748  *
2749  * Returns: The number of handlers that matched.
2750  */
2751 guint
2752 g_signal_handlers_block_matched (gpointer         instance,
2753                                  GSignalMatchType mask,
2754                                  guint            signal_id,
2755                                  GQuark           detail,
2756                                  GClosure        *closure,
2757                                  gpointer         func,
2758                                  gpointer         data)
2759 {
2760   guint n_handlers = 0;
2761   
2762   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2763   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2764   
2765   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2766     {
2767       SIGNAL_LOCK ();
2768       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2769                                                       closure, func, data,
2770                                                       g_signal_handler_block);
2771       SIGNAL_UNLOCK ();
2772     }
2773   
2774   return n_handlers;
2775 }
2776
2777 /**
2778  * g_signal_handlers_unblock_matched:
2779  * @instance: (type GObject.Object): The instance to unblock handlers from.
2780  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2781  *  and/or @data the handlers have to match.
2782  * @signal_id: Signal the handlers have to be connected to.
2783  * @detail: Signal detail the handlers have to be connected to.
2784  * @closure: (allow-none): The closure the handlers will invoke.
2785  * @func: The C closure callback of the handlers (useless for non-C closures).
2786  * @data: The closure data of the handlers' closures.
2787  *
2788  * Unblocks all handlers on an instance that match a certain selection
2789  * criteria. The criteria mask is passed as an OR-ed combination of
2790  * #GSignalMatchType flags, and the criteria values are passed as arguments.
2791  * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2792  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2793  * If no handlers were found, 0 is returned, the number of unblocked handlers
2794  * otherwise. The match criteria should not apply to any handlers that are
2795  * not currently blocked.
2796  *
2797  * Returns: The number of handlers that matched.
2798  */
2799 guint
2800 g_signal_handlers_unblock_matched (gpointer         instance,
2801                                    GSignalMatchType mask,
2802                                    guint            signal_id,
2803                                    GQuark           detail,
2804                                    GClosure        *closure,
2805                                    gpointer         func,
2806                                    gpointer         data)
2807 {
2808   guint n_handlers = 0;
2809   
2810   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2811   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2812   
2813   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2814     {
2815       SIGNAL_LOCK ();
2816       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2817                                                       closure, func, data,
2818                                                       g_signal_handler_unblock);
2819       SIGNAL_UNLOCK ();
2820     }
2821   
2822   return n_handlers;
2823 }
2824
2825 /**
2826  * g_signal_handlers_disconnect_matched:
2827  * @instance: (type GObject.Object): The instance to remove handlers from.
2828  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2829  *  and/or @data the handlers have to match.
2830  * @signal_id: Signal the handlers have to be connected to.
2831  * @detail: Signal detail the handlers have to be connected to.
2832  * @closure: (allow-none): The closure the handlers will invoke.
2833  * @func: The C closure callback of the handlers (useless for non-C closures).
2834  * @data: The closure data of the handlers' closures.
2835  *
2836  * Disconnects all handlers on an instance that match a certain
2837  * selection criteria. The criteria mask is passed as an OR-ed
2838  * combination of #GSignalMatchType flags, and the criteria values are
2839  * passed as arguments.  Passing at least one of the
2840  * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2841  * %G_SIGNAL_MATCH_DATA match flags is required for successful
2842  * matches.  If no handlers were found, 0 is returned, the number of
2843  * disconnected handlers otherwise.
2844  *
2845  * Returns: The number of handlers that matched.
2846  */
2847 guint
2848 g_signal_handlers_disconnect_matched (gpointer         instance,
2849                                       GSignalMatchType mask,
2850                                       guint            signal_id,
2851                                       GQuark           detail,
2852                                       GClosure        *closure,
2853                                       gpointer         func,
2854                                       gpointer         data)
2855 {
2856   guint n_handlers = 0;
2857   
2858   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2859   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2860   
2861   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2862     {
2863       SIGNAL_LOCK ();
2864       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2865                                                       closure, func, data,
2866                                                       g_signal_handler_disconnect);
2867       SIGNAL_UNLOCK ();
2868     }
2869   
2870   return n_handlers;
2871 }
2872
2873 /**
2874  * g_signal_has_handler_pending:
2875  * @instance: (type GObject.Object): the object whose signal handlers are sought.
2876  * @signal_id: the signal id.
2877  * @detail: the detail.
2878  * @may_be_blocked: whether blocked handlers should count as match.
2879  *
2880  * Returns whether there are any handlers connected to @instance for the
2881  * given signal id and detail.
2882  *
2883  * If @detail is 0 then it will only match handlers that were connected
2884  * without detail.  If @detail is non-zero then it will match handlers
2885  * connected both without detail and with the given detail.  This is
2886  * consistent with how a signal emitted with @detail would be delivered
2887  * to those handlers.
2888  *
2889  * One example of when you might use this is when the arguments to the
2890  * signal are difficult to compute. A class implementor may opt to not
2891  * emit the signal if no one is attached anyway, thus saving the cost
2892  * of building the arguments.
2893  *
2894  * Returns: %TRUE if a handler is connected to the signal, %FALSE
2895  *          otherwise.
2896  */
2897 gboolean
2898 g_signal_has_handler_pending (gpointer instance,
2899                               guint    signal_id,
2900                               GQuark   detail,
2901                               gboolean may_be_blocked)
2902 {
2903   HandlerMatch *mlist;
2904   gboolean has_pending;
2905   
2906   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2907   g_return_val_if_fail (signal_id > 0, FALSE);
2908   
2909   SIGNAL_LOCK ();
2910   if (detail)
2911     {
2912       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2913       
2914       if (!(node->flags & G_SIGNAL_DETAILED))
2915         {
2916           g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2917           SIGNAL_UNLOCK ();
2918           return FALSE;
2919         }
2920     }
2921   mlist = handlers_find (instance,
2922                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2923                          signal_id, detail, NULL, NULL, NULL, TRUE);
2924   if (mlist)
2925     {
2926       has_pending = TRUE;
2927       handler_match_free1_R (mlist, instance);
2928     }
2929   else
2930     has_pending = FALSE;
2931   SIGNAL_UNLOCK ();
2932   
2933   return has_pending;
2934 }
2935
2936 /**
2937  * g_signal_emitv:
2938  * @instance_and_params: (array): argument list for the signal emission.
2939  *  The first element in the array is a #GValue for the instance the signal
2940  *  is being emitted on. The rest are any arguments to be passed to the signal.
2941  * @signal_id: the signal id
2942  * @detail: the detail
2943  * @return_value: Location to store the return value of the signal emission.
2944  *
2945  * Emits a signal.
2946  *
2947  * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2948  * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2949  */
2950 void
2951 g_signal_emitv (const GValue *instance_and_params,
2952                 guint         signal_id,
2953                 GQuark        detail,
2954                 GValue       *return_value)
2955 {
2956   gpointer instance;
2957   SignalNode *node;
2958 #ifdef G_ENABLE_DEBUG
2959   const GValue *param_values;
2960   guint i;
2961 #endif
2962   
2963   g_return_if_fail (instance_and_params != NULL);
2964   instance = g_value_peek_pointer (instance_and_params);
2965   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2966   g_return_if_fail (signal_id > 0);
2967
2968 #ifdef G_ENABLE_DEBUG
2969   param_values = instance_and_params + 1;
2970 #endif
2971
2972   SIGNAL_LOCK ();
2973   node = LOOKUP_SIGNAL_NODE (signal_id);
2974   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2975     {
2976       g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
2977       SIGNAL_UNLOCK ();
2978       return;
2979     }
2980 #ifdef G_ENABLE_DEBUG
2981   if (detail && !(node->flags & G_SIGNAL_DETAILED))
2982     {
2983       g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2984       SIGNAL_UNLOCK ();
2985       return;
2986     }
2987   for (i = 0; i < node->n_params; i++)
2988     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2989       {
2990         g_critical ("%s: value for '%s' parameter %u for signal \"%s\" is of type '%s'",
2991                     G_STRLOC,
2992                     type_debug_name (node->param_types[i]),
2993                     i,
2994                     node->name,
2995                     G_VALUE_TYPE_NAME (param_values + i));
2996         SIGNAL_UNLOCK ();
2997         return;
2998       }
2999   if (node->return_type != G_TYPE_NONE)
3000     {
3001       if (!return_value)
3002         {
3003           g_critical ("%s: return value '%s' for signal \"%s\" is (NULL)",
3004                       G_STRLOC,
3005                       type_debug_name (node->return_type),
3006                       node->name);
3007           SIGNAL_UNLOCK ();
3008           return;
3009         }
3010       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
3011         {
3012           g_critical ("%s: return value '%s' for signal \"%s\" is of type '%s'",
3013                       G_STRLOC,
3014                       type_debug_name (node->return_type),
3015                       node->name,
3016                       G_VALUE_TYPE_NAME (return_value));
3017           SIGNAL_UNLOCK ();
3018           return;
3019         }
3020     }
3021   else
3022     return_value = NULL;
3023 #endif  /* G_ENABLE_DEBUG */
3024
3025   /* optimize NOP emissions */
3026   if (!node->single_va_closure_is_valid)
3027     node_update_single_va_closure (node);
3028
3029   if (node->single_va_closure != NULL &&
3030       (node->single_va_closure == SINGLE_VA_CLOSURE_EMPTY_MAGIC ||
3031        _g_closure_is_void (node->single_va_closure, instance)))
3032     {
3033       HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3034       if (hlist == NULL || hlist->handlers == NULL)
3035         {
3036           /* nothing to do to emit this signal */
3037           SIGNAL_UNLOCK ();
3038           /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
3039           return;
3040         }
3041     }
3042
3043   SIGNAL_UNLOCK ();
3044   signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
3045 }
3046
3047 static inline gboolean
3048 accumulate (GSignalInvocationHint *ihint,
3049             GValue                *return_accu,
3050             GValue                *handler_return,
3051             SignalAccumulator     *accumulator)
3052 {
3053   gboolean continue_emission;
3054
3055   if (!accumulator)
3056     return TRUE;
3057
3058   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3059   g_value_reset (handler_return);
3060
3061   return continue_emission;
3062 }
3063
3064 /**
3065  * g_signal_emit_valist: (skip)
3066  * @instance: the instance the signal is being emitted on.
3067  * @signal_id: the signal id
3068  * @detail: the detail
3069  * @var_args: a list of parameters to be passed to the signal, followed by a
3070  *  location for the return value. If the return type of the signal
3071  *  is #G_TYPE_NONE, the return value location can be omitted.
3072  *
3073  * Emits a signal.
3074  *
3075  * Note that g_signal_emit_valist() resets the return value to the default
3076  * if no handlers are connected, in contrast to g_signal_emitv().
3077  */
3078 void
3079 g_signal_emit_valist (gpointer instance,
3080                       guint    signal_id,
3081                       GQuark   detail,
3082                       va_list  var_args)
3083 {
3084   GValue *instance_and_params;
3085   GType signal_return_type;
3086   GValue *param_values;
3087   SignalNode *node;
3088   guint i, n_params;
3089
3090   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3091   g_return_if_fail (signal_id > 0);
3092
3093   SIGNAL_LOCK ();
3094   node = LOOKUP_SIGNAL_NODE (signal_id);
3095   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
3096     {
3097       g_warning ("%s: signal id '%u' is invalid for instance '%p'", G_STRLOC, signal_id, instance);
3098       SIGNAL_UNLOCK ();
3099       return;
3100     }
3101 #ifndef G_DISABLE_CHECKS
3102   if (detail && !(node->flags & G_SIGNAL_DETAILED))
3103     {
3104       g_warning ("%s: signal id '%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
3105       SIGNAL_UNLOCK ();
3106       return;
3107     }
3108 #endif  /* !G_DISABLE_CHECKS */
3109
3110   if (!node->single_va_closure_is_valid)
3111     node_update_single_va_closure (node);
3112
3113   if (node->single_va_closure != NULL)
3114     {
3115       HandlerList* hlist = handler_list_lookup (node->signal_id, instance);
3116       Handler *fastpath_handler = NULL;
3117       Handler *l;
3118       GClosure *closure = NULL;
3119       gboolean fastpath = TRUE;
3120       GSignalFlags run_type = G_SIGNAL_RUN_FIRST;
3121
3122       if (node->single_va_closure != SINGLE_VA_CLOSURE_EMPTY_MAGIC &&
3123           !_g_closure_is_void (node->single_va_closure, instance))
3124         {
3125           if (_g_closure_supports_invoke_va (node->single_va_closure))
3126             {
3127               closure = node->single_va_closure;
3128               if (node->single_va_closure_is_after)
3129                 run_type = G_SIGNAL_RUN_LAST;
3130               else
3131                 run_type = G_SIGNAL_RUN_FIRST;
3132             }
3133           else
3134             fastpath = FALSE;
3135         }
3136
3137       for (l = hlist ? hlist->handlers : NULL; fastpath && l != NULL; l = l->next)
3138         {
3139           if (!l->block_count &&
3140               (!l->detail || l->detail == detail))
3141             {
3142               if (closure != NULL || !_g_closure_supports_invoke_va (l->closure))
3143                 {
3144                   fastpath = FALSE;
3145                   break;
3146                 }
3147               else
3148                 {
3149                   fastpath_handler = l;
3150                   closure = l->closure;
3151                   if (l->after)
3152                     run_type = G_SIGNAL_RUN_LAST;
3153                   else
3154                     run_type = G_SIGNAL_RUN_FIRST;
3155                 }
3156             }
3157         }
3158
3159       if (fastpath && closure == NULL && node->return_type == G_TYPE_NONE)
3160         {
3161           SIGNAL_UNLOCK ();
3162           return;
3163         }
3164
3165       /* Don't allow no-recurse emission as we might have to restart, which means
3166          we will run multiple handlers and thus must ref all arguments */
3167       if (closure != NULL && (node->flags & (G_SIGNAL_NO_RECURSE)) != 0)
3168         fastpath = FALSE;
3169       
3170       if (fastpath)
3171         {
3172           SignalAccumulator *accumulator;
3173           Emission emission;
3174           GValue *return_accu, accu = G_VALUE_INIT;
3175           guint signal_id;
3176           GType instance_type = G_TYPE_FROM_INSTANCE (instance);
3177           GValue emission_return = G_VALUE_INIT;
3178           GType rtype = node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3179           gboolean static_scope = node->return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3180
3181           signal_id = node->signal_id;
3182           accumulator = node->accumulator;
3183           if (rtype == G_TYPE_NONE)
3184             return_accu = NULL;
3185           else if (accumulator)
3186             return_accu = &accu;
3187           else
3188             return_accu = &emission_return;
3189
3190           emission.instance = instance;
3191           emission.ihint.signal_id = signal_id;
3192           emission.ihint.detail = detail;
3193           emission.ihint.run_type = run_type;
3194           emission.state = EMISSION_RUN;
3195           emission.chain_type = instance_type;
3196           emission_push (&emission);
3197
3198           if (fastpath_handler)
3199             handler_ref (fastpath_handler);
3200
3201           SIGNAL_UNLOCK ();
3202
3203           TRACE(GOBJECT_SIGNAL_EMIT(signal_id, detail, instance, instance_type));
3204
3205           if (rtype != G_TYPE_NONE)
3206             g_value_init (&emission_return, rtype);
3207
3208           if (accumulator)
3209             g_value_init (&accu, rtype);
3210
3211           if (closure != NULL)
3212             {
3213               g_object_ref (instance);
3214               _g_closure_invoke_va (closure,
3215                                     return_accu,
3216                                     instance,
3217                                     var_args,
3218                                     node->n_params,
3219                                     node->param_types);
3220               accumulate (&emission.ihint, &emission_return, &accu, accumulator);
3221             }
3222
3223           SIGNAL_LOCK ();
3224
3225           emission.chain_type = G_TYPE_NONE;
3226           emission_pop (&emission);
3227
3228           if (fastpath_handler)
3229             handler_unref_R (signal_id, instance, fastpath_handler);
3230
3231           SIGNAL_UNLOCK ();
3232
3233           if (accumulator)
3234             g_value_unset (&accu);
3235
3236           if (rtype != G_TYPE_NONE)
3237             {
3238               gchar *error = NULL;
3239               for (i = 0; i < node->n_params; i++)
3240                 {
3241                   GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3242                   G_VALUE_COLLECT_SKIP (ptype, var_args);
3243                 }
3244
3245               G_VALUE_LCOPY (&emission_return,
3246                              var_args,
3247                              static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3248                              &error);
3249               if (!error)
3250                 g_value_unset (&emission_return);
3251               else
3252                 {
3253                   g_warning ("%s: %s", G_STRLOC, error);
3254                   g_free (error);
3255                   /* we purposely leak the value here, it might not be
3256                    * in a sane state if an error condition occurred
3257                    */
3258                 }
3259             }
3260           
3261           TRACE(GOBJECT_SIGNAL_EMIT_END(signal_id, detail, instance, instance_type));
3262
3263           if (closure != NULL)
3264             g_object_unref (instance);
3265
3266           return;
3267         }
3268     }
3269   SIGNAL_UNLOCK ();
3270
3271   n_params = node->n_params;
3272   signal_return_type = node->return_type;
3273   instance_and_params = g_alloca (sizeof (GValue) * (n_params + 1));
3274   memset (instance_and_params, 0, sizeof (GValue) * (n_params + 1));
3275   param_values = instance_and_params + 1;
3276
3277   for (i = 0; i < node->n_params; i++)
3278     {
3279       gchar *error;
3280       GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3281       gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
3282
3283       G_VALUE_COLLECT_INIT (param_values + i, ptype,
3284                             var_args,
3285                             static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3286                             &error);
3287       if (error)
3288         {
3289           g_warning ("%s: %s", G_STRLOC, error);
3290           g_free (error);
3291
3292           /* we purposely leak the value here, it might not be
3293            * in a sane state if an error condition occoured
3294            */
3295           while (i--)
3296             g_value_unset (param_values + i);
3297
3298           return;
3299         }
3300     }
3301
3302   instance_and_params->g_type = 0;
3303   g_value_init_from_instance (instance_and_params, instance);
3304   if (signal_return_type == G_TYPE_NONE)
3305     signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
3306   else
3307     {
3308       GValue return_value = G_VALUE_INIT;
3309       gchar *error = NULL;
3310       GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
3311       gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
3312       
3313       g_value_init (&return_value, rtype);
3314
3315       signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
3316
3317       G_VALUE_LCOPY (&return_value,
3318                      var_args,
3319                      static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3320                      &error);
3321       if (!error)
3322         g_value_unset (&return_value);
3323       else
3324         {
3325           g_warning ("%s: %s", G_STRLOC, error);
3326           g_free (error);
3327           
3328           /* we purposely leak the value here, it might not be
3329            * in a sane state if an error condition occurred
3330            */
3331         }
3332     }
3333   for (i = 0; i < n_params; i++)
3334     g_value_unset (param_values + i);
3335   g_value_unset (instance_and_params);
3336 }
3337
3338 /**
3339  * g_signal_emit:
3340  * @instance: (type GObject.Object): the instance the signal is being emitted on.
3341  * @signal_id: the signal id
3342  * @detail: the detail
3343  * @...: parameters to be passed to the signal, followed by a
3344  *  location for the return value. If the return type of the signal
3345  *  is #G_TYPE_NONE, the return value location can be omitted.
3346  *
3347  * Emits a signal.
3348  *
3349  * Note that g_signal_emit() resets the return value to the default
3350  * if no handlers are connected, in contrast to g_signal_emitv().
3351  */
3352 void
3353 g_signal_emit (gpointer instance,
3354                guint    signal_id,
3355                GQuark   detail,
3356                ...)
3357 {
3358   va_list var_args;
3359
3360   va_start (var_args, detail);
3361   g_signal_emit_valist (instance, signal_id, detail, var_args);
3362   va_end (var_args);
3363 }
3364
3365 /**
3366  * g_signal_emit_by_name:
3367  * @instance: (type GObject.Object): the instance the signal is being emitted on.
3368  * @detailed_signal: a string of the form "signal-name::detail".
3369  * @...: parameters to be passed to the signal, followed by a
3370  *  location for the return value. If the return type of the signal
3371  *  is #G_TYPE_NONE, the return value location can be omitted.
3372  *
3373  * Emits a signal.
3374  *
3375  * Note that g_signal_emit_by_name() resets the return value to the default
3376  * if no handlers are connected, in contrast to g_signal_emitv().
3377  */
3378 void
3379 g_signal_emit_by_name (gpointer     instance,
3380                        const gchar *detailed_signal,
3381                        ...)
3382 {
3383   GQuark detail = 0;
3384   guint signal_id;
3385   GType itype;
3386
3387   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3388   g_return_if_fail (detailed_signal != NULL);
3389
3390   itype = G_TYPE_FROM_INSTANCE (instance);
3391
3392   SIGNAL_LOCK ();
3393   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
3394   SIGNAL_UNLOCK ();
3395
3396   if (signal_id)
3397     {
3398       va_list var_args;
3399
3400       va_start (var_args, detailed_signal);
3401       g_signal_emit_valist (instance, signal_id, detail, var_args);
3402       va_end (var_args);
3403     }
3404   else
3405     g_warning ("%s: signal name '%s' is invalid for instance '%p' of type '%s'",
3406                G_STRLOC, detailed_signal, instance, g_type_name (itype));
3407 }
3408
3409 static gboolean
3410 signal_emit_unlocked_R (SignalNode   *node,
3411                         GQuark        detail,
3412                         gpointer      instance,
3413                         GValue       *emission_return,
3414                         const GValue *instance_and_params)
3415 {
3416   SignalAccumulator *accumulator;
3417   Emission emission;
3418   GClosure *class_closure;
3419   HandlerList *hlist;
3420   Handler *handler_list = NULL;
3421   GValue *return_accu, accu = G_VALUE_INIT;
3422   guint signal_id;
3423   gulong max_sequential_handler_number;
3424   gboolean return_value_altered = FALSE;
3425   
3426   TRACE(GOBJECT_SIGNAL_EMIT(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3427
3428   SIGNAL_LOCK ();
3429   signal_id = node->signal_id;
3430
3431   if (node->flags & G_SIGNAL_NO_RECURSE)
3432     {
3433       Emission *node = emission_find (signal_id, detail, instance);
3434       
3435       if (node)
3436         {
3437           node->state = EMISSION_RESTART;
3438           SIGNAL_UNLOCK ();
3439           return return_value_altered;
3440         }
3441     }
3442   accumulator = node->accumulator;
3443   if (accumulator)
3444     {
3445       SIGNAL_UNLOCK ();
3446       g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3447       return_accu = &accu;
3448       SIGNAL_LOCK ();
3449     }
3450   else
3451     return_accu = emission_return;
3452   emission.instance = instance;
3453   emission.ihint.signal_id = node->signal_id;
3454   emission.ihint.detail = detail;
3455   emission.ihint.run_type = 0;
3456   emission.state = 0;
3457   emission.chain_type = G_TYPE_NONE;
3458   emission_push (&emission);
3459   class_closure = signal_lookup_closure (node, instance);
3460   
3461  EMIT_RESTART:
3462   
3463   if (handler_list)
3464     handler_unref_R (signal_id, instance, handler_list);
3465   max_sequential_handler_number = g_handler_sequential_number;
3466   hlist = handler_list_lookup (signal_id, instance);
3467   handler_list = hlist ? hlist->handlers : NULL;
3468   if (handler_list)
3469     handler_ref (handler_list);
3470   
3471   emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3472   
3473   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3474     {
3475       emission.state = EMISSION_RUN;
3476
3477       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3478       SIGNAL_UNLOCK ();
3479       g_closure_invoke (class_closure,
3480                         return_accu,
3481                         node->n_params + 1,
3482                         instance_and_params,
3483                         &emission.ihint);
3484       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3485           emission.state == EMISSION_RUN)
3486         emission.state = EMISSION_STOP;
3487       SIGNAL_LOCK ();
3488       emission.chain_type = G_TYPE_NONE;
3489       return_value_altered = TRUE;
3490       
3491       if (emission.state == EMISSION_STOP)
3492         goto EMIT_CLEANUP;
3493       else if (emission.state == EMISSION_RESTART)
3494         goto EMIT_RESTART;
3495     }
3496   
3497   if (node->emission_hooks)
3498     {
3499       gboolean need_destroy, was_in_call, may_recurse = TRUE;
3500       GHook *hook;
3501
3502       emission.state = EMISSION_HOOK;
3503       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3504       while (hook)
3505         {
3506           SignalHook *signal_hook = SIGNAL_HOOK (hook);
3507           
3508           if (!signal_hook->detail || signal_hook->detail == detail)
3509             {
3510               GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3511               
3512               was_in_call = G_HOOK_IN_CALL (hook);
3513               hook->flags |= G_HOOK_FLAG_IN_CALL;
3514               SIGNAL_UNLOCK ();
3515               need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3516               SIGNAL_LOCK ();
3517               if (!was_in_call)
3518                 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3519               if (need_destroy)
3520                 g_hook_destroy_link (node->emission_hooks, hook);
3521             }
3522           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3523         }
3524       
3525       if (emission.state == EMISSION_RESTART)
3526         goto EMIT_RESTART;
3527     }
3528   
3529   if (handler_list)
3530     {
3531       Handler *handler = handler_list;
3532       
3533       emission.state = EMISSION_RUN;
3534       handler_ref (handler);
3535       do
3536         {
3537           Handler *tmp;
3538           
3539           if (handler->after)
3540             {
3541               handler_unref_R (signal_id, instance, handler_list);
3542               handler_list = handler;
3543               break;
3544             }
3545           else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3546                    handler->sequential_number < max_sequential_handler_number)
3547             {
3548               SIGNAL_UNLOCK ();
3549               g_closure_invoke (handler->closure,
3550                                 return_accu,
3551                                 node->n_params + 1,
3552                                 instance_and_params,
3553                                 &emission.ihint);
3554               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3555                   emission.state == EMISSION_RUN)
3556                 emission.state = EMISSION_STOP;
3557               SIGNAL_LOCK ();
3558               return_value_altered = TRUE;
3559               
3560               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3561             }
3562           else
3563             tmp = handler->next;
3564           
3565           if (tmp)
3566             handler_ref (tmp);
3567           handler_unref_R (signal_id, instance, handler_list);
3568           handler_list = handler;
3569           handler = tmp;
3570         }
3571       while (handler);
3572       
3573       if (emission.state == EMISSION_STOP)
3574         goto EMIT_CLEANUP;
3575       else if (emission.state == EMISSION_RESTART)
3576         goto EMIT_RESTART;
3577     }
3578   
3579   emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3580   
3581   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3582     {
3583       emission.state = EMISSION_RUN;
3584       
3585       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3586       SIGNAL_UNLOCK ();
3587       g_closure_invoke (class_closure,
3588                         return_accu,
3589                         node->n_params + 1,
3590                         instance_and_params,
3591                         &emission.ihint);
3592       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3593           emission.state == EMISSION_RUN)
3594         emission.state = EMISSION_STOP;
3595       SIGNAL_LOCK ();
3596       emission.chain_type = G_TYPE_NONE;
3597       return_value_altered = TRUE;
3598       
3599       if (emission.state == EMISSION_STOP)
3600         goto EMIT_CLEANUP;
3601       else if (emission.state == EMISSION_RESTART)
3602         goto EMIT_RESTART;
3603     }
3604   
3605   if (handler_list)
3606     {
3607       Handler *handler = handler_list;
3608       
3609       emission.state = EMISSION_RUN;
3610       handler_ref (handler);
3611       do
3612         {
3613           Handler *tmp;
3614           
3615           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3616               handler->sequential_number < max_sequential_handler_number)
3617             {
3618               SIGNAL_UNLOCK ();
3619               g_closure_invoke (handler->closure,
3620                                 return_accu,
3621                                 node->n_params + 1,
3622                                 instance_and_params,
3623                                 &emission.ihint);
3624               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3625                   emission.state == EMISSION_RUN)
3626                 emission.state = EMISSION_STOP;
3627               SIGNAL_LOCK ();
3628               return_value_altered = TRUE;
3629               
3630               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3631             }
3632           else
3633             tmp = handler->next;
3634           
3635           if (tmp)
3636             handler_ref (tmp);
3637           handler_unref_R (signal_id, instance, handler);
3638           handler = tmp;
3639         }
3640       while (handler);
3641       
3642       if (emission.state == EMISSION_STOP)
3643         goto EMIT_CLEANUP;
3644       else if (emission.state == EMISSION_RESTART)
3645         goto EMIT_RESTART;
3646     }
3647   
3648  EMIT_CLEANUP:
3649   
3650   emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3651   
3652   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3653     {
3654       gboolean need_unset = FALSE;
3655       
3656       emission.state = EMISSION_STOP;
3657       
3658       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3659       SIGNAL_UNLOCK ();
3660       if (node->return_type != G_TYPE_NONE && !accumulator)
3661         {
3662           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3663           need_unset = TRUE;
3664         }
3665       g_closure_invoke (class_closure,
3666                         node->return_type != G_TYPE_NONE ? &accu : NULL,
3667                         node->n_params + 1,
3668                         instance_and_params,
3669                         &emission.ihint);
3670       if (need_unset)
3671         g_value_unset (&accu);
3672       SIGNAL_LOCK ();
3673       emission.chain_type = G_TYPE_NONE;
3674       
3675       if (emission.state == EMISSION_RESTART)
3676         goto EMIT_RESTART;
3677     }
3678   
3679   if (handler_list)
3680     handler_unref_R (signal_id, instance, handler_list);
3681   
3682   emission_pop (&emission);
3683   SIGNAL_UNLOCK ();
3684   if (accumulator)
3685     g_value_unset (&accu);
3686
3687   TRACE(GOBJECT_SIGNAL_EMIT_END(node->signal_id, detail, instance, G_TYPE_FROM_INSTANCE (instance)));
3688
3689   return return_value_altered;
3690 }
3691
3692 static void
3693 add_invalid_closure_notify (Handler  *handler,
3694                             gpointer  instance)
3695 {
3696   g_closure_add_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3697   handler->has_invalid_closure_notify = 1;
3698 }
3699
3700 static void
3701 remove_invalid_closure_notify (Handler  *handler,
3702                                gpointer  instance)
3703 {
3704   if (handler->has_invalid_closure_notify)
3705     {
3706       g_closure_remove_invalidate_notifier (handler->closure, instance, invalid_closure_notify);
3707       handler->has_invalid_closure_notify = 0;
3708     }
3709 }
3710
3711 static void
3712 invalid_closure_notify (gpointer  instance,
3713                         GClosure *closure)
3714 {
3715   Handler *handler;
3716   guint signal_id;
3717
3718   SIGNAL_LOCK ();
3719
3720   handler = handler_lookup (instance, 0, closure, &signal_id);
3721   g_assert (handler->closure == closure);
3722
3723   handler->sequential_number = 0;
3724   handler->block_count = 1;
3725   handler_unref_R (signal_id, instance, handler);
3726
3727   SIGNAL_UNLOCK ();
3728 }
3729
3730 static const gchar*
3731 type_debug_name (GType type)
3732 {
3733   if (type)
3734     {
3735       const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3736       return name ? name : "<unknown>";
3737     }
3738   else
3739     return "<invalid>";
3740 }
3741
3742 /**
3743  * g_signal_accumulator_true_handled:
3744  * @ihint: standard #GSignalAccumulator parameter
3745  * @return_accu: standard #GSignalAccumulator parameter
3746  * @handler_return: standard #GSignalAccumulator parameter
3747  * @dummy: standard #GSignalAccumulator parameter
3748  *
3749  * A predefined #GSignalAccumulator for signals that return a
3750  * boolean values. The behavior that this accumulator gives is
3751  * that a return of %TRUE stops the signal emission: no further
3752  * callbacks will be invoked, while a return of %FALSE allows
3753  * the emission to continue. The idea here is that a %TRUE return
3754  * indicates that the callback handled the signal, and no further
3755  * handling is needed.
3756  *
3757  * Since: 2.4
3758  *
3759  * Returns: standard #GSignalAccumulator result
3760  */
3761 gboolean
3762 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3763                                    GValue                *return_accu,
3764                                    const GValue          *handler_return,
3765                                    gpointer               dummy)
3766 {
3767   gboolean continue_emission;
3768   gboolean signal_handled;
3769   
3770   signal_handled = g_value_get_boolean (handler_return);
3771   g_value_set_boolean (return_accu, signal_handled);
3772   continue_emission = !signal_handled;
3773   
3774   return continue_emission;
3775 }
3776
3777 /**
3778  * g_signal_accumulator_first_wins:
3779  * @ihint: standard #GSignalAccumulator parameter
3780  * @return_accu: standard #GSignalAccumulator parameter
3781  * @handler_return: standard #GSignalAccumulator parameter
3782  * @dummy: standard #GSignalAccumulator parameter
3783  *
3784  * A predefined #GSignalAccumulator for signals intended to be used as a
3785  * hook for application code to provide a particular value.  Usually
3786  * only one such value is desired and multiple handlers for the same
3787  * signal don't make much sense (except for the case of the default
3788  * handler defined in the class structure, in which case you will
3789  * usually want the signal connection to override the class handler).
3790  *
3791  * This accumulator will use the return value from the first signal
3792  * handler that is run as the return value for the signal and not run
3793  * any further handlers (ie: the first handler "wins").
3794  *
3795  * Returns: standard #GSignalAccumulator result
3796  *
3797  * Since: 2.28
3798  **/
3799 gboolean
3800 g_signal_accumulator_first_wins (GSignalInvocationHint *ihint,
3801                                  GValue                *return_accu,
3802                                  const GValue          *handler_return,
3803                                  gpointer               dummy)
3804 {
3805   g_value_copy (handler_return, return_accu);
3806   return FALSE;
3807 }