Bug 541208 – Functions to easily install and use signals without class
[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 /**
1356  * g_signal_new_class_handler:
1357  * @signal_name: the name for the signal
1358  * @itype: the type this signal pertains to. It will also pertain to
1359  *  types which are derived from this type.
1360  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1361  *  the default handler is to be invoked. You should at least specify
1362  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1363  * @class_handler: a #GCallback which acts as class implementation of
1364  *  this signal. Used to invoke a class method generically. Pass %NULL to
1365  *  not associate a class method with this signal.
1366  * @accumulator: the accumulator for this signal; may be %NULL.
1367  * @accu_data: user data for the @accumulator.
1368  * @c_marshaller: the function to translate arrays of parameter values to
1369  *  signal emissions into C language callback invocations.
1370  * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1371  *  without a return value.
1372  * @n_params: the number of parameter types to follow.
1373  * @...: a list of types, one for each parameter.
1374  *
1375  * Creates a new signal. (This is usually done in the class initializer.)
1376  *
1377  * This is a variant of g_signal_new() that takes a C callback instead
1378  * off a class offset for the signal's class handler. This function
1379  * doesn't need a function pointer exposed in the class structure of
1380  * an object definition, instead the function pointer is passed
1381  * directly and can be overriden by derived classes with
1382  * g_signal_override_class_closure() or
1383  * g_signal_override_class_handler()and chained to with
1384  * g_signal_chain_from_overridden() or
1385  * g_signal_chain_from_overridden().
1386  *
1387  * See g_signal_new() for information about signal names.
1388  *
1389  * Returns: the signal id
1390  *
1391  * Since: 2.18
1392  */
1393 guint
1394 g_signal_new_class_handler (const gchar        *signal_name,
1395                             GType               itype,
1396                             GSignalFlags        signal_flags,
1397                             GCallback           class_handler,
1398                             GSignalAccumulator  accumulator,
1399                             gpointer            accu_data,
1400                             GSignalCMarshaller  c_marshaller,
1401                             GType               return_type,
1402                             guint               n_params,
1403                             ...)
1404 {
1405   va_list args;
1406   guint signal_id;
1407
1408   g_return_val_if_fail (signal_name != NULL, 0);
1409
1410   va_start (args, n_params);
1411
1412   signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1413                                    class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL,
1414                                    accumulator, accu_data, c_marshaller,
1415                                    return_type, n_params, args);
1416
1417   va_end (args);
1418
1419   return signal_id;
1420 }
1421
1422 static inline ClassClosure*
1423 signal_find_class_closure (SignalNode *node,
1424                            GType       itype)
1425 {
1426   GBSearchArray *bsa = node->class_closure_bsa;
1427   ClassClosure *cc;
1428
1429   if (bsa)
1430     {
1431       ClassClosure key;
1432
1433       /* cc->instance_type is 0 for default closure */
1434       
1435       key.instance_type = itype;
1436       cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1437       while (!cc && key.instance_type)
1438         {
1439           key.instance_type = g_type_parent (key.instance_type);
1440           cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1441         }
1442     }
1443   else
1444     cc = NULL;
1445   return cc;
1446 }
1447
1448 static inline GClosure*
1449 signal_lookup_closure (SignalNode    *node,
1450                        GTypeInstance *instance)
1451 {
1452   ClassClosure *cc;
1453
1454   if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1455     cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1456   else
1457     cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1458   return cc ? cc->closure : NULL;
1459 }
1460
1461 static void
1462 signal_add_class_closure (SignalNode *node,
1463                           GType       itype,
1464                           GClosure   *closure)
1465 {
1466   ClassClosure key;
1467
1468   /* can't optimize NOP emissions with overridden class closures */
1469   node->test_class_offset = 0;
1470
1471   if (!node->class_closure_bsa)
1472     node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1473   key.instance_type = itype;
1474   key.closure = g_closure_ref (closure);
1475   node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1476                                                     &g_class_closure_bconfig,
1477                                                     &key);
1478   g_closure_sink (closure);
1479   if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1480     g_closure_set_marshal (closure, node->c_marshaller);
1481 }
1482
1483 /**
1484  * g_signal_newv:
1485  * @signal_name: the name for the signal
1486  * @itype: the type this signal pertains to. It will also pertain to
1487  *  types which are derived from this type.
1488  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1489  *  the default handler is to be invoked. You should at least specify
1490  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1491  * @class_closure: The closure to invoke on signal emission; may be %NULL.
1492  * @accumulator: the accumulator for this signal; may be %NULL.
1493  * @accu_data: user data for the @accumulator.
1494  * @c_marshaller: the function to translate arrays of parameter values to
1495  *  signal emissions into C language callback invocations.
1496  * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1497  *  without a return value.
1498  * @n_params: the length of @param_types.
1499  * @param_types: an array types, one for each parameter.
1500  *
1501  * Creates a new signal. (This is usually done in the class initializer.)
1502  *
1503  * See g_signal_new() for details on allowed signal names.
1504  *
1505  * Returns: the signal id
1506  */
1507 guint
1508 g_signal_newv (const gchar       *signal_name,
1509                GType              itype,
1510                GSignalFlags       signal_flags,
1511                GClosure          *class_closure,
1512                GSignalAccumulator accumulator,
1513                gpointer           accu_data,
1514                GSignalCMarshaller c_marshaller,
1515                GType              return_type,
1516                guint              n_params,
1517                GType             *param_types)
1518 {
1519   gchar *name;
1520   guint signal_id, i;
1521   SignalNode *node;
1522   
1523   g_return_val_if_fail (signal_name != NULL, 0);
1524   g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1525   if (n_params)
1526     g_return_val_if_fail (param_types != NULL, 0);
1527   g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1528   if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1529     g_return_val_if_fail (accumulator == NULL, 0);
1530   if (!accumulator)
1531     g_return_val_if_fail (accu_data == NULL, 0);
1532
1533   name = g_strdup (signal_name);
1534   g_strdelimit (name, G_STR_DELIMITERS ":^", '_');  /* FIXME do character checks like for types */
1535   
1536   SIGNAL_LOCK ();
1537   
1538   signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1539   node = LOOKUP_SIGNAL_NODE (signal_id);
1540   if (node && !node->destroyed)
1541     {
1542       g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1543                  name,
1544                  type_debug_name (node->itype),
1545                  G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1546       g_free (name);
1547       SIGNAL_UNLOCK ();
1548       return 0;
1549     }
1550   if (node && node->itype != itype)
1551     {
1552       g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1553                  name,
1554                  type_debug_name (itype),
1555                  type_debug_name (node->itype));
1556       g_free (name);
1557       SIGNAL_UNLOCK ();
1558       return 0;
1559     }
1560   for (i = 0; i < n_params; i++)
1561     if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1562       {
1563         g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1564                    i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1565         g_free (name);
1566         SIGNAL_UNLOCK ();
1567         return 0;
1568       }
1569   if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1570     {
1571       g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1572                  type_debug_name (return_type), type_debug_name (itype), name);
1573       g_free (name);
1574       SIGNAL_UNLOCK ();
1575       return 0;
1576     }
1577   if (return_type != G_TYPE_NONE &&
1578       (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1579     {
1580       g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1581                  type_debug_name (itype), name, type_debug_name (return_type));
1582       g_free (name);
1583       SIGNAL_UNLOCK ();
1584       return 0;
1585     }
1586   
1587   /* setup permanent portion of signal node */
1588   if (!node)
1589     {
1590       SignalKey key;
1591       
1592       signal_id = g_n_signal_nodes++;
1593       node = g_new (SignalNode, 1);
1594       node->signal_id = signal_id;
1595       g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1596       g_signal_nodes[signal_id] = node;
1597       node->itype = itype;
1598       node->name = name;
1599       key.itype = itype;
1600       key.quark = g_quark_from_string (node->name);
1601       key.signal_id = signal_id;
1602       g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1603       g_strdelimit (name, "_", '-');
1604       node->name = g_intern_string (name);
1605       key.quark = g_quark_from_string (name);
1606       g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1607     }
1608   node->destroyed = FALSE;
1609   node->test_class_offset = 0;
1610
1611   /* setup reinitializable portion */
1612   node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1613   node->n_params = n_params;
1614   node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1615   node->return_type = return_type;
1616   node->class_closure_bsa = NULL;
1617   if (accumulator)
1618     {
1619       node->accumulator = g_new (SignalAccumulator, 1);
1620       node->accumulator->func = accumulator;
1621       node->accumulator->data = accu_data;
1622     }
1623   else
1624     node->accumulator = NULL;
1625   node->c_marshaller = c_marshaller;
1626   node->emission_hooks = NULL;
1627   if (class_closure)
1628     signal_add_class_closure (node, 0, class_closure);
1629   else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1630     {
1631       /* optimize NOP emissions */
1632       node->test_class_offset = TEST_CLASS_MAGIC;
1633     }
1634   SIGNAL_UNLOCK ();
1635
1636   g_free (name);
1637
1638   return signal_id;
1639 }
1640
1641 /**
1642  * g_signal_new_valist:
1643  * @signal_name: the name for the signal
1644  * @itype: the type this signal pertains to. It will also pertain to
1645  *  types which are derived from this type.
1646  * @signal_flags: a combination of #GSignalFlags specifying detail of when
1647  *  the default handler is to be invoked. You should at least specify
1648  *  %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST.
1649  * @class_closure: The closure to invoke on signal emission; may be %NULL.
1650  * @accumulator: the accumulator for this signal; may be %NULL.
1651  * @accu_data: user data for the @accumulator.
1652  * @c_marshaller: the function to translate arrays of parameter values to
1653  *  signal emissions into C language callback invocations.
1654  * @return_type: the type of return value, or #G_TYPE_NONE for a signal
1655  *  without a return value.
1656  * @n_params: the number of parameter types in @args.
1657  * @args: va_list of #GType, one for each parameter.
1658  *
1659  * Creates a new signal. (This is usually done in the class initializer.)
1660  *
1661  * See g_signal_new() for details on allowed signal names.
1662  *
1663  * Returns: the signal id
1664  */
1665 guint
1666 g_signal_new_valist (const gchar       *signal_name,
1667                      GType              itype,
1668                      GSignalFlags       signal_flags,
1669                      GClosure          *class_closure,
1670                      GSignalAccumulator accumulator,
1671                      gpointer           accu_data,
1672                      GSignalCMarshaller c_marshaller,
1673                      GType              return_type,
1674                      guint              n_params,
1675                      va_list            args)
1676 {
1677   GType *param_types;
1678   guint i;
1679   guint signal_id;
1680
1681   if (n_params > 0)
1682     {
1683       param_types = g_new (GType, n_params);
1684
1685       for (i = 0; i < n_params; i++)
1686         param_types[i] = va_arg (args, GType);
1687     }
1688   else
1689     param_types = NULL;
1690
1691   signal_id = g_signal_newv (signal_name, itype, signal_flags,
1692                              class_closure, accumulator, accu_data, c_marshaller,
1693                              return_type, n_params, param_types);
1694   g_free (param_types);
1695
1696   return signal_id;
1697 }
1698
1699 static void
1700 signal_destroy_R (SignalNode *signal_node)
1701 {
1702   SignalNode node = *signal_node;
1703
1704   signal_node->destroyed = TRUE;
1705   
1706   /* reentrancy caution, zero out real contents first */
1707   signal_node->test_class_offset = 0;
1708   signal_node->n_params = 0;
1709   signal_node->param_types = NULL;
1710   signal_node->return_type = 0;
1711   signal_node->class_closure_bsa = NULL;
1712   signal_node->accumulator = NULL;
1713   signal_node->c_marshaller = NULL;
1714   signal_node->emission_hooks = NULL;
1715   
1716 #ifdef  G_ENABLE_DEBUG
1717   /* check current emissions */
1718   {
1719     Emission *emission;
1720     
1721     for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1722          emission; emission = emission->next)
1723       if (emission->ihint.signal_id == node.signal_id)
1724         g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1725                     node.name, emission->instance);
1726   }
1727 #endif
1728   
1729   /* free contents that need to
1730    */
1731   SIGNAL_UNLOCK ();
1732   g_free (node.param_types);
1733   if (node.class_closure_bsa)
1734     {
1735       guint i;
1736
1737       for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1738         {
1739           ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1740
1741           g_closure_unref (cc->closure);
1742         }
1743       g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1744     }
1745   g_free (node.accumulator);
1746   if (node.emission_hooks)
1747     {
1748       g_hook_list_clear (node.emission_hooks);
1749       g_free (node.emission_hooks);
1750     }
1751   SIGNAL_LOCK ();
1752 }
1753
1754 /**
1755  * g_signal_override_class_closure:
1756  * @signal_id: the signal id
1757  * @instance_type: the instance type on which to override the class closure
1758  *  for the signal.
1759  * @class_closure: the closure.
1760  *
1761  * Overrides the class closure (i.e. the default handler) for the given signal
1762  * for emissions on instances of @instance_type. @instance_type must be derived
1763  * from the type to which the signal belongs.
1764  *
1765  * See g_signal_chain_from_overridden() and
1766  * g_signal_chain_from_overridden_handler() for how to chain up to the
1767  * parent class closure from inside the overridden one.
1768  */
1769 void
1770 g_signal_override_class_closure (guint     signal_id,
1771                                  GType     instance_type,
1772                                  GClosure *class_closure)
1773 {
1774   SignalNode *node;
1775   
1776   g_return_if_fail (signal_id > 0);
1777   g_return_if_fail (class_closure != NULL);
1778   
1779   SIGNAL_LOCK ();
1780   node = LOOKUP_SIGNAL_NODE (signal_id);
1781   if (!g_type_is_a (instance_type, node->itype))
1782     g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1783   else
1784     {
1785       ClassClosure *cc = signal_find_class_closure (node, instance_type);
1786       
1787       if (cc && cc->instance_type == instance_type)
1788         g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1789       else
1790         signal_add_class_closure (node, instance_type, class_closure);
1791     }
1792   SIGNAL_UNLOCK ();
1793 }
1794
1795 /**
1796  * g_signal_override_class_handler:
1797  * @signal_name: the name for the signal
1798  * @instance_type: the instance type on which to override the class handler
1799  *  for the signal.
1800  * @class_handler: the handler.
1801  *
1802  * Overrides the class closure (i.e. the default handler) for the
1803  * given signal for emissions on instances of @instance_type with
1804  * callabck @class_handler. @instance_type must be derived from the
1805  * type to which the signal belongs.
1806  *
1807  * See g_signal_chain_from_overridden() and
1808  * g_signal_chain_from_overridden_handler() for how to chain up to the
1809  * parent class closure from inside the overridden one.
1810  *
1811  * Since: 2.18
1812  */
1813 void
1814 g_signal_override_class_handler (const gchar *signal_name,
1815                                  GType        instance_type,
1816                                  GCallback    class_handler)
1817 {
1818   guint signal_id;
1819
1820   g_return_if_fail (signal_name != NULL);
1821   g_return_if_fail (instance_type != G_TYPE_NONE);
1822   g_return_if_fail (class_handler != NULL);
1823
1824   signal_id = g_signal_lookup (signal_name, instance_type);
1825
1826   if (signal_id)
1827     g_signal_override_class_closure (signal_id, instance_type,
1828                                      g_cclosure_new (class_handler, NULL, NULL));
1829   else
1830     g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'",
1831                G_STRLOC, signal_name, instance_type);
1832
1833 }
1834
1835 /**
1836  * g_signal_chain_from_overridden:
1837  * @instance_and_params: the argument list of the signal emission. The first
1838  *  element in the array is a #GValue for the instance the signal is being
1839  *  emitted on. The rest are any arguments to be passed to the signal.
1840  * @return_value: Location for the return value.
1841  *
1842  * Calls the original class closure of a signal. This function should only
1843  * be called from an overridden class closure; see
1844  * g_signal_override_class_closure() and
1845  * g_signal_override_class_handler().
1846  */
1847 void
1848 g_signal_chain_from_overridden (const GValue *instance_and_params,
1849                                 GValue       *return_value)
1850 {
1851   GType chain_type = 0, restore_type = 0;
1852   Emission *emission = NULL;
1853   GClosure *closure = NULL;
1854   guint n_params = 0;
1855   gpointer instance;
1856   
1857   g_return_if_fail (instance_and_params != NULL);
1858   instance = g_value_peek_pointer (instance_and_params);
1859   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1860   
1861   SIGNAL_LOCK ();
1862   emission = emission_find_innermost (instance);
1863   if (emission)
1864     {
1865       SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1866       
1867       g_assert (node != NULL);  /* paranoid */
1868       
1869       /* we should probably do the same parameter checks as g_signal_emit() here.
1870        */
1871       if (emission->chain_type != G_TYPE_NONE)
1872         {
1873           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1874           
1875           g_assert (cc != NULL);        /* closure currently in call stack */
1876
1877           n_params = node->n_params;
1878           restore_type = cc->instance_type;
1879           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1880           if (cc && cc->instance_type != restore_type)
1881             {
1882               closure = cc->closure;
1883               chain_type = cc->instance_type;
1884             }
1885         }
1886       else
1887         g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1888     }
1889   else
1890     g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1891
1892   if (closure)
1893     {
1894       emission->chain_type = chain_type;
1895       SIGNAL_UNLOCK ();
1896       g_closure_invoke (closure,
1897                         return_value,
1898                         n_params + 1,
1899                         instance_and_params,
1900                         &emission->ihint);
1901       SIGNAL_LOCK ();
1902       emission->chain_type = restore_type;
1903     }
1904   SIGNAL_UNLOCK ();
1905 }
1906
1907 /**
1908  * g_signal_chain_from_overridden_handler:
1909  * @instance: the instance the signal is being emitted on.
1910  * @...: parameters to be passed to the parent class closure, followed by a
1911  *  location for the return value. If the return type of the signal
1912  *  is #G_TYPE_NONE, the return value location can be omitted.
1913  *
1914  * Calls the original class closure of a signal. This function should
1915  * only be called from an overridden class closure; see
1916  * g_signal_override_class_closure() and
1917  * g_signal_override_class_handler().
1918  *
1919  * Since: 2.18
1920  */
1921 void
1922 g_signal_chain_from_overridden_handler (gpointer instance,
1923                                         ...)
1924 {
1925   GType chain_type = 0, restore_type = 0;
1926   Emission *emission = NULL;
1927   GClosure *closure = NULL;
1928   SignalNode *node;
1929   guint n_params = 0;
1930
1931   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1932
1933   SIGNAL_LOCK ();
1934   emission = emission_find_innermost (instance);
1935   if (emission)
1936     {
1937       node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1938
1939       g_assert (node != NULL);  /* paranoid */
1940
1941       /* we should probably do the same parameter checks as g_signal_emit() here.
1942        */
1943       if (emission->chain_type != G_TYPE_NONE)
1944         {
1945           ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1946
1947           g_assert (cc != NULL);        /* closure currently in call stack */
1948
1949           n_params = node->n_params;
1950           restore_type = cc->instance_type;
1951           cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1952           if (cc && cc->instance_type != restore_type)
1953             {
1954               closure = cc->closure;
1955               chain_type = cc->instance_type;
1956             }
1957         }
1958       else
1959         g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1960     }
1961   else
1962     g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1963
1964   if (closure)
1965     {
1966       GValue *instance_and_params, *free_me = NULL;
1967       GType signal_return_type;
1968       GValue *param_values;
1969       va_list var_args;
1970       guint i;
1971
1972       va_start (var_args, instance);
1973
1974       signal_return_type = node->return_type;
1975       free_me = g_new (GValue, node->n_params + 1);
1976       instance_and_params = free_me;
1977       param_values = instance_and_params + 1;
1978
1979       for (i = 0; i < node->n_params; i++)
1980         {
1981           gchar *error;
1982           GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1983           gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1984
1985           param_values[i].g_type = 0;
1986           SIGNAL_UNLOCK ();
1987           g_value_init (param_values + i, ptype);
1988           G_VALUE_COLLECT (param_values + i,
1989                            var_args,
1990                            static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1991                            &error);
1992           if (error)
1993             {
1994               g_warning ("%s: %s", G_STRLOC, error);
1995               g_free (error);
1996
1997               /* we purposely leak the value here, it might not be
1998                * in a sane state if an error condition occoured
1999                */
2000               while (i--)
2001                 g_value_unset (param_values + i);
2002
2003               g_free (free_me);
2004               va_end (var_args);
2005               return;
2006             }
2007           SIGNAL_LOCK ();
2008         }
2009
2010       SIGNAL_UNLOCK ();
2011       instance_and_params->g_type = 0;
2012       g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2013       g_value_set_instance (instance_and_params, instance);
2014       SIGNAL_LOCK ();
2015
2016       emission->chain_type = chain_type;
2017       SIGNAL_UNLOCK ();
2018
2019       if (signal_return_type == G_TYPE_NONE)
2020         {
2021           g_closure_invoke (closure,
2022                             NULL,
2023                             n_params + 1,
2024                             instance_and_params,
2025                             &emission->ihint);
2026         }
2027       else
2028         {
2029           GValue return_value = { 0, };
2030           gchar *error = NULL;
2031           GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2032           gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2033
2034           g_value_init (&return_value, rtype);
2035
2036           g_closure_invoke (closure,
2037                             &return_value,
2038                             n_params + 1,
2039                             instance_and_params,
2040                             &emission->ihint);
2041
2042           G_VALUE_LCOPY (&return_value,
2043                          var_args,
2044                          static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2045                          &error);
2046           if (!error)
2047             {
2048               g_value_unset (&return_value);
2049             }
2050           else
2051             {
2052               g_warning ("%s: %s", G_STRLOC, error);
2053               g_free (error);
2054
2055               /* we purposely leak the value here, it might not be
2056                * in a sane state if an error condition occured
2057                */
2058             }
2059         }
2060
2061       for (i = 0; i < n_params; i++)
2062         g_value_unset (param_values + i);
2063       g_value_unset (instance_and_params);
2064       if (free_me)
2065         g_free (free_me);
2066
2067       va_end (var_args);
2068
2069       SIGNAL_LOCK ();
2070       emission->chain_type = restore_type;
2071     }
2072   SIGNAL_UNLOCK ();
2073 }
2074
2075 /**
2076  * g_signal_get_invocation_hint:
2077  * @instance: the instance to query
2078  *
2079  * Returns the invocation hint of the innermost signal emission of instance.
2080  *
2081  * Returns: the invocation hint of the innermost signal emission.
2082  */
2083 GSignalInvocationHint*
2084 g_signal_get_invocation_hint (gpointer instance)
2085 {
2086   Emission *emission = NULL;
2087   
2088   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
2089
2090   SIGNAL_LOCK ();
2091   emission = emission_find_innermost (instance);
2092   SIGNAL_UNLOCK ();
2093   
2094   return emission ? &emission->ihint : NULL;
2095 }
2096
2097 /**
2098  * g_signal_connect_closure_by_id:
2099  * @instance: the instance to connect to.
2100  * @signal_id: the id of the signal.
2101  * @detail: the detail.
2102  * @closure: the closure to connect.
2103  * @after: whether the handler should be called before or after the
2104  *  default handler of the signal.
2105  *
2106  * Connects a closure to a signal for a particular object.
2107  *
2108  * Returns: the handler id
2109  */
2110 gulong
2111 g_signal_connect_closure_by_id (gpointer  instance,
2112                                 guint     signal_id,
2113                                 GQuark    detail,
2114                                 GClosure *closure,
2115                                 gboolean  after)
2116 {
2117   SignalNode *node;
2118   gulong handler_seq_no = 0;
2119   
2120   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2121   g_return_val_if_fail (signal_id > 0, 0);
2122   g_return_val_if_fail (closure != NULL, 0);
2123   
2124   SIGNAL_LOCK ();
2125   node = LOOKUP_SIGNAL_NODE (signal_id);
2126   if (node)
2127     {
2128       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2129         g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2130       else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2131         g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2132       else
2133         {
2134           Handler *handler = handler_new (after);
2135           
2136           handler_seq_no = handler->sequential_number;
2137           handler->detail = detail;
2138           handler->closure = g_closure_ref (closure);
2139           g_closure_sink (closure);
2140           handler_insert (signal_id, instance, handler);
2141           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
2142             g_closure_set_marshal (closure, node->c_marshaller);
2143         }
2144     }
2145   else
2146     g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2147   SIGNAL_UNLOCK ();
2148   
2149   return handler_seq_no;
2150 }
2151
2152 /**
2153  * g_signal_connect_closure:
2154  * @instance: the instance to connect to.
2155  * @detailed_signal: a string of the form "signal-name::detail".
2156  * @closure: the closure to connect.
2157  * @after: whether the handler should be called before or after the
2158  *  default handler of the signal.
2159  *
2160  * Connects a closure to a signal for a particular object.
2161  *
2162  * Returns: the handler id
2163  */
2164 gulong
2165 g_signal_connect_closure (gpointer     instance,
2166                           const gchar *detailed_signal,
2167                           GClosure    *closure,
2168                           gboolean     after)
2169 {
2170   guint signal_id;
2171   gulong handler_seq_no = 0;
2172   GQuark detail = 0;
2173   GType itype;
2174
2175   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2176   g_return_val_if_fail (detailed_signal != NULL, 0);
2177   g_return_val_if_fail (closure != NULL, 0);
2178
2179   SIGNAL_LOCK ();
2180   itype = G_TYPE_FROM_INSTANCE (instance);
2181   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2182   if (signal_id)
2183     {
2184       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2185
2186       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2187         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2188       else if (!g_type_is_a (itype, node->itype))
2189         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2190       else
2191         {
2192           Handler *handler = handler_new (after);
2193
2194           handler_seq_no = handler->sequential_number;
2195           handler->detail = detail;
2196           handler->closure = g_closure_ref (closure);
2197           g_closure_sink (closure);
2198           handler_insert (signal_id, instance, handler);
2199           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2200             g_closure_set_marshal (handler->closure, node->c_marshaller);
2201         }
2202     }
2203   else
2204     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2205   SIGNAL_UNLOCK ();
2206
2207   return handler_seq_no;
2208 }
2209
2210 /**
2211  * g_signal_connect_data:
2212  * @instance: the instance to connect to.
2213  * @detailed_signal: a string of the form "signal-name::detail".
2214  * @c_handler: the #GCallback to connect.
2215  * @data: data to pass to @c_handler calls.
2216  * @destroy_data: a #GClosureNotify for @data.
2217  * @connect_flags: a combination of #GConnectFlags.
2218  *
2219  * Connects a #GCallback function to a signal for a particular object. Similar
2220  * to g_signal_connect(), but allows to provide a #GClosureNotify for the data
2221  * which will be called when the signal handler is disconnected and no longer
2222  * used. Specify @connect_flags if you need <literal>..._after()</literal> or
2223  * <literal>..._swapped()</literal> variants of this function.
2224  *
2225  * Returns: the handler id
2226  */
2227 gulong
2228 g_signal_connect_data (gpointer       instance,
2229                        const gchar   *detailed_signal,
2230                        GCallback      c_handler,
2231                        gpointer       data,
2232                        GClosureNotify destroy_data,
2233                        GConnectFlags  connect_flags)
2234 {
2235   guint signal_id;
2236   gulong handler_seq_no = 0;
2237   GQuark detail = 0;
2238   GType itype;
2239   gboolean swapped, after;
2240   
2241   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2242   g_return_val_if_fail (detailed_signal != NULL, 0);
2243   g_return_val_if_fail (c_handler != NULL, 0);
2244
2245   swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
2246   after = (connect_flags & G_CONNECT_AFTER) != FALSE;
2247
2248   SIGNAL_LOCK ();
2249   itype = G_TYPE_FROM_INSTANCE (instance);
2250   signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
2251   if (signal_id)
2252     {
2253       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2254
2255       if (detail && !(node->flags & G_SIGNAL_DETAILED))
2256         g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
2257       else if (!g_type_is_a (itype, node->itype))
2258         g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2259       else
2260         {
2261           Handler *handler = handler_new (after);
2262
2263           handler_seq_no = handler->sequential_number;
2264           handler->detail = detail;
2265           handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
2266           g_closure_sink (handler->closure);
2267           handler_insert (signal_id, instance, handler);
2268           if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
2269             g_closure_set_marshal (handler->closure, node->c_marshaller);
2270         }
2271     }
2272   else
2273     g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2274   SIGNAL_UNLOCK ();
2275
2276   return handler_seq_no;
2277 }
2278
2279 /**
2280  * g_signal_handler_block:
2281  * @instance: The instance to block the signal handler of.
2282  * @handler_id: Handler id of the handler to be blocked.
2283  *
2284  * Blocks a handler of an instance so it will not be called during any
2285  * signal emissions unless it is unblocked again. Thus "blocking" a
2286  * signal handler means to temporarily deactive it, a signal handler
2287  * has to be unblocked exactly the same amount of times it has been
2288  * blocked before to become active again.
2289  *
2290  * The @handler_id has to be a valid signal handler id, connected to a
2291  * signal of @instance.
2292  */
2293 void
2294 g_signal_handler_block (gpointer instance,
2295                         gulong   handler_id)
2296 {
2297   Handler *handler;
2298   
2299   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2300   g_return_if_fail (handler_id > 0);
2301   
2302   SIGNAL_LOCK ();
2303   handler = handler_lookup (instance, handler_id, NULL);
2304   if (handler)
2305     {
2306 #ifndef G_DISABLE_CHECKS
2307       if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
2308         g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
2309 #endif
2310       handler->block_count += 1;
2311     }
2312   else
2313     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2314   SIGNAL_UNLOCK ();
2315 }
2316
2317 /**
2318  * g_signal_handler_unblock:
2319  * @instance: The instance to unblock the signal handler of.
2320  * @handler_id: Handler id of the handler to be unblocked.
2321  *
2322  * Undoes the effect of a previous g_signal_handler_block() call.  A
2323  * blocked handler is skipped during signal emissions and will not be
2324  * invoked, unblocking it (for exactly the amount of times it has been
2325  * blocked before) reverts its "blocked" state, so the handler will be
2326  * recognized by the signal system and is called upon future or
2327  * currently ongoing signal emissions (since the order in which
2328  * handlers are called during signal emissions is deterministic,
2329  * whether the unblocked handler in question is called as part of a
2330  * currently ongoing emission depends on how far that emission has
2331  * proceeded yet).
2332  *
2333  * The @handler_id has to be a valid id of a signal handler that is
2334  * connected to a signal of @instance and is currently blocked.
2335  */
2336 void
2337 g_signal_handler_unblock (gpointer instance,
2338                           gulong   handler_id)
2339 {
2340   Handler *handler;
2341   
2342   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2343   g_return_if_fail (handler_id > 0);
2344   
2345   SIGNAL_LOCK ();
2346   handler = handler_lookup (instance, handler_id, NULL);
2347   if (handler)
2348     {
2349       if (handler->block_count)
2350         handler->block_count -= 1;
2351       else
2352         g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
2353     }
2354   else
2355     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2356   SIGNAL_UNLOCK ();
2357 }
2358
2359 /**
2360  * g_signal_handler_disconnect:
2361  * @instance: The instance to remove the signal handler from.
2362  * @handler_id: Handler id of the handler to be disconnected.
2363  *
2364  * Disconnects a handler from an instance so it will not be called during
2365  * any future or currently ongoing emissions of the signal it has been
2366  * connected to. The @handler_id becomes invalid and may be reused.
2367  *
2368  * The @handler_id has to be a valid signal handler id, connected to a
2369  * signal of @instance.
2370  */
2371 void
2372 g_signal_handler_disconnect (gpointer instance,
2373                              gulong   handler_id)
2374 {
2375   Handler *handler;
2376   guint signal_id;
2377   
2378   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2379   g_return_if_fail (handler_id > 0);
2380   
2381   SIGNAL_LOCK ();
2382   handler = handler_lookup (instance, handler_id, &signal_id);
2383   if (handler)
2384     {
2385       handler->sequential_number = 0;
2386       handler->block_count = 1;
2387       handler_unref_R (signal_id, instance, handler);
2388     }
2389   else
2390     g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
2391   SIGNAL_UNLOCK ();
2392 }
2393
2394 /**
2395  * g_signal_handler_is_connected:
2396  * @instance: The instance where a signal handler is sought.
2397  * @handler_id: the handler id.
2398  *
2399  * Returns whether @handler_id is the id of a handler connected to @instance.
2400  *
2401  * Returns: whether @handler_id identifies a handler connected to @instance.
2402  */
2403 gboolean
2404 g_signal_handler_is_connected (gpointer instance,
2405                                gulong   handler_id)
2406 {
2407   Handler *handler;
2408   gboolean connected;
2409
2410   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2411
2412   SIGNAL_LOCK ();
2413   handler = handler_lookup (instance, handler_id, NULL);
2414   connected = handler != NULL;
2415   SIGNAL_UNLOCK ();
2416
2417   return connected;
2418 }
2419
2420 void
2421 g_signal_handlers_destroy (gpointer instance)
2422 {
2423   GBSearchArray *hlbsa;
2424   
2425   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2426   
2427   SIGNAL_LOCK ();
2428   hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
2429   if (hlbsa)
2430     {
2431       guint i;
2432       
2433       /* reentrancy caution, delete instance trace first */
2434       g_hash_table_remove (g_handler_list_bsa_ht, instance);
2435       
2436       for (i = 0; i < hlbsa->n_nodes; i++)
2437         {
2438           HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
2439           Handler *handler = hlist->handlers;
2440           
2441           while (handler)
2442             {
2443               Handler *tmp = handler;
2444               
2445               handler = tmp->next;
2446               tmp->block_count = 1;
2447               /* cruel unlink, this works because _all_ handlers vanish */
2448               tmp->next = NULL;
2449               tmp->prev = tmp;
2450               if (tmp->sequential_number)
2451                 {
2452                   tmp->sequential_number = 0;
2453                   handler_unref_R (0, NULL, tmp);
2454                 }
2455             }
2456         }
2457       g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
2458     }
2459   SIGNAL_UNLOCK ();
2460 }
2461
2462 /**
2463  * g_signal_handler_find:
2464  * @instance: The instance owning the signal handler to be found.
2465  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2466  *  and/or @data the handler has to match.
2467  * @signal_id: Signal the handler has to be connected to.
2468  * @detail: Signal detail the handler has to be connected to.
2469  * @closure: The closure the handler will invoke.
2470  * @func: The C closure callback of the handler (useless for non-C closures).
2471  * @data: The closure data of the handler's closure.
2472  *
2473  * Finds the first signal handler that matches certain selection criteria.
2474  * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2475  * flags, and the criteria values are passed as arguments.
2476  * The match @mask has to be non-0 for successful matches.
2477  * If no handler was found, 0 is returned.
2478  *
2479  * Returns: A valid non-0 signal handler id for a successful match.
2480  */
2481 gulong
2482 g_signal_handler_find (gpointer         instance,
2483                        GSignalMatchType mask,
2484                        guint            signal_id,
2485                        GQuark           detail,
2486                        GClosure        *closure,
2487                        gpointer         func,
2488                        gpointer         data)
2489 {
2490   gulong handler_seq_no = 0;
2491   
2492   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2493   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2494   
2495   if (mask & G_SIGNAL_MATCH_MASK)
2496     {
2497       HandlerMatch *mlist;
2498       
2499       SIGNAL_LOCK ();
2500       mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
2501       if (mlist)
2502         {
2503           handler_seq_no = mlist->handler->sequential_number;
2504           handler_match_free1_R (mlist, instance);
2505         }
2506       SIGNAL_UNLOCK ();
2507     }
2508   
2509   return handler_seq_no;
2510 }
2511
2512 static guint
2513 signal_handlers_foreach_matched_R (gpointer         instance,
2514                                    GSignalMatchType mask,
2515                                    guint            signal_id,
2516                                    GQuark           detail,
2517                                    GClosure        *closure,
2518                                    gpointer         func,
2519                                    gpointer         data,
2520                                    void           (*callback) (gpointer instance,
2521                                                                gulong   handler_seq_no))
2522 {
2523   HandlerMatch *mlist;
2524   guint n_handlers = 0;
2525   
2526   mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
2527   while (mlist)
2528     {
2529       n_handlers++;
2530       if (mlist->handler->sequential_number)
2531         {
2532           SIGNAL_UNLOCK ();
2533           callback (instance, mlist->handler->sequential_number);
2534           SIGNAL_LOCK ();
2535         }
2536       mlist = handler_match_free1_R (mlist, instance);
2537     }
2538   
2539   return n_handlers;
2540 }
2541
2542 /**
2543  * g_signal_handlers_block_matched:
2544  * @instance: The instance to block handlers from.
2545  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2546  *  and/or @data the handlers have to match.
2547  * @signal_id: Signal the handlers have to be connected to.
2548  * @detail: Signal detail the handlers have to be connected to.
2549  * @closure: The closure the handlers will invoke.
2550  * @func: The C closure callback of the handlers (useless for non-C closures).
2551  * @data: The closure data of the handlers' closures.
2552  *
2553  * Blocks all handlers on an instance that match a certain selection criteria.
2554  * The criteria mask is passed as an OR-ed combination of #GSignalMatchType
2555  * flags, and the criteria values are passed as arguments.
2556  * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2557  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2558  * If no handlers were found, 0 is returned, the number of blocked handlers
2559  * otherwise.
2560  *
2561  * Returns: The number of handlers that matched.
2562  */
2563 guint
2564 g_signal_handlers_block_matched (gpointer         instance,
2565                                  GSignalMatchType mask,
2566                                  guint            signal_id,
2567                                  GQuark           detail,
2568                                  GClosure        *closure,
2569                                  gpointer         func,
2570                                  gpointer         data)
2571 {
2572   guint n_handlers = 0;
2573   
2574   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2575   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2576   
2577   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2578     {
2579       SIGNAL_LOCK ();
2580       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2581                                                       closure, func, data,
2582                                                       g_signal_handler_block);
2583       SIGNAL_UNLOCK ();
2584     }
2585   
2586   return n_handlers;
2587 }
2588
2589 /**
2590  * g_signal_handlers_unblock_matched:
2591  * @instance: The instance to unblock handlers from.
2592  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2593  *  and/or @data the handlers have to match.
2594  * @signal_id: Signal the handlers have to be connected to.
2595  * @detail: Signal detail the handlers have to be connected to.
2596  * @closure: The closure the handlers will invoke.
2597  * @func: The C closure callback of the handlers (useless for non-C closures).
2598  * @data: The closure data of the handlers' closures.
2599  *
2600  * Unblocks all handlers on an instance that match a certain selection
2601  * criteria. The criteria mask is passed as an OR-ed combination of
2602  * #GSignalMatchType flags, and the criteria values are passed as arguments.
2603  * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC
2604  * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches.
2605  * If no handlers were found, 0 is returned, the number of unblocked handlers
2606  * otherwise. The match criteria should not apply to any handlers that are
2607  * not currently blocked.
2608  *
2609  * Returns: The number of handlers that matched.
2610  */
2611 guint
2612 g_signal_handlers_unblock_matched (gpointer         instance,
2613                                    GSignalMatchType mask,
2614                                    guint            signal_id,
2615                                    GQuark           detail,
2616                                    GClosure        *closure,
2617                                    gpointer         func,
2618                                    gpointer         data)
2619 {
2620   guint n_handlers = 0;
2621   
2622   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2623   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2624   
2625   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2626     {
2627       SIGNAL_LOCK ();
2628       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2629                                                       closure, func, data,
2630                                                       g_signal_handler_unblock);
2631       SIGNAL_UNLOCK ();
2632     }
2633   
2634   return n_handlers;
2635 }
2636
2637 /**
2638  * g_signal_handlers_disconnect_matched:
2639  * @instance: The instance to remove handlers from.
2640  * @mask: Mask indicating which of @signal_id, @detail, @closure, @func
2641  *  and/or @data the handlers have to match.
2642  * @signal_id: Signal the handlers have to be connected to.
2643  * @detail: Signal detail the handlers have to be connected to.
2644  * @closure: The closure the handlers will invoke.
2645  * @func: The C closure callback of the handlers (useless for non-C closures).
2646  * @data: The closure data of the handlers' closures.
2647  *
2648  * Disconnects all handlers on an instance that match a certain
2649  * selection criteria. The criteria mask is passed as an OR-ed
2650  * combination of #GSignalMatchType flags, and the criteria values are
2651  * passed as arguments.  Passing at least one of the
2652  * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or
2653  * %G_SIGNAL_MATCH_DATA match flags is required for successful
2654  * matches.  If no handlers were found, 0 is returned, the number of
2655  * disconnected handlers otherwise.
2656  *
2657  * Returns: The number of handlers that matched.
2658  */
2659 guint
2660 g_signal_handlers_disconnect_matched (gpointer         instance,
2661                                       GSignalMatchType mask,
2662                                       guint            signal_id,
2663                                       GQuark           detail,
2664                                       GClosure        *closure,
2665                                       gpointer         func,
2666                                       gpointer         data)
2667 {
2668   guint n_handlers = 0;
2669   
2670   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
2671   g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
2672   
2673   if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
2674     {
2675       SIGNAL_LOCK ();
2676       n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
2677                                                       closure, func, data,
2678                                                       g_signal_handler_disconnect);
2679       SIGNAL_UNLOCK ();
2680     }
2681   
2682   return n_handlers;
2683 }
2684
2685 /**
2686  * g_signal_has_handler_pending:
2687  * @instance: the object whose signal handlers are sought.
2688  * @signal_id: the signal id.
2689  * @detail: the detail.
2690  * @may_be_blocked: whether blocked handlers should count as match.
2691  *
2692  * Returns whether there are any handlers connected to @instance for the
2693  * given signal id and detail.
2694  *
2695  * One example of when you might use this is when the arguments to the
2696  * signal are difficult to compute. A class implementor may opt to not
2697  * emit the signal if no one is attached anyway, thus saving the cost
2698  * of building the arguments.
2699  *
2700  * Returns: %TRUE if a handler is connected to the signal, %FALSE
2701  *          otherwise.
2702  */
2703 gboolean
2704 g_signal_has_handler_pending (gpointer instance,
2705                               guint    signal_id,
2706                               GQuark   detail,
2707                               gboolean may_be_blocked)
2708 {
2709   HandlerMatch *mlist;
2710   gboolean has_pending;
2711   
2712   g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
2713   g_return_val_if_fail (signal_id > 0, FALSE);
2714   
2715   SIGNAL_LOCK ();
2716   if (detail)
2717     {
2718       SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
2719       
2720       if (!(node->flags & G_SIGNAL_DETAILED))
2721         {
2722           g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2723           SIGNAL_UNLOCK ();
2724           return FALSE;
2725         }
2726     }
2727   mlist = handlers_find (instance,
2728                          (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
2729                          signal_id, detail, NULL, NULL, NULL, TRUE);
2730   if (mlist)
2731     {
2732       has_pending = TRUE;
2733       handler_match_free1_R (mlist, instance);
2734     }
2735   else
2736     has_pending = FALSE;
2737   SIGNAL_UNLOCK ();
2738   
2739   return has_pending;
2740 }
2741
2742 static inline gboolean
2743 signal_check_skip_emission (SignalNode *node,
2744                             gpointer    instance,
2745                             GQuark      detail)
2746 {
2747   HandlerList *hlist;
2748
2749   /* are we able to check for NULL class handlers? */
2750   if (!node->test_class_offset)
2751     return FALSE;
2752
2753   /* are there emission hooks pending? */
2754   if (node->emission_hooks && node->emission_hooks->hooks)
2755     return FALSE;
2756
2757   /* is there a non-NULL class handler? */
2758   if (node->test_class_offset != TEST_CLASS_MAGIC)
2759     {
2760       GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2761
2762       if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2763         return FALSE;
2764     }
2765
2766   /* are signals being debugged? */
2767 #ifdef  G_ENABLE_DEBUG
2768   IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2769     return FALSE;
2770 #endif /* G_ENABLE_DEBUG */
2771
2772   /* is this a no-recurse signal already in emission? */
2773   if (node->flags & G_SIGNAL_NO_RECURSE &&
2774       emission_find (g_restart_emissions, node->signal_id, detail, instance))
2775     return FALSE;
2776
2777   /* do we have pending handlers? */
2778   hlist = handler_list_lookup (node->signal_id, instance);
2779   if (hlist && hlist->handlers)
2780     return FALSE;
2781
2782   /* none of the above, no emission required */
2783   return TRUE;
2784 }
2785
2786 /**
2787  * g_signal_emitv:
2788  * @instance_and_params: argument list for the signal emission. The first
2789  *  element in the array is a #GValue for the instance the signal is
2790  *  being emitted on. The rest are any arguments to be passed to the
2791  *  signal.
2792  * @signal_id: the signal id
2793  * @detail: the detail
2794  * @return_value: Location to store the return value of the signal emission.
2795  *
2796  * Emits a signal.
2797  *
2798  * Note that g_signal_emitv() doesn't change @return_value if no handlers are
2799  * connected, in contrast to g_signal_emit() and g_signal_emit_valist().
2800  */
2801 void
2802 g_signal_emitv (const GValue *instance_and_params,
2803                 guint         signal_id,
2804                 GQuark        detail,
2805                 GValue       *return_value)
2806 {
2807   gpointer instance;
2808   SignalNode *node;
2809 #ifdef G_ENABLE_DEBUG
2810   const GValue *param_values;
2811   guint i;
2812 #endif
2813   
2814   g_return_if_fail (instance_and_params != NULL);
2815   instance = g_value_peek_pointer (instance_and_params);
2816   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2817   g_return_if_fail (signal_id > 0);
2818
2819 #ifdef G_ENABLE_DEBUG
2820   param_values = instance_and_params + 1;
2821 #endif
2822
2823   SIGNAL_LOCK ();
2824   node = LOOKUP_SIGNAL_NODE (signal_id);
2825   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2826     {
2827       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2828       SIGNAL_UNLOCK ();
2829       return;
2830     }
2831 #ifdef G_ENABLE_DEBUG
2832   if (detail && !(node->flags & G_SIGNAL_DETAILED))
2833     {
2834       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2835       SIGNAL_UNLOCK ();
2836       return;
2837     }
2838   for (i = 0; i < node->n_params; i++)
2839     if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2840       {
2841         g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2842                     G_STRLOC,
2843                     type_debug_name (node->param_types[i]),
2844                     i,
2845                     node->name,
2846                     G_VALUE_TYPE_NAME (param_values + i));
2847         SIGNAL_UNLOCK ();
2848         return;
2849       }
2850   if (node->return_type != G_TYPE_NONE)
2851     {
2852       if (!return_value)
2853         {
2854           g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2855                       G_STRLOC,
2856                       type_debug_name (node->return_type),
2857                       node->name);
2858           SIGNAL_UNLOCK ();
2859           return;
2860         }
2861       else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2862         {
2863           g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2864                       G_STRLOC,
2865                       type_debug_name (node->return_type),
2866                       node->name,
2867                       G_VALUE_TYPE_NAME (return_value));
2868           SIGNAL_UNLOCK ();
2869           return;
2870         }
2871     }
2872   else
2873     return_value = NULL;
2874 #endif  /* G_ENABLE_DEBUG */
2875
2876   /* optimize NOP emissions */
2877   if (signal_check_skip_emission (node, instance, detail))
2878     {
2879       /* nothing to do to emit this signal */
2880       SIGNAL_UNLOCK ();
2881       /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2882       return;
2883     }
2884
2885   SIGNAL_UNLOCK ();
2886   signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2887 }
2888
2889 /**
2890  * g_signal_emit_valist:
2891  * @instance: the instance the signal is being emitted on.
2892  * @signal_id: the signal id
2893  * @detail: the detail
2894  * @var_args: a list of parameters to be passed to the signal, followed by a
2895  *  location for the return value. If the return type of the signal
2896  *  is #G_TYPE_NONE, the return value location can be omitted.
2897  *
2898  * Emits a signal.
2899  *
2900  * Note that g_signal_emit_valist() resets the return value to the default
2901  * if no handlers are connected, in contrast to g_signal_emitv().
2902  */
2903 void
2904 g_signal_emit_valist (gpointer instance,
2905                       guint    signal_id,
2906                       GQuark   detail,
2907                       va_list  var_args)
2908 {
2909   GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2910   GType signal_return_type;
2911   GValue *param_values;
2912   SignalNode *node;
2913   guint i, n_params;
2914   
2915   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2916   g_return_if_fail (signal_id > 0);
2917
2918   SIGNAL_LOCK ();
2919   node = LOOKUP_SIGNAL_NODE (signal_id);
2920   if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2921     {
2922       g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2923       SIGNAL_UNLOCK ();
2924       return;
2925     }
2926 #ifndef G_DISABLE_CHECKS
2927   if (detail && !(node->flags & G_SIGNAL_DETAILED))
2928     {
2929       g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2930       SIGNAL_UNLOCK ();
2931       return;
2932     }
2933 #endif  /* !G_DISABLE_CHECKS */
2934
2935   /* optimize NOP emissions */
2936   if (signal_check_skip_emission (node, instance, detail))
2937     {
2938       /* nothing to do to emit this signal */
2939       SIGNAL_UNLOCK ();
2940       /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2941       return;
2942     }
2943
2944   n_params = node->n_params;
2945   signal_return_type = node->return_type;
2946   if (node->n_params < MAX_STACK_VALUES)
2947     instance_and_params = stack_values;
2948   else
2949     {
2950       free_me = g_new (GValue, node->n_params + 1);
2951       instance_and_params = free_me;
2952     }
2953   param_values = instance_and_params + 1;
2954   for (i = 0; i < node->n_params; i++)
2955     {
2956       gchar *error;
2957       GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2958       gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2959       
2960       param_values[i].g_type = 0;
2961       SIGNAL_UNLOCK ();
2962       g_value_init (param_values + i, ptype);
2963       G_VALUE_COLLECT (param_values + i,
2964                        var_args,
2965                        static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2966                        &error);
2967       if (error)
2968         {
2969           g_warning ("%s: %s", G_STRLOC, error);
2970           g_free (error);
2971
2972           /* we purposely leak the value here, it might not be
2973            * in a sane state if an error condition occoured
2974            */
2975           while (i--)
2976             g_value_unset (param_values + i);
2977
2978           g_free (free_me);
2979           return;
2980         }
2981       SIGNAL_LOCK ();
2982     }
2983   SIGNAL_UNLOCK ();
2984   instance_and_params->g_type = 0;
2985   g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2986   g_value_set_instance (instance_and_params, instance);
2987   if (signal_return_type == G_TYPE_NONE)
2988     signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2989   else
2990     {
2991       GValue return_value = { 0, };
2992       gchar *error = NULL;
2993       GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2994       gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2995       
2996       g_value_init (&return_value, rtype);
2997
2998       signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2999
3000       G_VALUE_LCOPY (&return_value,
3001                      var_args,
3002                      static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
3003                      &error);
3004       if (!error)
3005         g_value_unset (&return_value);
3006       else
3007         {
3008           g_warning ("%s: %s", G_STRLOC, error);
3009           g_free (error);
3010           
3011           /* we purposely leak the value here, it might not be
3012            * in a sane state if an error condition occured
3013            */
3014         }
3015     }
3016   for (i = 0; i < n_params; i++)
3017     g_value_unset (param_values + i);
3018   g_value_unset (instance_and_params);
3019   if (free_me)
3020     g_free (free_me);
3021 }
3022
3023 /**
3024  * g_signal_emit:
3025  * @instance: the instance the signal is being emitted on.
3026  * @signal_id: the signal id
3027  * @detail: the detail
3028  * @...: parameters to be passed to the signal, followed by a
3029  *  location for the return value. If the return type of the signal
3030  *  is #G_TYPE_NONE, the return value location can be omitted.
3031  *
3032  * Emits a signal.
3033  *
3034  * Note that g_signal_emit() resets the return value to the default
3035  * if no handlers are connected, in contrast to g_signal_emitv().
3036  */
3037 void
3038 g_signal_emit (gpointer instance,
3039                guint    signal_id,
3040                GQuark   detail,
3041                ...)
3042 {
3043   va_list var_args;
3044
3045   va_start (var_args, detail);
3046   g_signal_emit_valist (instance, signal_id, detail, var_args);
3047   va_end (var_args);
3048 }
3049
3050 /**
3051  * g_signal_emit_by_name:
3052  * @instance: the instance the signal is being emitted on.
3053  * @detailed_signal: a string of the form "signal-name::detail".
3054  * @...: parameters to be passed to the signal, followed by a
3055  *  location for the return value. If the return type of the signal
3056  *  is #G_TYPE_NONE, the return value location can be omitted.
3057  *
3058  * Emits a signal.
3059  *
3060  * Note that g_signal_emit_by_name() resets the return value to the default
3061  * if no handlers are connected, in contrast to g_signal_emitv().
3062  */
3063 void
3064 g_signal_emit_by_name (gpointer     instance,
3065                        const gchar *detailed_signal,
3066                        ...)
3067 {
3068   GQuark detail = 0;
3069   guint signal_id;
3070
3071   g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
3072   g_return_if_fail (detailed_signal != NULL);
3073
3074   SIGNAL_LOCK ();
3075   signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
3076   SIGNAL_UNLOCK ();
3077
3078   if (signal_id)
3079     {
3080       va_list var_args;
3081
3082       va_start (var_args, detailed_signal);
3083       g_signal_emit_valist (instance, signal_id, detail, var_args);
3084       va_end (var_args);
3085     }
3086   else
3087     g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
3088 }
3089
3090 static inline gboolean
3091 accumulate (GSignalInvocationHint *ihint,
3092             GValue                *return_accu,
3093             GValue                *handler_return,
3094             SignalAccumulator     *accumulator)
3095 {
3096   gboolean continue_emission;
3097
3098   if (!accumulator)
3099     return TRUE;
3100
3101   continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
3102   g_value_reset (handler_return);
3103
3104   return continue_emission;
3105 }
3106
3107 static gboolean
3108 signal_emit_unlocked_R (SignalNode   *node,
3109                         GQuark        detail,
3110                         gpointer      instance,
3111                         GValue       *emission_return,
3112                         const GValue *instance_and_params)
3113 {
3114   SignalAccumulator *accumulator;
3115   Emission emission;
3116   GClosure *class_closure;
3117   HandlerList *hlist;
3118   Handler *handler_list = NULL;
3119   GValue *return_accu, accu = { 0, };
3120   guint signal_id;
3121   gulong max_sequential_handler_number;
3122   gboolean return_value_altered = FALSE;
3123   
3124 #ifdef  G_ENABLE_DEBUG
3125   IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
3126     {
3127       g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
3128                  g_type_name (G_TYPE_FROM_INSTANCE (instance)),
3129                  node->name, detail,
3130                  instance, node);
3131       if (g_trap_instance_signals == instance)
3132         G_BREAKPOINT ();
3133     }
3134 #endif  /* G_ENABLE_DEBUG */
3135   
3136   SIGNAL_LOCK ();
3137   signal_id = node->signal_id;
3138   if (node->flags & G_SIGNAL_NO_RECURSE)
3139     {
3140       Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
3141       
3142       if (node)
3143         {
3144           node->state = EMISSION_RESTART;
3145           SIGNAL_UNLOCK ();
3146           return return_value_altered;
3147         }
3148     }
3149   accumulator = node->accumulator;
3150   if (accumulator)
3151     {
3152       SIGNAL_UNLOCK ();
3153       g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3154       return_accu = &accu;
3155       SIGNAL_LOCK ();
3156     }
3157   else
3158     return_accu = emission_return;
3159   emission.instance = instance;
3160   emission.ihint.signal_id = node->signal_id;
3161   emission.ihint.detail = detail;
3162   emission.ihint.run_type = 0;
3163   emission.state = 0;
3164   emission.chain_type = G_TYPE_NONE;
3165   emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3166   class_closure = signal_lookup_closure (node, instance);
3167   
3168  EMIT_RESTART:
3169   
3170   if (handler_list)
3171     handler_unref_R (signal_id, instance, handler_list);
3172   max_sequential_handler_number = g_handler_sequential_number;
3173   hlist = handler_list_lookup (signal_id, instance);
3174   handler_list = hlist ? hlist->handlers : NULL;
3175   if (handler_list)
3176     handler_ref (handler_list);
3177   
3178   emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
3179   
3180   if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
3181     {
3182       emission.state = EMISSION_RUN;
3183
3184       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3185       SIGNAL_UNLOCK ();
3186       g_closure_invoke (class_closure,
3187                         return_accu,
3188                         node->n_params + 1,
3189                         instance_and_params,
3190                         &emission.ihint);
3191       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3192           emission.state == EMISSION_RUN)
3193         emission.state = EMISSION_STOP;
3194       SIGNAL_LOCK ();
3195       emission.chain_type = G_TYPE_NONE;
3196       return_value_altered = TRUE;
3197       
3198       if (emission.state == EMISSION_STOP)
3199         goto EMIT_CLEANUP;
3200       else if (emission.state == EMISSION_RESTART)
3201         goto EMIT_RESTART;
3202     }
3203   
3204   if (node->emission_hooks)
3205     {
3206       gboolean need_destroy, was_in_call, may_recurse = TRUE;
3207       GHook *hook;
3208
3209       emission.state = EMISSION_HOOK;
3210       hook = g_hook_first_valid (node->emission_hooks, may_recurse);
3211       while (hook)
3212         {
3213           SignalHook *signal_hook = SIGNAL_HOOK (hook);
3214           
3215           if (!signal_hook->detail || signal_hook->detail == detail)
3216             {
3217               GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
3218               
3219               was_in_call = G_HOOK_IN_CALL (hook);
3220               hook->flags |= G_HOOK_FLAG_IN_CALL;
3221               SIGNAL_UNLOCK ();
3222               need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
3223               SIGNAL_LOCK ();
3224               if (!was_in_call)
3225                 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
3226               if (need_destroy)
3227                 g_hook_destroy_link (node->emission_hooks, hook);
3228             }
3229           hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
3230         }
3231       
3232       if (emission.state == EMISSION_RESTART)
3233         goto EMIT_RESTART;
3234     }
3235   
3236   if (handler_list)
3237     {
3238       Handler *handler = handler_list;
3239       
3240       emission.state = EMISSION_RUN;
3241       handler_ref (handler);
3242       do
3243         {
3244           Handler *tmp;
3245           
3246           if (handler->after)
3247             {
3248               handler_unref_R (signal_id, instance, handler_list);
3249               handler_list = handler;
3250               break;
3251             }
3252           else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
3253                    handler->sequential_number < max_sequential_handler_number)
3254             {
3255               SIGNAL_UNLOCK ();
3256               g_closure_invoke (handler->closure,
3257                                 return_accu,
3258                                 node->n_params + 1,
3259                                 instance_and_params,
3260                                 &emission.ihint);
3261               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3262                   emission.state == EMISSION_RUN)
3263                 emission.state = EMISSION_STOP;
3264               SIGNAL_LOCK ();
3265               return_value_altered = TRUE;
3266               
3267               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3268             }
3269           else
3270             tmp = handler->next;
3271           
3272           if (tmp)
3273             handler_ref (tmp);
3274           handler_unref_R (signal_id, instance, handler_list);
3275           handler_list = handler;
3276           handler = tmp;
3277         }
3278       while (handler);
3279       
3280       if (emission.state == EMISSION_STOP)
3281         goto EMIT_CLEANUP;
3282       else if (emission.state == EMISSION_RESTART)
3283         goto EMIT_RESTART;
3284     }
3285   
3286   emission.ihint.run_type = G_SIGNAL_RUN_LAST;
3287   
3288   if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
3289     {
3290       emission.state = EMISSION_RUN;
3291       
3292       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3293       SIGNAL_UNLOCK ();
3294       g_closure_invoke (class_closure,
3295                         return_accu,
3296                         node->n_params + 1,
3297                         instance_and_params,
3298                         &emission.ihint);
3299       if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3300           emission.state == EMISSION_RUN)
3301         emission.state = EMISSION_STOP;
3302       SIGNAL_LOCK ();
3303       emission.chain_type = G_TYPE_NONE;
3304       return_value_altered = TRUE;
3305       
3306       if (emission.state == EMISSION_STOP)
3307         goto EMIT_CLEANUP;
3308       else if (emission.state == EMISSION_RESTART)
3309         goto EMIT_RESTART;
3310     }
3311   
3312   if (handler_list)
3313     {
3314       Handler *handler = handler_list;
3315       
3316       emission.state = EMISSION_RUN;
3317       handler_ref (handler);
3318       do
3319         {
3320           Handler *tmp;
3321           
3322           if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
3323               handler->sequential_number < max_sequential_handler_number)
3324             {
3325               SIGNAL_UNLOCK ();
3326               g_closure_invoke (handler->closure,
3327                                 return_accu,
3328                                 node->n_params + 1,
3329                                 instance_and_params,
3330                                 &emission.ihint);
3331               if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
3332                   emission.state == EMISSION_RUN)
3333                 emission.state = EMISSION_STOP;
3334               SIGNAL_LOCK ();
3335               return_value_altered = TRUE;
3336               
3337               tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
3338             }
3339           else
3340             tmp = handler->next;
3341           
3342           if (tmp)
3343             handler_ref (tmp);
3344           handler_unref_R (signal_id, instance, handler);
3345           handler = tmp;
3346         }
3347       while (handler);
3348       
3349       if (emission.state == EMISSION_STOP)
3350         goto EMIT_CLEANUP;
3351       else if (emission.state == EMISSION_RESTART)
3352         goto EMIT_RESTART;
3353     }
3354   
3355  EMIT_CLEANUP:
3356   
3357   emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
3358   
3359   if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
3360     {
3361       gboolean need_unset = FALSE;
3362       
3363       emission.state = EMISSION_STOP;
3364       
3365       emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
3366       SIGNAL_UNLOCK ();
3367       if (node->return_type != G_TYPE_NONE && !accumulator)
3368         {
3369           g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3370           need_unset = TRUE;
3371         }
3372       g_closure_invoke (class_closure,
3373                         node->return_type != G_TYPE_NONE ? &accu : NULL,
3374                         node->n_params + 1,
3375                         instance_and_params,
3376                         &emission.ihint);
3377       if (need_unset)
3378         g_value_unset (&accu);
3379       SIGNAL_LOCK ();
3380       emission.chain_type = G_TYPE_NONE;
3381       
3382       if (emission.state == EMISSION_RESTART)
3383         goto EMIT_RESTART;
3384     }
3385   
3386   if (handler_list)
3387     handler_unref_R (signal_id, instance, handler_list);
3388   
3389   emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
3390   SIGNAL_UNLOCK ();
3391   if (accumulator)
3392     g_value_unset (&accu);
3393   
3394   return return_value_altered;
3395 }
3396
3397 static const gchar*
3398 type_debug_name (GType type)
3399 {
3400   if (type)
3401     {
3402       const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
3403       return name ? name : "<unknown>";
3404     }
3405   else
3406     return "<invalid>";
3407 }
3408
3409 /**
3410  * g_signal_accumulator_true_handled:
3411  * @ihint: standard #GSignalAccumulator parameter
3412  * @return_accu: standard #GSignalAccumulator parameter
3413  * @handler_return: standard #GSignalAccumulator parameter
3414  * @dummy: standard #GSignalAccumulator parameter
3415  *
3416  * A predefined #GSignalAccumulator for signals that return a
3417  * boolean values. The behavior that this accumulator gives is
3418  * that a return of %TRUE stops the signal emission: no further
3419  * callbacks will be invoked, while a return of %FALSE allows
3420  * the emission to coninue. The idea here is that a %TRUE return
3421  * indicates that the callback <emphasis>handled</emphasis> the signal,
3422  * and no further handling is needed.
3423  *
3424  * Since: 2.4
3425  *
3426  * Returns: standard #GSignalAccumulator result
3427  */
3428 gboolean
3429 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
3430                                    GValue                *return_accu,
3431                                    const GValue          *handler_return,
3432                                    gpointer               dummy)
3433 {
3434   gboolean continue_emission;
3435   gboolean signal_handled;
3436   
3437   signal_handled = g_value_get_boolean (handler_return);
3438   g_value_set_boolean (return_accu, signal_handled);
3439   continue_emission = !signal_handled;
3440   
3441   return continue_emission;
3442 }
3443
3444 /* --- compile standard marshallers --- */
3445 #include "gmarshal.c"
3446
3447 #define __G_SIGNAL_C__
3448 #include "gobjectaliasdef.c"