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