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