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>
29 #include "gobjectalias.h"
31 #include "gbsearcharray.h"
32 #include "gvaluecollector.h"
33 #include "gvaluetypes.h"
39 /* pre allocation configurations
41 #define MAX_STACK_VALUES (16)
42 #define HANDLER_PRE_ALLOC (48)
44 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
46 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
47 static volatile gpointer g_trace_instance_signals = NULL;
48 static volatile gpointer g_trap_instance_signals = NULL;
49 #endif /* G_ENABLE_DEBUG */
52 /* --- generic allocation --- */
53 /* we special case allocations generically by replacing
54 * these functions with more speed/memory aware variants
56 #ifndef DISABLE_MEM_POOLS
57 static inline gpointer
58 g_generic_node_alloc (GTrashStack **trash_stack_p,
60 guint nodes_pre_alloc)
62 gpointer node = g_trash_stack_pop (trash_stack_p);
68 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
69 block = g_malloc (sizeof_node * nodes_pre_alloc);
70 while (--nodes_pre_alloc)
72 g_trash_stack_push (trash_stack_p, block);
80 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
81 #else /* !DISABLE_MEM_POOLS */
82 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
83 #define g_generic_node_free(t,node) g_free (node)
84 #endif /* !DISABLE_MEM_POOLS */
87 /* --- typedefs --- */
88 typedef struct _SignalNode SignalNode;
89 typedef struct _SignalKey SignalKey;
90 typedef struct _Emission Emission;
91 typedef struct _Handler Handler;
92 typedef struct _HandlerList HandlerList;
93 typedef struct _HandlerMatch HandlerMatch;
103 /* --- prototypes --- */
104 static inline guint signal_id_lookup (GQuark quark,
106 static void signal_destroy_R (SignalNode *signal_node);
107 static inline HandlerList* handler_list_ensure (guint signal_id,
109 static inline HandlerList* handler_list_lookup (guint signal_id,
111 static inline Handler* handler_new (gboolean after);
112 static void handler_insert (guint signal_id,
115 static Handler* handler_lookup (gpointer instance,
118 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
121 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
123 static HandlerMatch* handlers_find (gpointer instance,
124 GSignalMatchType mask,
130 gboolean one_and_only);
131 static inline void handler_ref (Handler *handler);
132 static inline void handler_unref_R (guint signal_id,
135 static gint handler_lists_cmp (gconstpointer node1,
136 gconstpointer node2);
137 static inline void emission_push (Emission **emission_list_p,
139 static inline void emission_pop (Emission **emission_list_p,
141 static inline Emission* emission_find (Emission *emission_list,
145 static gint class_closures_cmp (gconstpointer node1,
146 gconstpointer node2);
147 static gint signal_key_cmp (gconstpointer node1,
148 gconstpointer node2);
149 static gboolean signal_emit_unlocked_R (SignalNode *node,
152 GValue *return_value,
153 const GValue *instance_and_params);
154 static const gchar * type_debug_name (GType type);
157 /* --- structures --- */
160 GSignalAccumulator func;
168 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
172 /* permanent portion */
178 /* reinitializable portion */
179 guint test_class_offset : 12;
182 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
183 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
184 GBSearchArray *class_closure_bsa;
185 SignalAccumulator *accumulator;
186 GSignalCMarshaller c_marshaller;
187 GHookList *emission_hooks;
189 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
190 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
203 GSignalInvocationHint ihint;
215 gulong sequential_number;
219 guint ref_count : 16;
220 #define HANDLER_MAX_REF_COUNT (1 << 16)
221 guint block_count : 12;
222 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
238 GType instance_type; /* 0 for default closure */
243 /* --- variables --- */
244 static GBSearchArray *g_signal_key_bsa = NULL;
245 static const GBSearchConfig g_signal_key_bconfig = {
248 G_BSEARCH_ARRAY_ALIGN_POWER2,
250 static GBSearchConfig g_signal_hlbsa_bconfig = {
251 sizeof (HandlerList),
255 static GBSearchConfig g_class_closure_bconfig = {
256 sizeof (ClassClosure),
260 static GHashTable *g_handler_list_bsa_ht = NULL;
261 static Emission *g_recursive_emissions = NULL;
262 static Emission *g_restart_emissions = NULL;
263 #ifndef DISABLE_MEM_POOLS
264 static GTrashStack *g_handler_ts = NULL;
266 static gulong g_handler_sequential_number = 1;
267 G_LOCK_DEFINE_STATIC (g_signal_mutex);
268 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
269 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
272 /* --- signal nodes --- */
273 static guint g_n_signal_nodes = 0;
274 static SignalNode **g_signal_nodes = NULL;
276 static inline SignalNode*
277 LOOKUP_SIGNAL_NODE (register guint signal_id)
279 if (signal_id < g_n_signal_nodes)
280 return g_signal_nodes[signal_id];
286 /* --- functions --- */
288 signal_id_lookup (GQuark quark,
291 GType *ifaces, type = itype;
297 /* try looking up signals for this type and its ancestors */
300 SignalKey *signal_key;
303 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
306 return signal_key->signal_id;
308 type = g_type_parent (type);
312 /* no luck, try interfaces it exports */
313 ifaces = g_type_interfaces (itype, &n_ifaces);
316 SignalKey *signal_key;
318 key.itype = ifaces[n_ifaces];
319 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
324 return signal_key->signal_id;
333 class_closures_cmp (gconstpointer node1,
336 const ClassClosure *c1 = node1, *c2 = node2;
338 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
342 handler_lists_cmp (gconstpointer node1,
345 const HandlerList *hlist1 = node1, *hlist2 = node2;
347 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
350 static inline HandlerList*
351 handler_list_ensure (guint signal_id,
354 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
357 key.signal_id = signal_id;
361 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
362 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
363 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
367 GBSearchArray *o = hlbsa;
369 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
371 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
373 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
376 static inline HandlerList*
377 handler_list_lookup (guint signal_id,
380 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
383 key.signal_id = signal_id;
385 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
389 handler_lookup (gpointer instance,
393 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
399 for (i = 0; i < hlbsa->n_nodes; i++)
401 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
404 for (handler = hlist->handlers; handler; handler = handler->next)
405 if (handler->sequential_number == handler_id)
408 *signal_id_p = hlist->signal_id;
418 static inline HandlerMatch*
419 handler_match_prepend (HandlerMatch *list,
425 /* yeah, we could use our own memchunk here, introducing yet more
426 * rarely used cached nodes and extra allocation overhead.
427 * instead, we use GList* nodes, since they are exactly the size
428 * we need and are already cached. g_signal_init() asserts this.
430 node = (HandlerMatch*) g_list_alloc ();
431 node->handler = handler;
433 node->d.signal_id = signal_id;
434 handler_ref (handler);
438 static inline HandlerMatch*
439 handler_match_free1_R (HandlerMatch *node,
442 HandlerMatch *next = node->next;
444 handler_unref_R (node->d.signal_id, instance, node->handler);
445 g_list_free_1 ((GList*) node);
451 handlers_find (gpointer instance,
452 GSignalMatchType mask,
458 gboolean one_and_only)
460 HandlerMatch *mlist = NULL;
462 if (mask & G_SIGNAL_MATCH_ID)
464 HandlerList *hlist = handler_list_lookup (signal_id, instance);
466 SignalNode *node = NULL;
468 if (mask & G_SIGNAL_MATCH_FUNC)
470 node = LOOKUP_SIGNAL_NODE (signal_id);
471 if (!node || !node->c_marshaller)
476 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
477 if (handler->sequential_number &&
478 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
479 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
480 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
481 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
482 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
483 handler->closure->meta_marshal == 0 &&
484 ((GCClosure*) handler->closure)->callback == func)))
486 mlist = handler_match_prepend (mlist, handler, signal_id);
493 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
500 for (i = 0; i < hlbsa->n_nodes; i++)
502 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
503 SignalNode *node = NULL;
506 if (!(mask & G_SIGNAL_MATCH_FUNC))
508 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
509 if (!node->c_marshaller)
513 for (handler = hlist->handlers; handler; handler = handler->next)
514 if (handler->sequential_number &&
515 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
516 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
517 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
518 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
519 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
520 handler->closure->meta_marshal == 0 &&
521 ((GCClosure*) handler->closure)->callback == func)))
523 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
534 static inline Handler*
535 handler_new (gboolean after)
537 Handler *handler = g_generic_node_alloc (&g_handler_ts,
540 #ifndef G_DISABLE_CHECKS
541 if (g_handler_sequential_number < 1)
542 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
545 handler->sequential_number = g_handler_sequential_number++;
546 handler->prev = NULL;
547 handler->next = NULL;
549 handler->ref_count = 1;
550 handler->block_count = 0;
551 handler->after = after != FALSE;
552 handler->closure = NULL;
558 handler_ref (Handler *handler)
560 g_return_if_fail (handler->ref_count > 0);
562 #ifndef G_DISABLE_CHECKS
563 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
564 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
567 handler->ref_count += 1;
571 handler_unref_R (guint signal_id,
575 g_return_if_fail (handler->ref_count > 0);
577 handler->ref_count -= 1;
578 if (!handler->ref_count)
581 handler->next->prev = handler->prev;
582 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
583 handler->prev->next = handler->next;
586 HandlerList *hlist = handler_list_lookup (signal_id, instance);
588 hlist->handlers = handler->next;
591 g_closure_unref (handler->closure);
593 g_generic_node_free (&g_handler_ts, handler);
598 handler_insert (guint signal_id,
604 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
606 hlist = handler_list_ensure (signal_id, instance);
607 if (!hlist->handlers)
608 hlist->handlers = handler;
609 else if (hlist->handlers->after && !handler->after)
611 handler->next = hlist->handlers;
612 hlist->handlers->prev = handler;
613 hlist->handlers = handler;
617 Handler *tmp = hlist->handlers;
623 while (tmp->next && !tmp->next->after)
626 tmp->next->prev = handler;
627 handler->next = tmp->next;
634 emission_push (Emission **emission_list_p,
637 emission->next = *emission_list_p;
638 *emission_list_p = emission;
642 emission_pop (Emission **emission_list_p,
645 Emission *node, *last = NULL;
647 for (node = *emission_list_p; node; last = node, node = last->next)
648 if (node == emission)
651 last->next = node->next;
653 *emission_list_p = node->next;
656 g_assert_not_reached ();
659 static inline Emission*
660 emission_find (Emission *emission_list,
667 for (emission = emission_list; emission; emission = emission->next)
668 if (emission->instance == instance &&
669 emission->ihint.signal_id == signal_id &&
670 emission->ihint.detail == detail)
675 static inline Emission*
676 emission_find_innermost (gpointer instance)
678 Emission *emission, *s = NULL, *c = NULL;
680 for (emission = g_restart_emissions; emission; emission = emission->next)
681 if (emission->instance == instance)
686 for (emission = g_recursive_emissions; emission; emission = emission->next)
687 if (emission->instance == instance)
697 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
701 signal_key_cmp (gconstpointer node1,
704 const SignalKey *key1 = node1, *key2 = node2;
706 if (key1->itype == key2->itype)
707 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
709 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
716 if (!g_n_signal_nodes)
718 /* handler_id_node_prepend() requires this */
719 g_assert (sizeof (GList) == sizeof (HandlerMatch));
721 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
722 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
723 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
725 /* invalid (0) signal_id */
726 g_n_signal_nodes = 1;
727 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
728 g_signal_nodes[0] = NULL;
734 _g_signals_destroy (GType itype)
739 for (i = 1; i < g_n_signal_nodes; i++)
741 SignalNode *node = g_signal_nodes[i];
743 if (node->itype == itype)
746 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
748 type_debug_name (node->itype));
750 signal_destroy_R (node);
757 g_signal_stop_emission (gpointer instance,
763 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
764 g_return_if_fail (signal_id > 0);
767 node = LOOKUP_SIGNAL_NODE (signal_id);
768 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
770 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
774 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
776 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
777 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
781 if (emission->state == EMISSION_HOOK)
782 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
783 node->name, instance);
784 else if (emission->state == EMISSION_RUN)
785 emission->state = EMISSION_STOP;
788 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
789 node->name, instance);
792 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
797 signal_finalize_hook (GHookList *hook_list,
800 GDestroyNotify destroy = hook->destroy;
804 hook->destroy = NULL;
806 destroy (hook->data);
812 g_signal_add_emission_hook (guint signal_id,
814 GSignalEmissionHook hook_func,
816 GDestroyNotify data_destroy)
818 static gulong seq_hook_id = 1;
821 SignalHook *signal_hook;
823 g_return_val_if_fail (signal_id > 0, 0);
824 g_return_val_if_fail (hook_func != NULL, 0);
827 node = LOOKUP_SIGNAL_NODE (signal_id);
828 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
830 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
834 if (detail && !(node->flags & G_SIGNAL_DETAILED))
836 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
840 if (!node->emission_hooks)
842 node->emission_hooks = g_new (GHookList, 1);
843 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
844 node->emission_hooks->finalize_hook = signal_finalize_hook;
846 hook = g_hook_alloc (node->emission_hooks);
847 hook->data = hook_data;
848 hook->func = (gpointer) hook_func;
849 hook->destroy = data_destroy;
850 signal_hook = SIGNAL_HOOK (hook);
851 signal_hook->detail = detail;
852 node->emission_hooks->seq_id = seq_hook_id;
853 g_hook_append (node->emission_hooks, hook);
854 seq_hook_id = node->emission_hooks->seq_id;
857 return hook->hook_id;
861 g_signal_remove_emission_hook (guint signal_id,
866 g_return_if_fail (signal_id > 0);
867 g_return_if_fail (hook_id > 0);
870 node = LOOKUP_SIGNAL_NODE (signal_id);
871 if (!node || node->destroyed)
872 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
873 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
874 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
879 signal_parse_name (const gchar *name,
882 gboolean force_quark)
884 const gchar *colon = strchr (name, ':');
889 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
890 if (signal_id && detail_p)
893 else if (colon[1] == ':')
896 guint l = colon - name;
900 memcpy (buffer, name, l);
902 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
906 gchar *signal = g_new (gchar, l + 1);
908 memcpy (signal, name, l);
910 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
914 if (signal_id && detail_p)
915 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
923 g_signal_parse_name (const gchar *detailed_signal,
927 gboolean force_detail_quark)
933 g_return_val_if_fail (detailed_signal != NULL, FALSE);
934 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
937 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
940 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
941 if (!node || node->destroyed ||
942 (detail && !(node->flags & G_SIGNAL_DETAILED)))
946 *signal_id_p = signal_id;
954 g_signal_stop_emission_by_name (gpointer instance,
955 const gchar *detailed_signal)
961 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
962 g_return_if_fail (detailed_signal != NULL);
965 itype = G_TYPE_FROM_INSTANCE (instance);
966 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
969 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
971 if (detail && !(node->flags & G_SIGNAL_DETAILED))
972 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
973 else if (!g_type_is_a (itype, node->itype))
974 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
977 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
978 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
982 if (emission->state == EMISSION_HOOK)
983 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
984 node->name, instance);
985 else if (emission->state == EMISSION_RUN)
986 emission->state = EMISSION_STOP;
989 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
990 node->name, instance);
994 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
999 g_signal_lookup (const gchar *name,
1003 g_return_val_if_fail (name != NULL, 0);
1004 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1007 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1011 /* give elaborate warnings */
1012 if (!g_type_name (itype))
1013 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1015 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1016 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1017 name, g_type_name (itype));
1018 else if (!g_type_class_peek (itype))
1019 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1020 name, g_type_name (itype));
1027 g_signal_list_ids (GType itype,
1035 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1036 g_return_val_if_fail (n_ids != NULL, NULL);
1039 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1040 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1041 result = g_array_new (FALSE, FALSE, sizeof (guint));
1043 for (i = 0; i < n_nodes; i++)
1044 if (keys[i].itype == itype)
1046 const gchar *name = g_quark_to_string (keys[i].quark);
1048 /* Signal names with "_" in them are aliases to the same
1049 * name with "-" instead of "_".
1051 if (!strchr (name, '_'))
1052 g_array_append_val (result, keys[i].signal_id);
1054 *n_ids = result->len;
1058 /* give elaborate warnings */
1059 if (!g_type_name (itype))
1060 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1062 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1063 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1064 g_type_name (itype));
1065 else if (!g_type_class_peek (itype))
1066 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1067 g_type_name (itype));
1070 return (guint*) g_array_free (result, FALSE);
1073 G_CONST_RETURN gchar*
1074 g_signal_name (guint signal_id)
1080 node = LOOKUP_SIGNAL_NODE (signal_id);
1081 name = node ? node->name : NULL;
1088 g_signal_query (guint signal_id,
1089 GSignalQuery *query)
1093 g_return_if_fail (query != NULL);
1096 node = LOOKUP_SIGNAL_NODE (signal_id);
1097 if (!node || node->destroyed)
1098 query->signal_id = 0;
1101 query->signal_id = node->signal_id;
1102 query->signal_name = node->name;
1103 query->itype = node->itype;
1104 query->signal_flags = node->flags;
1105 query->return_type = node->return_type;
1106 query->n_params = node->n_params;
1107 query->param_types = node->param_types;
1113 g_signal_new (const gchar *signal_name,
1115 GSignalFlags signal_flags,
1117 GSignalAccumulator accumulator,
1119 GSignalCMarshaller c_marshaller,
1127 g_return_val_if_fail (signal_name != NULL, 0);
1129 va_start (args, n_params);
1131 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1132 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1133 accumulator, accu_data, c_marshaller,
1134 return_type, n_params, args);
1138 /* optimize NOP emissions with NULL class handlers */
1139 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1140 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1145 node = LOOKUP_SIGNAL_NODE (signal_id);
1146 node->test_class_offset = class_offset;
1153 static inline ClassClosure*
1154 signal_find_class_closure (SignalNode *node,
1157 GBSearchArray *bsa = node->class_closure_bsa;
1164 /* cc->instance_type is 0 for default closure */
1166 key.instance_type = itype;
1167 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1168 while (!cc && key.instance_type)
1170 key.instance_type = g_type_parent (key.instance_type);
1171 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1179 static inline GClosure*
1180 signal_lookup_closure (SignalNode *node,
1181 GTypeInstance *instance)
1185 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1186 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1188 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1189 return cc ? cc->closure : NULL;
1193 signal_add_class_closure (SignalNode *node,
1199 /* can't optimize NOP emissions with overridden class closures */
1200 node->test_class_offset = 0;
1202 if (!node->class_closure_bsa)
1203 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1204 key.instance_type = itype;
1205 key.closure = g_closure_ref (closure);
1206 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1207 &g_class_closure_bconfig,
1209 g_closure_sink (closure);
1210 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1211 g_closure_set_marshal (closure, node->c_marshaller);
1215 g_signal_newv (const gchar *signal_name,
1217 GSignalFlags signal_flags,
1218 GClosure *class_closure,
1219 GSignalAccumulator accumulator,
1221 GSignalCMarshaller c_marshaller,
1230 g_return_val_if_fail (signal_name != NULL, 0);
1231 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1233 g_return_val_if_fail (param_types != NULL, 0);
1234 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1235 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1236 g_return_val_if_fail (accumulator == NULL, 0);
1238 g_return_val_if_fail (accu_data == NULL, 0);
1240 name = g_strdup (signal_name);
1241 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1245 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1246 node = LOOKUP_SIGNAL_NODE (signal_id);
1247 if (node && !node->destroyed)
1249 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1251 type_debug_name (node->itype),
1252 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1257 if (node && node->itype != itype)
1259 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1261 type_debug_name (itype),
1262 type_debug_name (node->itype));
1267 for (i = 0; i < n_params; i++)
1268 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1270 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1271 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1276 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1278 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1279 type_debug_name (return_type), type_debug_name (itype), name);
1284 if (return_type != G_TYPE_NONE &&
1285 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1287 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1288 type_debug_name (itype), name, type_debug_name (return_type));
1294 /* setup permanent portion of signal node */
1299 signal_id = g_n_signal_nodes++;
1300 node = g_new (SignalNode, 1);
1301 node->signal_id = signal_id;
1302 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1303 g_signal_nodes[signal_id] = node;
1304 node->itype = itype;
1307 key.quark = g_quark_from_string (node->name);
1308 key.signal_id = signal_id;
1309 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1310 g_strdelimit (node->name, "_", '-');
1311 key.quark = g_quark_from_static_string (node->name);
1312 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1314 node->destroyed = FALSE;
1315 node->test_class_offset = 0;
1317 /* setup reinitializable portion */
1318 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1319 node->n_params = n_params;
1320 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1321 node->return_type = return_type;
1322 node->class_closure_bsa = NULL;
1325 node->accumulator = g_new (SignalAccumulator, 1);
1326 node->accumulator->func = accumulator;
1327 node->accumulator->data = accu_data;
1330 node->accumulator = NULL;
1331 node->c_marshaller = c_marshaller;
1332 node->emission_hooks = NULL;
1334 signal_add_class_closure (node, 0, class_closure);
1335 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1337 /* optimize NOP emissions */
1338 node->test_class_offset = TEST_CLASS_MAGIC;
1346 g_signal_new_valist (const gchar *signal_name,
1348 GSignalFlags signal_flags,
1349 GClosure *class_closure,
1350 GSignalAccumulator accumulator,
1352 GSignalCMarshaller c_marshaller,
1363 param_types = g_new (GType, n_params);
1365 for (i = 0; i < n_params; i++)
1366 param_types[i] = va_arg (args, GType);
1371 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1372 class_closure, accumulator, accu_data, c_marshaller,
1373 return_type, n_params, param_types);
1374 g_free (param_types);
1380 signal_destroy_R (SignalNode *signal_node)
1382 SignalNode node = *signal_node;
1384 signal_node->destroyed = TRUE;
1386 /* reentrancy caution, zero out real contents first */
1387 signal_node->test_class_offset = 0;
1388 signal_node->n_params = 0;
1389 signal_node->param_types = NULL;
1390 signal_node->return_type = 0;
1391 signal_node->class_closure_bsa = NULL;
1392 signal_node->accumulator = NULL;
1393 signal_node->c_marshaller = NULL;
1394 signal_node->emission_hooks = NULL;
1396 #ifdef G_ENABLE_DEBUG
1397 /* check current emissions */
1401 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1402 emission; emission = emission->next)
1403 if (emission->ihint.signal_id == node.signal_id)
1404 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1405 node.name, emission->instance);
1409 /* free contents that need to
1412 g_free (node.param_types);
1413 if (node.class_closure_bsa)
1417 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1419 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1421 g_closure_unref (cc->closure);
1423 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1425 g_free (node.accumulator);
1426 if (node.emission_hooks)
1428 g_hook_list_clear (node.emission_hooks);
1429 g_free (node.emission_hooks);
1435 g_signal_override_class_closure (guint signal_id,
1436 GType instance_type,
1437 GClosure *class_closure)
1441 g_return_if_fail (signal_id > 0);
1442 g_return_if_fail (class_closure != NULL);
1445 node = LOOKUP_SIGNAL_NODE (signal_id);
1446 if (!g_type_is_a (instance_type, node->itype))
1447 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1450 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1452 if (cc && cc->instance_type == instance_type)
1453 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1455 signal_add_class_closure (node, instance_type, class_closure);
1461 g_signal_chain_from_overridden (const GValue *instance_and_params,
1462 GValue *return_value)
1464 GType chain_type = 0, restore_type = 0;
1465 Emission *emission = NULL;
1466 GClosure *closure = NULL;
1470 g_return_if_fail (instance_and_params != NULL);
1471 instance = g_value_peek_pointer (instance_and_params);
1472 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1475 emission = emission_find_innermost (instance);
1478 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1480 g_assert (node != NULL); /* paranoid */
1482 /* we should probably do the same parameter checks as g_signal_emit() here.
1484 if (emission->chain_type != G_TYPE_NONE)
1486 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1488 g_assert (cc != NULL); /* closure currently in call stack */
1490 n_params = node->n_params;
1491 restore_type = cc->instance_type;
1492 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1493 if (cc && cc->instance_type != restore_type)
1495 closure = cc->closure;
1496 chain_type = cc->instance_type;
1500 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1503 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1506 emission->chain_type = chain_type;
1508 g_closure_invoke (closure,
1511 instance_and_params,
1514 emission->chain_type = restore_type;
1519 GSignalInvocationHint*
1520 g_signal_get_invocation_hint (gpointer instance)
1522 Emission *emission = NULL;
1524 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1527 emission = emission_find_innermost (instance);
1530 return emission ? &emission->ihint : NULL;
1534 g_signal_connect_closure_by_id (gpointer instance,
1541 gulong handler_seq_no = 0;
1543 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1544 g_return_val_if_fail (signal_id > 0, 0);
1545 g_return_val_if_fail (closure != NULL, 0);
1548 node = LOOKUP_SIGNAL_NODE (signal_id);
1551 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1552 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1553 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1554 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1557 Handler *handler = handler_new (after);
1559 handler_seq_no = handler->sequential_number;
1560 handler->detail = detail;
1561 handler->closure = g_closure_ref (closure);
1562 g_closure_sink (closure);
1563 handler_insert (signal_id, instance, handler);
1564 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1565 g_closure_set_marshal (closure, node->c_marshaller);
1569 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1572 return handler_seq_no;
1576 g_signal_connect_closure (gpointer instance,
1577 const gchar *detailed_signal,
1582 gulong handler_seq_no = 0;
1586 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1587 g_return_val_if_fail (detailed_signal != NULL, 0);
1588 g_return_val_if_fail (closure != NULL, 0);
1591 itype = G_TYPE_FROM_INSTANCE (instance);
1592 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1595 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1597 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1598 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1599 else if (!g_type_is_a (itype, node->itype))
1600 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1603 Handler *handler = handler_new (after);
1605 handler_seq_no = handler->sequential_number;
1606 handler->detail = detail;
1607 handler->closure = g_closure_ref (closure);
1608 g_closure_sink (closure);
1609 handler_insert (signal_id, instance, handler);
1610 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1611 g_closure_set_marshal (handler->closure, node->c_marshaller);
1615 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1618 return handler_seq_no;
1622 g_signal_connect_data (gpointer instance,
1623 const gchar *detailed_signal,
1624 GCallback c_handler,
1626 GClosureNotify destroy_data,
1627 GConnectFlags connect_flags)
1630 gulong handler_seq_no = 0;
1633 gboolean swapped, after;
1635 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1636 g_return_val_if_fail (detailed_signal != NULL, 0);
1637 g_return_val_if_fail (c_handler != NULL, 0);
1639 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1640 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1643 itype = G_TYPE_FROM_INSTANCE (instance);
1644 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1647 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1649 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1650 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1651 else if (!g_type_is_a (itype, node->itype))
1652 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1655 Handler *handler = handler_new (after);
1657 handler_seq_no = handler->sequential_number;
1658 handler->detail = detail;
1659 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1660 g_closure_sink (handler->closure);
1661 handler_insert (signal_id, instance, handler);
1662 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1663 g_closure_set_marshal (handler->closure, node->c_marshaller);
1667 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1670 return handler_seq_no;
1674 g_signal_handler_block (gpointer instance,
1679 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1680 g_return_if_fail (handler_id > 0);
1683 handler = handler_lookup (instance, handler_id, NULL);
1686 #ifndef G_DISABLE_CHECKS
1687 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1688 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1690 handler->block_count += 1;
1693 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1698 g_signal_handler_unblock (gpointer instance,
1703 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1704 g_return_if_fail (handler_id > 0);
1707 handler = handler_lookup (instance, handler_id, NULL);
1710 if (handler->block_count)
1711 handler->block_count -= 1;
1713 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1716 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1721 g_signal_handler_disconnect (gpointer instance,
1727 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1728 g_return_if_fail (handler_id > 0);
1731 handler = handler_lookup (instance, handler_id, &signal_id);
1734 handler->sequential_number = 0;
1735 handler->block_count = 1;
1736 handler_unref_R (signal_id, instance, handler);
1739 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1744 g_signal_handler_is_connected (gpointer instance,
1750 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1753 handler = handler_lookup (instance, handler_id, NULL);
1754 connected = handler != NULL;
1761 g_signal_handlers_destroy (gpointer instance)
1763 GBSearchArray *hlbsa;
1765 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1768 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1773 /* reentrancy caution, delete instance trace first */
1774 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1776 for (i = 0; i < hlbsa->n_nodes; i++)
1778 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1779 Handler *handler = hlist->handlers;
1783 Handler *tmp = handler;
1785 handler = tmp->next;
1786 tmp->block_count = 1;
1787 /* cruel unlink, this works because _all_ handlers vanish */
1790 if (tmp->sequential_number)
1792 tmp->sequential_number = 0;
1793 handler_unref_R (0, NULL, tmp);
1797 g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig);
1803 g_signal_handler_find (gpointer instance,
1804 GSignalMatchType mask,
1811 gulong handler_seq_no = 0;
1813 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1814 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1816 if (mask & G_SIGNAL_MATCH_MASK)
1818 HandlerMatch *mlist;
1821 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1824 handler_seq_no = mlist->handler->sequential_number;
1825 handler_match_free1_R (mlist, instance);
1830 return handler_seq_no;
1834 signal_handlers_foreach_matched_R (gpointer instance,
1835 GSignalMatchType mask,
1841 void (*callback) (gpointer instance,
1842 gulong handler_seq_no))
1844 HandlerMatch *mlist;
1845 guint n_handlers = 0;
1847 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1851 if (mlist->handler->sequential_number)
1854 callback (instance, mlist->handler->sequential_number);
1857 mlist = handler_match_free1_R (mlist, instance);
1864 g_signal_handlers_block_matched (gpointer instance,
1865 GSignalMatchType mask,
1872 guint n_handlers = 0;
1874 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1875 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1877 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1880 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1881 closure, func, data,
1882 g_signal_handler_block);
1890 g_signal_handlers_unblock_matched (gpointer instance,
1891 GSignalMatchType mask,
1898 guint n_handlers = 0;
1900 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1901 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1903 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1906 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1907 closure, func, data,
1908 g_signal_handler_unblock);
1916 g_signal_handlers_disconnect_matched (gpointer instance,
1917 GSignalMatchType mask,
1924 guint n_handlers = 0;
1926 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1927 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1929 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1932 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1933 closure, func, data,
1934 g_signal_handler_disconnect);
1942 g_signal_has_handler_pending (gpointer instance,
1945 gboolean may_be_blocked)
1947 HandlerMatch *mlist;
1948 gboolean has_pending;
1950 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1951 g_return_val_if_fail (signal_id > 0, FALSE);
1956 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1958 if (!(node->flags & G_SIGNAL_DETAILED))
1960 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1965 mlist = handlers_find (instance,
1966 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1967 signal_id, detail, NULL, NULL, NULL, TRUE);
1971 handler_match_free1_R (mlist, instance);
1974 has_pending = FALSE;
1980 static inline gboolean
1981 signal_check_skip_emission (SignalNode *node,
1987 /* are we able to check for NULL class handlers? */
1988 if (!node->test_class_offset)
1991 /* are there emission hooks pending? */
1992 if (node->emission_hooks && node->emission_hooks->hooks)
1995 /* is there a non-NULL class handler? */
1996 if (node->test_class_offset != TEST_CLASS_MAGIC)
1998 GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass);
2000 if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset))
2004 /* are signals being debugged? */
2005 #ifdef G_ENABLE_DEBUG
2006 IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals)
2008 #endif /* G_ENABLE_DEBUG */
2010 /* is this a no-recurse signal already in emission? */
2011 if (node->flags & G_SIGNAL_NO_RECURSE &&
2012 emission_find (g_restart_emissions, node->signal_id, detail, instance))
2015 /* do we have pending handlers? */
2016 hlist = handler_list_lookup (node->signal_id, instance);
2017 if (hlist && hlist->handlers)
2020 /* none of the above, no emission required */
2025 g_signal_emitv (const GValue *instance_and_params,
2028 GValue *return_value)
2030 const GValue *param_values;
2033 #ifdef G_ENABLE_DEBUG
2037 g_return_if_fail (instance_and_params != NULL);
2038 instance = g_value_peek_pointer (instance_and_params);
2039 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2040 g_return_if_fail (signal_id > 0);
2042 param_values = instance_and_params + 1;
2045 node = LOOKUP_SIGNAL_NODE (signal_id);
2046 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2048 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2052 #ifdef G_ENABLE_DEBUG
2053 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2055 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2059 for (i = 0; i < node->n_params; i++)
2060 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2062 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
2064 type_debug_name (node->param_types[i]),
2067 G_VALUE_TYPE_NAME (param_values + i));
2071 if (node->return_type != G_TYPE_NONE)
2075 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2077 type_debug_name (node->return_type),
2082 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2084 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2086 type_debug_name (node->return_type),
2088 G_VALUE_TYPE_NAME (return_value));
2094 return_value = NULL;
2095 #endif /* G_ENABLE_DEBUG */
2097 /* optimize NOP emissions */
2098 if (signal_check_skip_emission (node, instance, detail))
2100 /* nothing to do to emit this signal */
2102 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2107 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2111 g_signal_emit_valist (gpointer instance,
2116 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2117 GType signal_return_type;
2118 GValue *param_values;
2122 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2123 g_return_if_fail (signal_id > 0);
2126 node = LOOKUP_SIGNAL_NODE (signal_id);
2127 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2129 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2133 #ifndef G_DISABLE_CHECKS
2134 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2136 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2140 #endif /* !G_DISABLE_CHECKS */
2142 /* optimize NOP emissions */
2143 if (signal_check_skip_emission (node, instance, detail))
2145 /* nothing to do to emit this signal */
2147 /* g_printerr ("omitting emission of \"%s\"\n", node->name); */
2151 n_params = node->n_params;
2152 signal_return_type = node->return_type;
2153 if (node->n_params < MAX_STACK_VALUES)
2154 instance_and_params = stack_values;
2157 free_me = g_new (GValue, node->n_params + 1);
2158 instance_and_params = free_me;
2160 param_values = instance_and_params + 1;
2161 for (i = 0; i < node->n_params; i++)
2164 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2165 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2167 param_values[i].g_type = 0;
2169 g_value_init (param_values + i, ptype);
2170 G_VALUE_COLLECT (param_values + i,
2172 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2176 g_warning ("%s: %s", G_STRLOC, error);
2179 /* we purposely leak the value here, it might not be
2180 * in a sane state if an error condition occoured
2183 g_value_unset (param_values + i);
2191 instance_and_params->g_type = 0;
2192 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2193 g_value_set_instance (instance_and_params, instance);
2194 if (signal_return_type == G_TYPE_NONE)
2195 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2198 GValue return_value = { 0, };
2199 gchar *error = NULL;
2200 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2201 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2203 g_value_init (&return_value, rtype);
2205 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2207 G_VALUE_LCOPY (&return_value,
2209 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2212 g_value_unset (&return_value);
2215 g_warning ("%s: %s", G_STRLOC, error);
2218 /* we purposely leak the value here, it might not be
2219 * in a sane state if an error condition occured
2223 for (i = 0; i < n_params; i++)
2224 g_value_unset (param_values + i);
2225 g_value_unset (instance_and_params);
2231 g_signal_emit (gpointer instance,
2238 va_start (var_args, detail);
2239 g_signal_emit_valist (instance, signal_id, detail, var_args);
2244 g_signal_emit_by_name (gpointer instance,
2245 const gchar *detailed_signal,
2251 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2252 g_return_if_fail (detailed_signal != NULL);
2255 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2262 va_start (var_args, detailed_signal);
2263 g_signal_emit_valist (instance, signal_id, detail, var_args);
2267 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2270 static inline gboolean
2271 accumulate (GSignalInvocationHint *ihint,
2272 GValue *return_accu,
2273 GValue *handler_return,
2274 SignalAccumulator *accumulator)
2276 gboolean continue_emission;
2281 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2282 g_value_reset (handler_return);
2284 return continue_emission;
2288 signal_emit_unlocked_R (SignalNode *node,
2291 GValue *emission_return,
2292 const GValue *instance_and_params)
2294 SignalAccumulator *accumulator;
2296 GClosure *class_closure;
2298 Handler *handler_list = NULL;
2299 GValue *return_accu, accu = { 0, };
2301 gulong max_sequential_handler_number;
2302 gboolean return_value_altered = FALSE;
2304 #ifdef G_ENABLE_DEBUG
2305 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2307 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2308 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2311 if (g_trap_instance_signals == instance)
2314 #endif /* G_ENABLE_DEBUG */
2317 signal_id = node->signal_id;
2318 if (node->flags & G_SIGNAL_NO_RECURSE)
2320 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2324 node->state = EMISSION_RESTART;
2326 return return_value_altered;
2329 accumulator = node->accumulator;
2333 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2334 return_accu = &accu;
2338 return_accu = emission_return;
2339 emission.instance = instance;
2340 emission.ihint.signal_id = node->signal_id;
2341 emission.ihint.detail = detail;
2342 emission.ihint.run_type = 0;
2344 emission.chain_type = G_TYPE_NONE;
2345 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2346 class_closure = signal_lookup_closure (node, instance);
2351 handler_unref_R (signal_id, instance, handler_list);
2352 max_sequential_handler_number = g_handler_sequential_number;
2353 hlist = handler_list_lookup (signal_id, instance);
2354 handler_list = hlist ? hlist->handlers : NULL;
2356 handler_ref (handler_list);
2358 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2360 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2362 emission.state = EMISSION_RUN;
2364 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2366 g_closure_invoke (class_closure,
2369 instance_and_params,
2371 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2372 emission.state == EMISSION_RUN)
2373 emission.state = EMISSION_STOP;
2375 emission.chain_type = G_TYPE_NONE;
2376 return_value_altered = TRUE;
2378 if (emission.state == EMISSION_STOP)
2380 else if (emission.state == EMISSION_RESTART)
2384 if (node->emission_hooks)
2386 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2389 emission.state = EMISSION_HOOK;
2390 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2393 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2395 if (!signal_hook->detail || signal_hook->detail == detail)
2397 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2399 was_in_call = G_HOOK_IN_CALL (hook);
2400 hook->flags |= G_HOOK_FLAG_IN_CALL;
2402 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2405 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2407 g_hook_destroy_link (node->emission_hooks, hook);
2409 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2412 if (emission.state == EMISSION_RESTART)
2418 Handler *handler = handler_list;
2420 emission.state = EMISSION_RUN;
2421 handler_ref (handler);
2428 handler_unref_R (signal_id, instance, handler_list);
2429 handler_list = handler;
2432 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2433 handler->sequential_number < max_sequential_handler_number)
2436 g_closure_invoke (handler->closure,
2439 instance_and_params,
2441 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2442 emission.state == EMISSION_RUN)
2443 emission.state = EMISSION_STOP;
2445 return_value_altered = TRUE;
2447 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2450 tmp = handler->next;
2454 handler_unref_R (signal_id, instance, handler_list);
2455 handler_list = handler;
2460 if (emission.state == EMISSION_STOP)
2462 else if (emission.state == EMISSION_RESTART)
2466 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2468 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2470 emission.state = EMISSION_RUN;
2472 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2474 g_closure_invoke (class_closure,
2477 instance_and_params,
2479 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2480 emission.state == EMISSION_RUN)
2481 emission.state = EMISSION_STOP;
2483 emission.chain_type = G_TYPE_NONE;
2484 return_value_altered = TRUE;
2486 if (emission.state == EMISSION_STOP)
2488 else if (emission.state == EMISSION_RESTART)
2494 Handler *handler = handler_list;
2496 emission.state = EMISSION_RUN;
2497 handler_ref (handler);
2502 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2503 handler->sequential_number < max_sequential_handler_number)
2506 g_closure_invoke (handler->closure,
2509 instance_and_params,
2511 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2512 emission.state == EMISSION_RUN)
2513 emission.state = EMISSION_STOP;
2515 return_value_altered = TRUE;
2517 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2520 tmp = handler->next;
2524 handler_unref_R (signal_id, instance, handler);
2529 if (emission.state == EMISSION_STOP)
2531 else if (emission.state == EMISSION_RESTART)
2537 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2539 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2541 gboolean need_unset = FALSE;
2543 emission.state = EMISSION_STOP;
2545 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2547 if (node->return_type != G_TYPE_NONE && !accumulator)
2549 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2552 g_closure_invoke (class_closure,
2553 node->return_type != G_TYPE_NONE ? &accu : NULL,
2555 instance_and_params,
2558 g_value_unset (&accu);
2560 emission.chain_type = G_TYPE_NONE;
2562 if (emission.state == EMISSION_RESTART)
2567 handler_unref_R (signal_id, instance, handler_list);
2569 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2572 g_value_unset (&accu);
2574 return return_value_altered;
2578 type_debug_name (GType type)
2582 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2583 return name ? name : "<unknown>";
2590 g_signal_accumulator_true_handled (GSignalInvocationHint *ihint,
2591 GValue *return_accu,
2592 const GValue *handler_return,
2595 gboolean continue_emission;
2596 gboolean signal_handled;
2598 signal_handled = g_value_get_boolean (handler_return);
2599 g_value_set_boolean (return_accu, signal_handled);
2600 continue_emission = !signal_handled;
2602 return continue_emission;
2605 /* --- compile standard marshallers --- */
2606 #include "gobject.h"
2608 #include "gmarshal.c"