1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
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.
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.
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.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
28 #include "gbsearcharray.h"
29 #include "gvaluecollector.h"
30 #include "gvaluetypes.h"
35 /* pre allocation configurations
37 #define MAX_STACK_VALUES (16)
38 #define HANDLER_PRE_ALLOC (48)
40 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
42 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
43 static volatile gpointer g_trace_instance_signals = NULL;
44 static volatile gpointer g_trap_instance_signals = NULL;
45 #endif /* G_ENABLE_DEBUG */
48 /* --- generic allocation --- */
49 /* we special case allocations generically by replacing
50 * these functions with more speed/memory aware variants
52 #ifndef DISABLE_MEM_POOLS
53 static inline gpointer
54 g_generic_node_alloc (GTrashStack **trash_stack_p,
56 guint nodes_pre_alloc)
58 gpointer node = g_trash_stack_pop (trash_stack_p);
64 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
65 block = g_malloc (sizeof_node * nodes_pre_alloc);
66 while (--nodes_pre_alloc)
68 g_trash_stack_push (trash_stack_p, block);
76 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
77 #else /* !DISABLE_MEM_POOLS */
78 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
79 #define g_generic_node_free(t,node) g_free (node)
80 #endif /* !DISABLE_MEM_POOLS */
83 /* --- typedefs --- */
84 typedef struct _SignalNode SignalNode;
85 typedef struct _SignalKey SignalKey;
86 typedef struct _Emission Emission;
87 typedef struct _Handler Handler;
88 typedef struct _HandlerList HandlerList;
89 typedef struct _HandlerMatch HandlerMatch;
99 /* --- prototypes --- */
100 static inline guint signal_id_lookup (GQuark quark,
102 static void signal_destroy_R (SignalNode *signal_node);
103 static inline HandlerList* handler_list_ensure (guint signal_id,
105 static inline HandlerList* handler_list_lookup (guint signal_id,
107 static inline Handler* handler_new (gboolean after);
108 static void handler_insert (guint signal_id,
111 static Handler* handler_lookup (gpointer instance,
114 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
117 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
119 static HandlerMatch* handlers_find (gpointer instance,
120 GSignalMatchType mask,
126 gboolean one_and_only);
127 static inline void handler_ref (Handler *handler);
128 static inline void handler_unref_R (guint signal_id,
131 static gint handler_lists_cmp (gconstpointer node1,
132 gconstpointer node2);
133 static inline void emission_push (Emission **emission_list_p,
135 static inline void emission_pop (Emission **emission_list_p,
137 static inline Emission* emission_find (Emission *emission_list,
141 static gint class_closures_cmp (gconstpointer node1,
142 gconstpointer node2);
143 static gint signal_key_cmp (gconstpointer node1,
144 gconstpointer node2);
145 static gboolean signal_emit_unlocked_R (SignalNode *node,
148 GValue *return_value,
149 const GValue *instance_and_params);
152 /* --- structures --- */
155 GSignalAccumulator func;
163 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
167 /* permanent portion */
173 /* reinitializable portion */
176 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
177 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
178 GBSearchArray *class_closure_bsa;
179 SignalAccumulator *accumulator;
180 GSignalCMarshaller c_marshaller;
181 GHookList *emission_hooks;
195 GSignalInvocationHint ihint;
207 gulong sequential_number;
211 guint ref_count : 16;
212 #define HANDLER_MAX_REF_COUNT (1 << 16)
213 guint block_count : 12;
214 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
230 GType instance_type; /* 0 for default closure */
235 /* --- variables --- */
236 static GBSearchArray *g_signal_key_bsa = NULL;
237 static GBSearchConfig g_signal_key_bconfig = G_STATIC_BCONFIG (sizeof (SignalKey),
239 G_BSEARCH_ARRAY_ALIGN_POWER2);
240 static GBSearchConfig g_signal_hlbsa_bconfig = G_STATIC_BCONFIG (sizeof (HandlerList),
242 G_BSEARCH_ARRAY_DEFER_SHRINK);
243 static GBSearchConfig g_class_closure_bconfig = G_STATIC_BCONFIG (sizeof (ClassClosure),
246 static GHashTable *g_handler_list_bsa_ht = NULL;
247 static Emission *g_recursive_emissions = NULL;
248 static Emission *g_restart_emissions = NULL;
249 static GTrashStack *g_handler_ts = NULL;
250 static gulong g_handler_sequential_number = 1;
251 G_LOCK_DEFINE_STATIC (g_signal_mutex);
252 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
253 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
256 /* --- signal nodes --- */
257 static guint g_n_signal_nodes = 0;
258 static SignalNode **g_signal_nodes = NULL;
260 static inline SignalNode*
261 LOOKUP_SIGNAL_NODE (register guint signal_id)
263 if (signal_id < g_n_signal_nodes)
264 return g_signal_nodes[signal_id];
270 /* --- functions --- */
272 signal_id_lookup (GQuark quark,
275 GType *ifaces, type = itype;
281 /* try looking up signals for this type and its anchestors */
284 SignalKey *signal_key;
287 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
290 return signal_key->signal_id;
292 type = g_type_parent (type);
296 /* no luck, try interfaces it exports */
297 ifaces = g_type_interfaces (itype, &n_ifaces);
300 SignalKey *signal_key;
302 key.itype = ifaces[n_ifaces];
303 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
308 return signal_key->signal_id;
317 class_closures_cmp (gconstpointer node1,
320 const ClassClosure *c1 = node1, *c2 = node2;
322 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
326 handler_lists_cmp (gconstpointer node1,
329 const HandlerList *hlist1 = node1, *hlist2 = node2;
331 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
334 static inline HandlerList*
335 handler_list_ensure (guint signal_id,
338 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
341 key.signal_id = signal_id;
345 hlbsa = g_bsearch_array_new (&g_signal_hlbsa_bconfig);
346 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key, FALSE);
347 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
351 GBSearchArray *o = hlbsa;
353 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key, FALSE);
355 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
357 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
360 static inline HandlerList*
361 handler_list_lookup (guint signal_id,
364 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
367 key.signal_id = signal_id;
369 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
373 handler_lookup (gpointer instance,
377 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
383 for (i = 0; i < hlbsa->n_nodes; i++)
385 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
388 for (handler = hlist->handlers; handler; handler = handler->next)
389 if (handler->sequential_number == handler_id)
392 *signal_id_p = hlist->signal_id;
402 static inline HandlerMatch*
403 handler_match_prepend (HandlerMatch *list,
409 /* yeah, we could use our own memchunk here, introducing yet more
410 * rarely used cached nodes and extra allocation overhead.
411 * instead, we use GList* nodes, since they are exactly the size
412 * we need and are already cached. g_signal_init() asserts this.
414 node = (HandlerMatch*) g_list_alloc ();
415 node->handler = handler;
417 node->d.signal_id = signal_id;
418 handler_ref (handler);
422 static inline HandlerMatch*
423 handler_match_free1_R (HandlerMatch *node,
426 HandlerMatch *next = node->next;
428 handler_unref_R (node->d.signal_id, instance, node->handler);
429 g_list_free_1 ((GList*) node);
435 handlers_find (gpointer instance,
436 GSignalMatchType mask,
442 gboolean one_and_only)
444 HandlerMatch *mlist = NULL;
446 if (mask & G_SIGNAL_MATCH_ID)
448 HandlerList *hlist = handler_list_lookup (signal_id, instance);
450 SignalNode *node = NULL;
452 if (mask & G_SIGNAL_MATCH_FUNC)
454 node = LOOKUP_SIGNAL_NODE (signal_id);
455 if (!node || !node->c_marshaller)
460 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
461 if (handler->sequential_number &&
462 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
463 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
464 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
465 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
466 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
467 handler->closure->meta_marshal == 0 &&
468 ((GCClosure*) handler->closure)->callback == func)))
470 mlist = handler_match_prepend (mlist, handler, signal_id);
477 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
484 for (i = 0; i < hlbsa->n_nodes; i++)
486 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
487 SignalNode *node = NULL;
490 if (!(mask & G_SIGNAL_MATCH_FUNC))
492 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
493 if (!node->c_marshaller)
497 for (handler = hlist->handlers; handler; handler = handler->next)
498 if (handler->sequential_number &&
499 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
500 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
501 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
502 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
503 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
504 handler->closure->meta_marshal == 0 &&
505 ((GCClosure*) handler->closure)->callback == func)))
507 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
518 static inline Handler*
519 handler_new (gboolean after)
521 Handler *handler = g_generic_node_alloc (&g_handler_ts,
524 #ifndef G_DISABLE_CHECKS
525 if (g_handler_sequential_number < 1)
526 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
529 handler->sequential_number = g_handler_sequential_number++;
530 handler->prev = NULL;
531 handler->next = NULL;
533 handler->ref_count = 1;
534 handler->block_count = 0;
535 handler->after = after != FALSE;
536 handler->closure = NULL;
542 handler_ref (Handler *handler)
544 g_return_if_fail (handler->ref_count > 0);
546 #ifndef G_DISABLE_CHECKS
547 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
548 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
551 handler->ref_count += 1;
555 handler_unref_R (guint signal_id,
559 g_return_if_fail (handler->ref_count > 0);
561 handler->ref_count -= 1;
562 if (!handler->ref_count)
565 handler->next->prev = handler->prev;
566 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
567 handler->prev->next = handler->next;
570 HandlerList *hlist = handler_list_lookup (signal_id, instance);
572 hlist->handlers = handler->next;
575 g_closure_unref (handler->closure);
577 g_generic_node_free (&g_handler_ts, handler);
582 handler_insert (guint signal_id,
588 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
590 hlist = handler_list_ensure (signal_id, instance);
591 if (!hlist->handlers)
592 hlist->handlers = handler;
593 else if (hlist->handlers->after && !handler->after)
595 handler->next = hlist->handlers;
596 hlist->handlers->prev = handler;
597 hlist->handlers = handler;
601 Handler *tmp = hlist->handlers;
607 while (tmp->next && !tmp->next->after)
610 tmp->next->prev = handler;
611 handler->next = tmp->next;
618 emission_push (Emission **emission_list_p,
621 emission->next = *emission_list_p;
622 *emission_list_p = emission;
626 emission_pop (Emission **emission_list_p,
629 Emission *node, *last = NULL;
631 for (node = *emission_list_p; node; last = node, node = last->next)
632 if (node == emission)
635 last->next = node->next;
637 *emission_list_p = node->next;
640 g_assert_not_reached ();
643 static inline Emission*
644 emission_find (Emission *emission_list,
651 for (emission = emission_list; emission; emission = emission->next)
652 if (emission->instance == instance &&
653 emission->ihint.signal_id == signal_id &&
654 emission->ihint.detail == detail)
659 static inline Emission*
660 emission_find_innermost (Emission *emission_list,
665 for (emission = emission_list; emission; emission = emission->next)
666 if (emission->instance == instance)
672 signal_key_cmp (gconstpointer node1,
675 const SignalKey *key1 = node1, *key2 = node2;
677 if (key1->itype == key2->itype)
678 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
680 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
684 g_signal_init (void) /* sync with gtype.c */
687 if (!g_n_signal_nodes)
689 /* handler_id_node_prepend() requires this */
690 g_assert (sizeof (GList) == sizeof (HandlerMatch));
692 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
693 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
694 g_signal_key_bsa = g_bsearch_array_new (&g_signal_key_bconfig);
696 /* invalid (0) signal_id */
697 g_n_signal_nodes = 1;
698 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
699 g_signal_nodes[0] = NULL;
705 _g_signals_destroy (GType itype)
710 for (i = 1; i < g_n_signal_nodes; i++)
712 SignalNode *node = g_signal_nodes[i];
714 if (node->itype == itype)
717 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
719 g_type_name (node->itype));
721 signal_destroy_R (node);
728 g_signal_stop_emission (gpointer instance,
734 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
735 g_return_if_fail (signal_id > 0);
738 node = LOOKUP_SIGNAL_NODE (signal_id);
739 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
741 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
745 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
747 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
748 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
752 if (emission->state == EMISSION_HOOK)
753 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
754 node->name, instance);
755 else if (emission->state == EMISSION_RUN)
756 emission->state = EMISSION_STOP;
759 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
760 node->name, instance);
763 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
768 signal_finalize_hook (GHookList *hook_list,
771 GDestroyNotify destroy = hook->destroy;
775 hook->destroy = NULL;
777 destroy (hook->data);
783 g_signal_add_emission_hook (guint signal_id,
785 GSignalEmissionHook hook_func,
787 GDestroyNotify data_destroy)
789 static gulong seq_hook_id = 1;
792 SignalHook *signal_hook;
794 g_return_val_if_fail (signal_id > 0, 0);
795 g_return_val_if_fail (hook_func != NULL, 0);
798 node = LOOKUP_SIGNAL_NODE (signal_id);
799 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
801 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
805 if (detail && !(node->flags & G_SIGNAL_DETAILED))
807 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
811 if (!node->emission_hooks)
813 node->emission_hooks = g_new (GHookList, 1);
814 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
815 node->emission_hooks->finalize_hook = signal_finalize_hook;
817 hook = g_hook_alloc (node->emission_hooks);
818 hook->data = hook_data;
819 hook->func = hook_func;
820 hook->destroy = data_destroy;
821 signal_hook = SIGNAL_HOOK (hook);
822 signal_hook->detail = detail;
823 node->emission_hooks->seq_id = seq_hook_id;
824 g_hook_append (node->emission_hooks, hook);
825 seq_hook_id = node->emission_hooks->seq_id;
828 return hook->hook_id;
832 g_signal_remove_emission_hook (guint signal_id,
837 g_return_if_fail (signal_id > 0);
838 g_return_if_fail (hook_id > 0);
841 node = LOOKUP_SIGNAL_NODE (signal_id);
842 if (!node || node->destroyed)
843 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
844 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
845 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
850 signal_parse_name (const gchar *name,
853 gboolean force_quark)
855 const gchar *colon = strchr (name, ':');
860 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
861 if (signal_id && detail_p)
864 else if (colon[1] == ':')
867 guint l = colon - name;
871 memcpy (buffer, name, l);
873 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
877 gchar *signal = g_new (gchar, l + 1);
879 memcpy (signal, name, l);
881 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
885 if (signal_id && detail_p)
886 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
894 g_signal_parse_name (const gchar *detailed_signal,
898 gboolean force_detail_quark)
904 g_return_val_if_fail (detailed_signal != NULL, FALSE);
905 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
908 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
911 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
912 if (!node || node->destroyed ||
913 (detail && !(node->flags & G_SIGNAL_DETAILED)))
917 *signal_id_p = signal_id;
925 g_signal_stop_emission_by_name (gpointer instance,
926 const gchar *detailed_signal)
932 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
933 g_return_if_fail (detailed_signal != NULL);
936 itype = G_TYPE_FROM_INSTANCE (instance);
937 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
940 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
942 if (detail && !(node->flags & G_SIGNAL_DETAILED))
943 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
944 else if (!g_type_is_a (itype, node->itype))
945 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
948 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
949 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
953 if (emission->state == EMISSION_HOOK)
954 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
955 node->name, instance);
956 else if (emission->state == EMISSION_RUN)
957 emission->state = EMISSION_STOP;
960 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
961 node->name, instance);
965 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
970 g_signal_lookup (const gchar *name,
975 g_return_val_if_fail (name != NULL, 0);
976 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
979 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
983 /* give elaborate warnings */
984 if (!g_type_name (itype))
985 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
987 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
988 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
989 name, g_type_name (itype));
990 else if (!g_type_class_peek (itype))
991 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
992 name, g_type_name (itype));
999 g_signal_list_ids (GType itype,
1007 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1008 g_return_val_if_fail (n_ids != NULL, NULL);
1011 keys = G_BSEARCH_ARRAY_NODES (&g_signal_key_bsa);
1012 n_nodes = g_signal_key_bsa->n_nodes;
1013 result = g_array_new (FALSE, FALSE, sizeof (guint));
1015 for (i = 0; i < n_nodes; i++)
1016 if (keys[i].itype == itype)
1018 const gchar *name = g_quark_to_string (keys[i].quark);
1020 /* Signal names with "_" in them are aliases to the same
1021 * name with "-" instead of "_".
1023 if (!strchr (name, '_'))
1024 g_array_append_val (result, keys[i].signal_id);
1026 *n_ids = result->len;
1030 /* give elaborate warnings */
1031 if (!g_type_name (itype))
1032 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1034 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1035 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1036 g_type_name (itype));
1037 else if (!g_type_class_peek (itype))
1038 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1039 g_type_name (itype));
1042 return (guint*) g_array_free (result, FALSE);
1045 G_CONST_RETURN gchar*
1046 g_signal_name (guint signal_id)
1052 node = LOOKUP_SIGNAL_NODE (signal_id);
1053 name = node ? node->name : NULL;
1060 g_signal_query (guint signal_id,
1061 GSignalQuery *query)
1065 g_return_if_fail (query != NULL);
1068 node = LOOKUP_SIGNAL_NODE (signal_id);
1069 if (!node || node->destroyed)
1070 query->signal_id = 0;
1073 query->signal_id = node->signal_id;
1074 query->signal_name = node->name;
1075 query->itype = node->itype;
1076 query->signal_flags = node->flags;
1077 query->return_type = node->return_type;
1078 query->n_params = node->n_params;
1079 query->param_types = node->param_types;
1085 g_signal_new (const gchar *signal_name,
1087 GSignalFlags signal_flags,
1089 GSignalAccumulator accumulator,
1091 GSignalCMarshaller c_marshaller,
1099 g_return_val_if_fail (signal_name != NULL, 0);
1101 va_start (args, n_params);
1103 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1104 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1105 accumulator, accu_data, c_marshaller,
1106 return_type, n_params, args);
1113 static inline ClassClosure*
1114 signal_find_class_closure (SignalNode *node,
1117 GBSearchArray *bsa = node->class_closure_bsa;
1124 /* cc->instance_type is 0 for default closure */
1126 key.instance_type = itype;
1127 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1128 while (!cc && key.instance_type)
1130 key.instance_type = g_type_parent (key.instance_type);
1131 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1139 static inline GClosure*
1140 signal_lookup_closure (SignalNode *node,
1141 GTypeInstance *instance)
1145 if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1146 cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1148 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1149 return cc ? cc->closure : NULL;
1153 signal_add_class_closure (SignalNode *node,
1159 if (!node->class_closure_bsa)
1160 node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1161 key.instance_type = itype;
1162 key.closure = g_closure_ref (closure);
1163 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1164 &g_class_closure_bconfig,
1167 g_closure_sink (closure);
1168 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1169 g_closure_set_marshal (closure, node->c_marshaller);
1173 g_signal_newv (const gchar *signal_name,
1175 GSignalFlags signal_flags,
1176 GClosure *class_closure,
1177 GSignalAccumulator accumulator,
1179 GSignalCMarshaller c_marshaller,
1188 g_return_val_if_fail (signal_name != NULL, 0);
1189 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1191 g_return_val_if_fail (param_types != NULL, 0);
1192 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1193 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1194 g_return_val_if_fail (accumulator == NULL, 0);
1196 g_return_val_if_fail (accu_data == NULL, 0);
1198 name = g_strdup (signal_name);
1199 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1203 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1204 node = LOOKUP_SIGNAL_NODE (signal_id);
1205 if (node && !node->destroyed)
1207 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1209 g_type_name (node->itype),
1210 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1215 if (node && node->itype != itype)
1217 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1219 g_type_name (itype),
1220 g_type_name (node->itype));
1225 for (i = 0; i < n_params; i++)
1226 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1228 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1229 i + 1, g_type_name (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1234 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1236 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1237 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1242 if (return_type != G_TYPE_NONE &&
1243 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1245 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1246 g_type_name (itype), name,
1247 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE));
1253 /* setup permanent portion of signal node */
1258 signal_id = g_n_signal_nodes++;
1259 node = g_new (SignalNode, 1);
1260 node->signal_id = signal_id;
1261 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1262 g_signal_nodes[signal_id] = node;
1263 node->itype = itype;
1266 key.quark = g_quark_from_string (node->name);
1267 key.signal_id = signal_id;
1268 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1269 g_strdelimit (node->name, "_", '-');
1270 key.quark = g_quark_from_static_string (node->name);
1271 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1273 node->destroyed = FALSE;
1275 /* setup reinitializable portion */
1276 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1277 node->n_params = n_params;
1278 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1279 node->return_type = return_type;
1280 node->class_closure_bsa = NULL;
1283 node->accumulator = g_new (SignalAccumulator, 1);
1284 node->accumulator->func = accumulator;
1285 node->accumulator->data = accu_data;
1288 node->accumulator = NULL;
1289 node->c_marshaller = c_marshaller;
1290 node->emission_hooks = NULL;
1292 signal_add_class_closure (node, 0, class_closure);
1299 g_signal_new_valist (const gchar *signal_name,
1301 GSignalFlags signal_flags,
1302 GClosure *class_closure,
1303 GSignalAccumulator accumulator,
1305 GSignalCMarshaller c_marshaller,
1316 param_types = g_new (GType, n_params);
1318 for (i = 0; i < n_params; i++)
1319 param_types[i] = va_arg (args, GType);
1324 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1325 class_closure, accumulator, accu_data, c_marshaller,
1326 return_type, n_params, param_types);
1327 g_free (param_types);
1333 signal_destroy_R (SignalNode *signal_node)
1335 SignalNode node = *signal_node;
1337 signal_node->destroyed = TRUE;
1339 /* reentrancy caution, zero out real contents first */
1340 signal_node->n_params = 0;
1341 signal_node->param_types = NULL;
1342 signal_node->return_type = 0;
1343 signal_node->class_closure_bsa = NULL;
1344 signal_node->accumulator = NULL;
1345 signal_node->c_marshaller = NULL;
1346 signal_node->emission_hooks = NULL;
1348 #ifdef G_ENABLE_DEBUG
1349 /* check current emissions */
1353 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1354 emission; emission = emission->next)
1355 if (emission->ihint.signal_id == node.signal_id)
1356 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1357 node.name, emission->instance);
1361 /* free contents that need to
1364 g_free (node.param_types);
1365 if (node.class_closure_bsa)
1369 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1371 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1373 g_closure_unref (cc->closure);
1375 g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1377 g_free (node.accumulator);
1378 if (node.emission_hooks)
1380 g_hook_list_clear (node.emission_hooks);
1381 g_free (node.emission_hooks);
1387 g_signal_override_class_closure (guint signal_id,
1388 GType instance_type,
1389 GClosure *class_closure)
1393 g_return_if_fail (signal_id > 0);
1394 g_return_if_fail (class_closure != NULL);
1397 node = LOOKUP_SIGNAL_NODE (signal_id);
1398 if (!g_type_is_a (instance_type, node->itype))
1399 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, g_type_name (instance_type), signal_id);
1402 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1404 if (cc && cc->instance_type == instance_type)
1405 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, g_type_name (instance_type), signal_id);
1407 signal_add_class_closure (node, instance_type, class_closure);
1413 g_signal_chain_from_overridden (const GValue *instance_and_params,
1415 GValue *return_value)
1417 GType chain_type = 0, restore_type = 0;
1418 Emission *emission = NULL;
1419 GClosure *closure = NULL;
1423 g_return_if_fail (instance_and_params != NULL);
1424 instance = g_value_peek_pointer (instance_and_params);
1425 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1426 g_return_if_fail (signal_id > 0);
1429 node = LOOKUP_SIGNAL_NODE (signal_id);
1430 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1432 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
1434 emission = emission_find_innermost (emission_list, instance);
1436 /* what we don't catch here is the scenario:
1437 * 1) signal1 being in class_closure, emitting signal2
1438 * 2) signal2 being in class closure, trying to chain signal1
1439 * 3) signal1&G_SIGNAL_NO_RECURSE != signal2&G_SIGNAL_NO_RECURSE.
1441 * also, it'd be a good idea to perform the same checks on parameters
1442 * as g_signal_emitv() here.
1444 if (emission && emission->ihint.signal_id == signal_id && emission->chain_type != G_TYPE_NONE)
1446 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1448 g_assert (cc != NULL); /* closure currently in call stack */
1450 restore_type = cc->instance_type;
1451 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1452 if (cc && cc->instance_type != restore_type)
1454 closure = cc->closure;
1455 chain_type = cc->instance_type;
1459 g_warning ("%s: signal id `%u' is not innermost class closure emission for instance `%p'", G_STRLOC, signal_id, instance);
1462 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1465 emission->chain_type = chain_type;
1467 g_closure_invoke (closure,
1470 instance_and_params,
1473 emission->chain_type = restore_type;
1479 g_signal_connect_closure_by_id (gpointer instance,
1486 gulong handler_seq_no = 0;
1488 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1489 g_return_val_if_fail (signal_id > 0, 0);
1490 g_return_val_if_fail (closure != NULL, 0);
1493 node = LOOKUP_SIGNAL_NODE (signal_id);
1496 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1497 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1498 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1499 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1502 Handler *handler = handler_new (after);
1504 handler_seq_no = handler->sequential_number;
1505 handler->detail = detail;
1506 handler->closure = g_closure_ref (closure);
1507 g_closure_sink (closure);
1508 handler_insert (signal_id, instance, handler);
1509 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1510 g_closure_set_marshal (closure, node->c_marshaller);
1514 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1517 return handler_seq_no;
1521 g_signal_connect_closure (gpointer instance,
1522 const gchar *detailed_signal,
1527 gulong handler_seq_no = 0;
1531 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1532 g_return_val_if_fail (detailed_signal != NULL, 0);
1533 g_return_val_if_fail (closure != NULL, 0);
1536 itype = G_TYPE_FROM_INSTANCE (instance);
1537 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1540 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1542 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1543 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1544 else if (!g_type_is_a (itype, node->itype))
1545 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1548 Handler *handler = handler_new (after);
1550 handler_seq_no = handler->sequential_number;
1551 handler->detail = detail;
1552 handler->closure = g_closure_ref (closure);
1553 g_closure_sink (closure);
1554 handler_insert (signal_id, instance, handler);
1555 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1556 g_closure_set_marshal (handler->closure, node->c_marshaller);
1560 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1563 return handler_seq_no;
1567 g_signal_connect_data (gpointer instance,
1568 const gchar *detailed_signal,
1569 GCallback c_handler,
1571 GClosureNotify destroy_data,
1572 GConnectFlags connect_flags)
1575 gulong handler_seq_no = 0;
1578 gboolean swapped, after;
1580 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1581 g_return_val_if_fail (detailed_signal != NULL, 0);
1582 g_return_val_if_fail (c_handler != NULL, 0);
1584 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1585 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1588 itype = G_TYPE_FROM_INSTANCE (instance);
1589 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1592 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1594 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1595 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1596 else if (!g_type_is_a (itype, node->itype))
1597 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1600 Handler *handler = handler_new (after);
1602 handler_seq_no = handler->sequential_number;
1603 handler->detail = detail;
1604 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1605 g_closure_sink (handler->closure);
1606 handler_insert (signal_id, instance, handler);
1607 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1608 g_closure_set_marshal (handler->closure, node->c_marshaller);
1612 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1615 return handler_seq_no;
1619 g_signal_handler_block (gpointer instance,
1624 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1625 g_return_if_fail (handler_id > 0);
1628 handler = handler_lookup (instance, handler_id, NULL);
1631 #ifndef G_DISABLE_CHECKS
1632 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1633 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1635 handler->block_count += 1;
1638 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1643 g_signal_handler_unblock (gpointer instance,
1648 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1649 g_return_if_fail (handler_id > 0);
1652 handler = handler_lookup (instance, handler_id, NULL);
1655 if (handler->block_count)
1656 handler->block_count -= 1;
1658 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1661 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1666 g_signal_handler_disconnect (gpointer instance,
1672 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1673 g_return_if_fail (handler_id > 0);
1676 handler = handler_lookup (instance, handler_id, &signal_id);
1679 handler->sequential_number = 0;
1680 handler->block_count = 1;
1681 handler_unref_R (signal_id, instance, handler);
1684 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1689 g_signal_handler_is_connected (gpointer instance,
1695 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1696 g_return_val_if_fail (handler_id > 0, FALSE);
1699 handler = handler_lookup (instance, handler_id, NULL);
1700 connected = handler != NULL;
1707 g_signal_handlers_destroy (gpointer instance)
1709 GBSearchArray *hlbsa;
1711 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1714 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1719 /* reentrancy caution, delete instance trace first */
1720 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1722 for (i = 0; i < hlbsa->n_nodes; i++)
1724 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1725 Handler *handler = hlist->handlers;
1729 Handler *tmp = handler;
1731 handler = tmp->next;
1732 tmp->block_count = 1;
1733 /* cruel unlink, this works because _all_ handlers vanish */
1736 if (tmp->sequential_number)
1738 tmp->sequential_number = 0;
1739 handler_unref_R (0, NULL, tmp);
1743 g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
1749 g_signal_handler_find (gpointer instance,
1750 GSignalMatchType mask,
1757 gulong handler_seq_no = 0;
1759 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1760 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1762 if (mask & G_SIGNAL_MATCH_MASK)
1764 HandlerMatch *mlist;
1767 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1770 handler_seq_no = mlist->handler->sequential_number;
1771 handler_match_free1_R (mlist, instance);
1776 return handler_seq_no;
1780 signal_handlers_foreach_matched_R (gpointer instance,
1781 GSignalMatchType mask,
1787 void (*callback) (gpointer instance,
1788 gulong handler_seq_no))
1790 HandlerMatch *mlist;
1791 guint n_handlers = 0;
1793 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1797 if (mlist->handler->sequential_number)
1800 callback (instance, mlist->handler->sequential_number);
1803 mlist = handler_match_free1_R (mlist, instance);
1810 g_signal_handlers_block_matched (gpointer instance,
1811 GSignalMatchType mask,
1818 guint n_handlers = 0;
1820 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1821 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1823 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1826 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1827 closure, func, data,
1828 g_signal_handler_block);
1836 g_signal_handlers_unblock_matched (gpointer instance,
1837 GSignalMatchType mask,
1844 guint n_handlers = 0;
1846 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1847 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1849 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1852 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1853 closure, func, data,
1854 g_signal_handler_unblock);
1862 g_signal_handlers_disconnect_matched (gpointer instance,
1863 GSignalMatchType mask,
1870 guint n_handlers = 0;
1872 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1873 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1875 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1878 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1879 closure, func, data,
1880 g_signal_handler_disconnect);
1888 g_signal_has_handler_pending (gpointer instance,
1891 gboolean may_be_blocked)
1893 HandlerMatch *mlist;
1894 gboolean has_pending;
1896 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1897 g_return_val_if_fail (signal_id > 0, FALSE);
1902 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1904 if (!(node->flags & G_SIGNAL_DETAILED))
1906 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1911 mlist = handlers_find (instance,
1912 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1913 signal_id, detail, NULL, NULL, NULL, TRUE);
1917 handler_match_free1_R (mlist, instance);
1920 has_pending = FALSE;
1927 g_signal_emitv (const GValue *instance_and_params,
1930 GValue *return_value)
1932 const GValue *param_values;
1935 #ifdef G_ENABLE_DEBUG
1939 g_return_if_fail (instance_and_params != NULL);
1940 instance = g_value_peek_pointer (instance_and_params);
1941 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1942 g_return_if_fail (signal_id > 0);
1944 param_values = instance_and_params + 1;
1947 node = LOOKUP_SIGNAL_NODE (signal_id);
1948 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1950 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1954 #ifdef G_ENABLE_DEBUG
1955 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1957 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1961 for (i = 0; i < node->n_params; i++)
1962 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1964 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1966 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1969 G_VALUE_TYPE_NAME (param_values + i));
1973 if (node->return_type != G_TYPE_NONE)
1977 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1979 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1984 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1986 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1988 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1990 G_VALUE_TYPE_NAME (return_value));
1996 return_value = NULL;
1997 #endif /* G_ENABLE_DEBUG */
2000 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2004 g_signal_emit_valist (gpointer instance,
2009 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2010 GType signal_return_type;
2011 GValue *param_values;
2015 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2016 g_return_if_fail (signal_id > 0);
2019 node = LOOKUP_SIGNAL_NODE (signal_id);
2020 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2022 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2026 #ifndef G_DISABLE_CHECKS
2027 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2029 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2033 #endif /* !G_DISABLE_CHECKS */
2035 n_params = node->n_params;
2036 signal_return_type = node->return_type;
2037 if (node->n_params < MAX_STACK_VALUES)
2038 instance_and_params = stack_values;
2041 free_me = g_new (GValue, node->n_params + 1);
2042 instance_and_params = free_me;
2044 param_values = instance_and_params + 1;
2045 for (i = 0; i < node->n_params; i++)
2048 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2049 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2051 param_values[i].g_type = 0;
2053 g_value_init (param_values + i, ptype);
2054 G_VALUE_COLLECT (param_values + i,
2056 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2060 g_warning ("%s: %s", G_STRLOC, error);
2063 /* we purposely leak the value here, it might not be
2064 * in a sane state if an error condition occoured
2067 g_value_unset (param_values + i);
2075 instance_and_params->g_type = 0;
2076 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2077 g_value_set_instance (instance_and_params, instance);
2078 if (signal_return_type == G_TYPE_NONE)
2079 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2082 GValue return_value = { 0, };
2083 gchar *error = NULL;
2084 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2085 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2087 g_value_init (&return_value, rtype);
2089 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2091 G_VALUE_LCOPY (&return_value,
2093 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2096 g_value_unset (&return_value);
2099 g_warning ("%s: %s", G_STRLOC, error);
2102 /* we purposely leak the value here, it might not be
2103 * in a sane state if an error condition occoured
2107 for (i = 0; i < n_params; i++)
2108 g_value_unset (param_values + i);
2109 g_value_unset (instance_and_params);
2115 g_signal_emit (gpointer instance,
2122 va_start (var_args, detail);
2123 g_signal_emit_valist (instance, signal_id, detail, var_args);
2128 g_signal_emit_by_name (gpointer instance,
2129 const gchar *detailed_signal,
2135 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2136 g_return_if_fail (detailed_signal != NULL);
2139 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2146 va_start (var_args, detailed_signal);
2147 g_signal_emit_valist (instance, signal_id, detail, var_args);
2151 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2154 static inline gboolean
2155 accumulate (GSignalInvocationHint *ihint,
2156 GValue *return_accu,
2157 GValue *handler_return,
2158 SignalAccumulator *accumulator)
2160 gboolean continue_emission;
2165 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2166 g_value_reset (handler_return);
2168 return continue_emission;
2172 signal_emit_unlocked_R (SignalNode *node,
2175 GValue *emission_return,
2176 const GValue *instance_and_params)
2178 SignalAccumulator *accumulator;
2180 GClosure *class_closure;
2182 Handler *handler_list = NULL;
2183 GValue *return_accu, accu = { 0, };
2185 gulong max_sequential_handler_number;
2186 gboolean return_value_altered = FALSE;
2188 #ifdef G_ENABLE_DEBUG
2189 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2191 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2192 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2195 if (g_trap_instance_signals == instance)
2198 #endif /* G_ENABLE_DEBUG */
2201 signal_id = node->signal_id;
2202 if (node->flags & G_SIGNAL_NO_RECURSE)
2204 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2208 node->state = EMISSION_RESTART;
2210 return return_value_altered;
2213 accumulator = node->accumulator;
2217 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2218 return_accu = &accu;
2222 return_accu = emission_return;
2223 emission.instance = instance;
2224 emission.ihint.signal_id = node->signal_id;
2225 emission.ihint.detail = detail;
2226 emission.ihint.run_type = 0;
2228 emission.chain_type = G_TYPE_NONE;
2229 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2230 class_closure = signal_lookup_closure (node, instance);
2235 handler_unref_R (signal_id, instance, handler_list);
2236 max_sequential_handler_number = g_handler_sequential_number;
2237 hlist = handler_list_lookup (signal_id, instance);
2238 handler_list = hlist ? hlist->handlers : NULL;
2240 handler_ref (handler_list);
2242 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2244 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2246 emission.state = EMISSION_RUN;
2248 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2250 g_closure_invoke (class_closure,
2253 instance_and_params,
2255 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2256 emission.state == EMISSION_RUN)
2257 emission.state = EMISSION_STOP;
2259 emission.chain_type = G_TYPE_NONE;
2260 return_value_altered = TRUE;
2262 if (emission.state == EMISSION_STOP)
2264 else if (emission.state == EMISSION_RESTART)
2268 if (node->emission_hooks)
2270 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2273 emission.state = EMISSION_HOOK;
2274 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2277 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2279 if (!signal_hook->detail || signal_hook->detail == detail)
2281 GSignalEmissionHook hook_func = hook->func;
2283 was_in_call = G_HOOK_IN_CALL (hook);
2284 hook->flags |= G_HOOK_FLAG_IN_CALL;
2286 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2289 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2291 g_hook_destroy_link (node->emission_hooks, hook);
2293 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2296 if (emission.state == EMISSION_RESTART)
2302 Handler *handler = handler_list;
2304 emission.state = EMISSION_RUN;
2305 handler_ref (handler);
2312 handler_unref_R (signal_id, instance, handler_list);
2313 handler_list = handler;
2316 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2317 handler->sequential_number < max_sequential_handler_number)
2320 g_closure_invoke (handler->closure,
2323 instance_and_params,
2325 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2326 emission.state == EMISSION_RUN)
2327 emission.state = EMISSION_STOP;
2329 return_value_altered = TRUE;
2331 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2334 tmp = handler->next;
2338 handler_unref_R (signal_id, instance, handler_list);
2339 handler_list = handler;
2344 if (emission.state == EMISSION_STOP)
2346 else if (emission.state == EMISSION_RESTART)
2350 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2352 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2354 emission.state = EMISSION_RUN;
2356 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2358 g_closure_invoke (class_closure,
2361 instance_and_params,
2363 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2364 emission.state == EMISSION_RUN)
2365 emission.state = EMISSION_STOP;
2367 emission.chain_type = G_TYPE_NONE;
2368 return_value_altered = TRUE;
2370 if (emission.state == EMISSION_STOP)
2372 else if (emission.state == EMISSION_RESTART)
2378 Handler *handler = handler_list;
2380 emission.state = EMISSION_RUN;
2381 handler_ref (handler);
2386 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2387 handler->sequential_number < max_sequential_handler_number)
2390 g_closure_invoke (handler->closure,
2393 instance_and_params,
2395 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2396 emission.state == EMISSION_RUN)
2397 emission.state = EMISSION_STOP;
2399 return_value_altered = TRUE;
2401 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2404 tmp = handler->next;
2408 handler_unref_R (signal_id, instance, handler);
2413 if (emission.state == EMISSION_STOP)
2415 else if (emission.state == EMISSION_RESTART)
2421 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2423 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2425 gboolean need_unset = FALSE;
2427 emission.state = EMISSION_STOP;
2429 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2431 if (node->return_type != G_TYPE_NONE && !accumulator)
2433 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2436 g_closure_invoke (class_closure,
2437 node->return_type != G_TYPE_NONE ? &accu : NULL,
2439 instance_and_params,
2442 g_value_unset (&accu);
2444 emission.chain_type = G_TYPE_NONE;
2446 if (emission.state == EMISSION_RESTART)
2451 handler_unref_R (signal_id, instance, handler_list);
2453 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2456 g_value_unset (&accu);
2458 return return_value_altered;
2462 /* --- compile standard marshallers --- */
2463 #include "gobject.h"
2465 #include "gmarshal.c"