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