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