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