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>
30 #include "gbsearcharray.h"
31 #include "gvaluecollector.h"
32 #include "gvaluetypes.h"
38 /* pre allocation configurations
40 #define MAX_STACK_VALUES (16)
41 #define HANDLER_PRE_ALLOC (48)
43 #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org"
45 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
46 static volatile gpointer g_trace_instance_signals = NULL;
47 static volatile gpointer g_trap_instance_signals = NULL;
48 #endif /* G_ENABLE_DEBUG */
51 /* --- generic allocation --- */
52 /* we special case allocations generically by replacing
53 * these functions with more speed/memory aware variants
55 #ifndef DISABLE_MEM_POOLS
56 static inline gpointer
57 g_generic_node_alloc (GTrashStack **trash_stack_p,
59 guint nodes_pre_alloc)
61 gpointer node = g_trash_stack_pop (trash_stack_p);
67 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
68 block = g_malloc (sizeof_node * nodes_pre_alloc);
69 while (--nodes_pre_alloc)
71 g_trash_stack_push (trash_stack_p, block);
79 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
80 #else /* !DISABLE_MEM_POOLS */
81 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
82 #define g_generic_node_free(t,node) g_free (node)
83 #endif /* !DISABLE_MEM_POOLS */
86 /* --- typedefs --- */
87 typedef struct _SignalNode SignalNode;
88 typedef struct _SignalKey SignalKey;
89 typedef struct _Emission Emission;
90 typedef struct _Handler Handler;
91 typedef struct _HandlerList HandlerList;
92 typedef struct _HandlerMatch HandlerMatch;
102 /* --- prototypes --- */
103 static inline guint signal_id_lookup (GQuark quark,
105 static void signal_destroy_R (SignalNode *signal_node);
106 static inline HandlerList* handler_list_ensure (guint signal_id,
108 static inline HandlerList* handler_list_lookup (guint signal_id,
110 static inline Handler* handler_new (gboolean after);
111 static void handler_insert (guint signal_id,
114 static Handler* handler_lookup (gpointer instance,
117 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
120 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
122 static HandlerMatch* handlers_find (gpointer instance,
123 GSignalMatchType mask,
129 gboolean one_and_only);
130 static inline void handler_ref (Handler *handler);
131 static inline void handler_unref_R (guint signal_id,
134 static gint handler_lists_cmp (gconstpointer node1,
135 gconstpointer node2);
136 static inline void emission_push (Emission **emission_list_p,
138 static inline void emission_pop (Emission **emission_list_p,
140 static inline Emission* emission_find (Emission *emission_list,
144 static gint class_closures_cmp (gconstpointer node1,
145 gconstpointer node2);
146 static gint signal_key_cmp (gconstpointer node1,
147 gconstpointer node2);
148 static gboolean signal_emit_unlocked_R (SignalNode *node,
151 GValue *return_value,
152 const GValue *instance_and_params);
153 static const gchar * type_debug_name (GType type);
156 /* --- structures --- */
159 GSignalAccumulator func;
167 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
171 /* permanent portion */
177 /* reinitializable portion */
178 guint test_class_offset : 12;
181 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
182 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
183 GBSearchArray *class_closure_bsa;
184 SignalAccumulator *accumulator;
185 GSignalCMarshaller c_marshaller;
186 GHookList *emission_hooks;
188 #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */
189 #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */
202 GSignalInvocationHint ihint;
214 gulong sequential_number;
218 guint ref_count : 16;
219 #define HANDLER_MAX_REF_COUNT (1 << 16)
220 guint block_count : 12;
221 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
237 GType instance_type; /* 0 for default closure */
242 /* --- variables --- */
243 static GBSearchArray *g_signal_key_bsa = NULL;
244 static const GBSearchConfig g_signal_key_bconfig = {
247 G_BSEARCH_ARRAY_ALIGN_POWER2,
249 static GBSearchConfig g_signal_hlbsa_bconfig = {
250 sizeof (HandlerList),
254 static GBSearchConfig g_class_closure_bconfig = {
255 sizeof (ClassClosure),
259 static GHashTable *g_handler_list_bsa_ht = NULL;
260 static Emission *g_recursive_emissions = NULL;
261 static Emission *g_restart_emissions = NULL;
262 #ifndef DISABLE_MEM_POOLS
263 static GTrashStack *g_handler_ts = NULL;
265 static gulong g_handler_sequential_number = 1;
266 G_LOCK_DEFINE_STATIC (g_signal_mutex);
267 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
268 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
271 /* --- signal nodes --- */
272 static guint g_n_signal_nodes = 0;
273 static SignalNode **g_signal_nodes = NULL;
275 static inline SignalNode*
276 LOOKUP_SIGNAL_NODE (register guint signal_id)
278 if (signal_id < g_n_signal_nodes)
279 return g_signal_nodes[signal_id];
285 /* --- functions --- */
287 signal_id_lookup (GQuark quark,
290 GType *ifaces, type = itype;
296 /* try looking up signals for this type and its ancestors */
299 SignalKey *signal_key;
302 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
305 return signal_key->signal_id;
307 type = g_type_parent (type);
311 /* no luck, try interfaces it exports */
312 ifaces = g_type_interfaces (itype, &n_ifaces);
315 SignalKey *signal_key;
317 key.itype = ifaces[n_ifaces];
318 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
323 return signal_key->signal_id;
332 class_closures_cmp (gconstpointer node1,
335 const ClassClosure *c1 = node1, *c2 = node2;
337 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
341 handler_lists_cmp (gconstpointer node1,
344 const HandlerList *hlist1 = node1, *hlist2 = node2;
346 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
349 static inline HandlerList*
350 handler_list_ensure (guint signal_id,
353 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
356 key.signal_id = signal_id;
360 hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig);
361 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key);
362 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
366 GBSearchArray *o = hlbsa;
368 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key);
370 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
372 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
375 static inline HandlerList*
376 handler_list_lookup (guint signal_id,
379 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
382 key.signal_id = signal_id;
384 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
388 handler_lookup (gpointer instance,
392 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
398 for (i = 0; i < hlbsa->n_nodes; i++)
400 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
403 for (handler = hlist->handlers; handler; handler = handler->next)
404 if (handler->sequential_number == handler_id)
407 *signal_id_p = hlist->signal_id;
417 static inline HandlerMatch*
418 handler_match_prepend (HandlerMatch *list,
424 /* yeah, we could use our own memchunk here, introducing yet more
425 * rarely used cached nodes and extra allocation overhead.
426 * instead, we use GList* nodes, since they are exactly the size
427 * we need and are already cached. g_signal_init() asserts this.
429 node = (HandlerMatch*) g_list_alloc ();
430 node->handler = handler;
432 node->d.signal_id = signal_id;
433 handler_ref (handler);
437 static inline HandlerMatch*
438 handler_match_free1_R (HandlerMatch *node,
441 HandlerMatch *next = node->next;
443 handler_unref_R (node->d.signal_id, instance, node->handler);
444 g_list_free_1 ((GList*) node);
450 handlers_find (gpointer instance,
451 GSignalMatchType mask,
457 gboolean one_and_only)
459 HandlerMatch *mlist = NULL;
461 if (mask & G_SIGNAL_MATCH_ID)
463 HandlerList *hlist = handler_list_lookup (signal_id, instance);
465 SignalNode *node = NULL;
467 if (mask & G_SIGNAL_MATCH_FUNC)
469 node = LOOKUP_SIGNAL_NODE (signal_id);
470 if (!node || !node->c_marshaller)
475 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
476 if (handler->sequential_number &&
477 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
478 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
479 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
480 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
481 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
482 handler->closure->meta_marshal == 0 &&
483 ((GCClosure*) handler->closure)->callback == func)))
485 mlist = handler_match_prepend (mlist, handler, signal_id);
492 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
499 for (i = 0; i < hlbsa->n_nodes; i++)
501 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
502 SignalNode *node = NULL;
505 if (!(mask & G_SIGNAL_MATCH_FUNC))
507 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
508 if (!node->c_marshaller)
512 for (handler = hlist->handlers; handler; handler = handler->next)
513 if (handler->sequential_number &&
514 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
515 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
516 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
517 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
518 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
519 handler->closure->meta_marshal == 0 &&
520 ((GCClosure*) handler->closure)->callback == func)))
522 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
533 static inline Handler*
534 handler_new (gboolean after)
536 Handler *handler = g_generic_node_alloc (&g_handler_ts,
539 #ifndef G_DISABLE_CHECKS
540 if (g_handler_sequential_number < 1)
541 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
544 handler->sequential_number = g_handler_sequential_number++;
545 handler->prev = NULL;
546 handler->next = NULL;
548 handler->ref_count = 1;
549 handler->block_count = 0;
550 handler->after = after != FALSE;
551 handler->closure = NULL;
557 handler_ref (Handler *handler)
559 g_return_if_fail (handler->ref_count > 0);
561 #ifndef G_DISABLE_CHECKS
562 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
563 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
566 handler->ref_count += 1;
570 handler_unref_R (guint signal_id,
574 g_return_if_fail (handler->ref_count > 0);
576 handler->ref_count -= 1;
577 if (!handler->ref_count)
580 handler->next->prev = handler->prev;
581 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
582 handler->prev->next = handler->next;
585 HandlerList *hlist = handler_list_lookup (signal_id, instance);
587 hlist->handlers = handler->next;
590 g_closure_unref (handler->closure);
592 g_generic_node_free (&g_handler_ts, handler);
597 handler_insert (guint signal_id,
603 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
605 hlist = handler_list_ensure (signal_id, instance);
606 if (!hlist->handlers)
607 hlist->handlers = handler;
608 else if (hlist->handlers->after && !handler->after)
610 handler->next = hlist->handlers;
611 hlist->handlers->prev = handler;
612 hlist->handlers = handler;
616 Handler *tmp = hlist->handlers;
622 while (tmp->next && !tmp->next->after)
625 tmp->next->prev = handler;
626 handler->next = tmp->next;
633 emission_push (Emission **emission_list_p,
636 emission->next = *emission_list_p;
637 *emission_list_p = emission;
641 emission_pop (Emission **emission_list_p,
644 Emission *node, *last = NULL;
646 for (node = *emission_list_p; node; last = node, node = last->next)
647 if (node == emission)
650 last->next = node->next;
652 *emission_list_p = node->next;
655 g_assert_not_reached ();
658 static inline Emission*
659 emission_find (Emission *emission_list,
666 for (emission = emission_list; emission; emission = emission->next)
667 if (emission->instance == instance &&
668 emission->ihint.signal_id == signal_id &&
669 emission->ihint.detail == detail)
674 static inline Emission*
675 emission_find_innermost (gpointer instance)
677 Emission *emission, *s = NULL, *c = NULL;
679 for (emission = g_restart_emissions; emission; emission = emission->next)
680 if (emission->instance == instance)
685 for (emission = g_recursive_emissions; emission; emission = emission->next)
686 if (emission->instance == instance)
696 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
700 signal_key_cmp (gconstpointer node1,
703 const SignalKey *key1 = node1, *key2 = node2;
705 if (key1->itype == key2->itype)
706 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
708 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
712 g_signal_init (void) /* sync with gtype.c */
715 if (!g_n_signal_nodes)
717 /* handler_id_node_prepend() requires this */
718 g_assert (sizeof (GList) == sizeof (HandlerMatch));
720 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
721 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
722 g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig);
724 /* invalid (0) signal_id */
725 g_n_signal_nodes = 1;
726 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
727 g_signal_nodes[0] = NULL;
733 _g_signals_destroy (GType itype)
738 for (i = 1; i < g_n_signal_nodes; i++)
740 SignalNode *node = g_signal_nodes[i];
742 if (node->itype == itype)
745 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
747 type_debug_name (node->itype));
749 signal_destroy_R (node);
756 g_signal_stop_emission (gpointer instance,
762 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
763 g_return_if_fail (signal_id > 0);
766 node = LOOKUP_SIGNAL_NODE (signal_id);
767 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
769 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
773 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
775 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
776 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
780 if (emission->state == EMISSION_HOOK)
781 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
782 node->name, instance);
783 else if (emission->state == EMISSION_RUN)
784 emission->state = EMISSION_STOP;
787 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
788 node->name, instance);
791 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
796 signal_finalize_hook (GHookList *hook_list,
799 GDestroyNotify destroy = hook->destroy;
803 hook->destroy = NULL;
805 destroy (hook->data);
811 g_signal_add_emission_hook (guint signal_id,
813 GSignalEmissionHook hook_func,
815 GDestroyNotify data_destroy)
817 static gulong seq_hook_id = 1;
820 SignalHook *signal_hook;
822 g_return_val_if_fail (signal_id > 0, 0);
823 g_return_val_if_fail (hook_func != NULL, 0);
826 node = LOOKUP_SIGNAL_NODE (signal_id);
827 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
829 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
833 if (detail && !(node->flags & G_SIGNAL_DETAILED))
835 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
839 if (!node->emission_hooks)
841 node->emission_hooks = g_new (GHookList, 1);
842 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
843 node->emission_hooks->finalize_hook = signal_finalize_hook;
845 hook = g_hook_alloc (node->emission_hooks);
846 hook->data = hook_data;
847 hook->func = (gpointer) hook_func;
848 hook->destroy = data_destroy;
849 signal_hook = SIGNAL_HOOK (hook);
850 signal_hook->detail = detail;
851 node->emission_hooks->seq_id = seq_hook_id;
852 g_hook_append (node->emission_hooks, hook);
853 seq_hook_id = node->emission_hooks->seq_id;
856 return hook->hook_id;
860 g_signal_remove_emission_hook (guint signal_id,
865 g_return_if_fail (signal_id > 0);
866 g_return_if_fail (hook_id > 0);
869 node = LOOKUP_SIGNAL_NODE (signal_id);
870 if (!node || node->destroyed)
871 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
872 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
873 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
878 signal_parse_name (const gchar *name,
881 gboolean force_quark)
883 const gchar *colon = strchr (name, ':');
888 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
889 if (signal_id && detail_p)
892 else if (colon[1] == ':')
895 guint l = colon - name;
899 memcpy (buffer, name, l);
901 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
905 gchar *signal = g_new (gchar, l + 1);
907 memcpy (signal, name, l);
909 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
913 if (signal_id && detail_p)
914 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
922 g_signal_parse_name (const gchar *detailed_signal,
926 gboolean force_detail_quark)
932 g_return_val_if_fail (detailed_signal != NULL, FALSE);
933 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
936 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
939 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
940 if (!node || node->destroyed ||
941 (detail && !(node->flags & G_SIGNAL_DETAILED)))
945 *signal_id_p = signal_id;
953 g_signal_stop_emission_by_name (gpointer instance,
954 const gchar *detailed_signal)
960 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
961 g_return_if_fail (detailed_signal != NULL);
964 itype = G_TYPE_FROM_INSTANCE (instance);
965 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
968 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
970 if (detail && !(node->flags & G_SIGNAL_DETAILED))
971 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
972 else if (!g_type_is_a (itype, node->itype))
973 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
976 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
977 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
981 if (emission->state == EMISSION_HOOK)
982 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
983 node->name, instance);
984 else if (emission->state == EMISSION_RUN)
985 emission->state = EMISSION_STOP;
988 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
989 node->name, instance);
993 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
998 g_signal_lookup (const gchar *name,
1002 g_return_val_if_fail (name != NULL, 0);
1003 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1006 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1010 /* give elaborate warnings */
1011 if (!g_type_name (itype))
1012 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1014 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1015 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1016 name, g_type_name (itype));
1017 else if (!g_type_class_peek (itype))
1018 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1019 name, g_type_name (itype));
1026 g_signal_list_ids (GType itype,
1034 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1035 g_return_val_if_fail (n_ids != NULL, NULL);
1038 keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0);
1039 n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa);
1040 result = g_array_new (FALSE, FALSE, sizeof (guint));
1042 for (i = 0; i < n_nodes; i++)
1043 if (keys[i].itype == itype)
1045 const gchar *name = g_quark_to_string (keys[i].quark);
1047 /* Signal names with "_" in them are aliases to the same
1048 * name with "-" instead of "_".
1050 if (!strchr (name, '_'))
1051 g_array_append_val (result, keys[i].signal_id);
1053 *n_ids = result->len;
1057 /* give elaborate warnings */
1058 if (!g_type_name (itype))
1059 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1061 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1062 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1063 g_type_name (itype));
1064 else if (!g_type_class_peek (itype))
1065 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1066 g_type_name (itype));
1069 return (guint*) g_array_free (result, FALSE);
1072 G_CONST_RETURN gchar*
1073 g_signal_name (guint signal_id)
1079 node = LOOKUP_SIGNAL_NODE (signal_id);
1080 name = node ? node->name : NULL;
1087 g_signal_query (guint signal_id,
1088 GSignalQuery *query)
1092 g_return_if_fail (query != NULL);
1095 node = LOOKUP_SIGNAL_NODE (signal_id);
1096 if (!node || node->destroyed)
1097 query->signal_id = 0;
1100 query->signal_id = node->signal_id;
1101 query->signal_name = node->name;
1102 query->itype = node->itype;
1103 query->signal_flags = node->flags;
1104 query->return_type = node->return_type;
1105 query->n_params = node->n_params;
1106 query->param_types = node->param_types;
1112 g_signal_new (const gchar *signal_name,
1114 GSignalFlags signal_flags,
1116 GSignalAccumulator accumulator,
1118 GSignalCMarshaller c_marshaller,
1126 g_return_val_if_fail (signal_name != NULL, 0);
1128 va_start (args, n_params);
1130 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1131 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1132 accumulator, accu_data, c_marshaller,
1133 return_type, n_params, args);
1137 /* optimize NOP emissions with NULL class handlers */
1138 if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE &&
1139 class_offset && class_offset < MAX_TEST_CLASS_OFFSET)
1144 node = LOOKUP_SIGNAL_NODE (signal_id);
1145 node->test_class_offset = class_offset;
1152 static inline ClassClosure*
1153 signal_find_class_closure (SignalNode *node,
1156 GBSearchArray *bsa = node->class_closure_bsa;
1163 /* cc->instance_type is 0 for default closure */
1165 key.instance_type = itype;
1166 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1167 while (!cc && key.instance_type)
1169 key.instance_type = g_type_parent (key.instance_type);
1170 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1178 static inline GClosure*
1179 signal_lookup_closure (SignalNode *node,
1180 GTypeInstance *instance)
1184 if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1)
1185 cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0);
1187 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1188 return cc ? cc->closure : NULL;
1192 signal_add_class_closure (SignalNode *node,
1198 /* can't optimize NOP emissions with overridden class closures */
1199 node->test_class_offset = 0;
1201 if (!node->class_closure_bsa)
1202 node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig);
1203 key.instance_type = itype;
1204 key.closure = g_closure_ref (closure);
1205 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1206 &g_class_closure_bconfig,
1208 g_closure_sink (closure);
1209 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1210 g_closure_set_marshal (closure, node->c_marshaller);
1214 g_signal_newv (const gchar *signal_name,
1216 GSignalFlags signal_flags,
1217 GClosure *class_closure,
1218 GSignalAccumulator accumulator,
1220 GSignalCMarshaller c_marshaller,
1229 g_return_val_if_fail (signal_name != NULL, 0);
1230 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1232 g_return_val_if_fail (param_types != NULL, 0);
1233 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1234 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1235 g_return_val_if_fail (accumulator == NULL, 0);
1237 g_return_val_if_fail (accu_data == NULL, 0);
1239 name = g_strdup (signal_name);
1240 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1244 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1245 node = LOOKUP_SIGNAL_NODE (signal_id);
1246 if (node && !node->destroyed)
1248 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1250 type_debug_name (node->itype),
1251 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1256 if (node && node->itype != itype)
1258 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1260 type_debug_name (itype),
1261 type_debug_name (node->itype));
1266 for (i = 0; i < n_params; i++)
1267 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1269 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1270 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1275 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1277 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1278 type_debug_name (return_type), type_debug_name (itype), name);
1283 if (return_type != G_TYPE_NONE &&
1284 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1286 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1287 type_debug_name (itype), name, type_debug_name (return_type));
1293 /* setup permanent portion of signal node */
1298 signal_id = g_n_signal_nodes++;
1299 node = g_new (SignalNode, 1);
1300 node->signal_id = signal_id;
1301 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1302 g_signal_nodes[signal_id] = node;
1303 node->itype = itype;
1306 key.quark = g_quark_from_string (node->name);
1307 key.signal_id = signal_id;
1308 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1309 g_strdelimit (node->name, "_", '-');
1310 key.quark = g_quark_from_static_string (node->name);
1311 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key);
1313 node->destroyed = FALSE;
1314 node->test_class_offset = 0;
1316 /* setup reinitializable portion */
1317 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1318 node->n_params = n_params;
1319 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1320 node->return_type = return_type;
1321 node->class_closure_bsa = NULL;
1324 node->accumulator = g_new (SignalAccumulator, 1);
1325 node->accumulator->func = accumulator;
1326 node->accumulator->data = accu_data;
1329 node->accumulator = NULL;
1330 node->c_marshaller = c_marshaller;
1331 node->emission_hooks = NULL;
1333 signal_add_class_closure (node, 0, class_closure);
1334 else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE)
1336 /* optimize NOP emissions */
1337 node->test_class_offset = TEST_CLASS_MAGIC;
1345 g_signal_new_valist (const gchar *signal_name,
1347 GSignalFlags signal_flags,
1348 GClosure *class_closure,
1349 GSignalAccumulator accumulator,
1351 GSignalCMarshaller c_marshaller,
1362 param_types = g_new (GType, n_params);
1364 for (i = 0; i < n_params; i++)
1365 param_types[i] = va_arg (args, GType);
1370 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1371 class_closure, accumulator, accu_data, c_marshaller,
1372 return_type, n_params, param_types);
1373 g_free (param_types);
1379 signal_destroy_R (SignalNode *signal_node)
1381 SignalNode node = *signal_node;
1383 signal_node->destroyed = TRUE;
1385 /* reentrancy caution, zero out real contents first */
1386 signal_node->test_class_offset = 0;
1387 signal_node->n_params = 0;
1388 signal_node->param_types = NULL;
1389 signal_node->return_type = 0;
1390 signal_node->class_closure_bsa = NULL;
1391 signal_node->accumulator = NULL;
1392 signal_node->c_marshaller = NULL;
1393 signal_node->emission_hooks = NULL;
1395 #ifdef G_ENABLE_DEBUG
1396 /* check current emissions */
1400 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1401 emission; emission = emission->next)
1402 if (emission->ihint.signal_id == node.signal_id)
1403 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1404 node.name, emission->instance);
1408 /* free contents that need to
1411 g_free (node.param_types);
1412 if (node.class_closure_bsa)
1416 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1418 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1420 g_closure_unref (cc->closure);
1422 g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig);
1424 g_free (node.accumulator);
1425 if (node.emission_hooks)
1427 g_hook_list_clear (node.emission_hooks);
1428 g_free (node.emission_hooks);
1434 g_signal_override_class_closure (guint signal_id,
1435 GType instance_type,
1436 GClosure *class_closure)
1440 g_return_if_fail (signal_id > 0);
1441 g_return_if_fail (class_closure != NULL);
1444 node = LOOKUP_SIGNAL_NODE (signal_id);
1445 if (!g_type_is_a (instance_type, node->itype))
1446 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1449 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1451 if (cc && cc->instance_type == instance_type)
1452 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1454 signal_add_class_closure (node, instance_type, class_closure);
1460 g_signal_chain_from_overridden (const GValue *instance_and_params,
1461 GValue *return_value)
1463 GType chain_type = 0, restore_type = 0;
1464 Emission *emission = NULL;
1465 GClosure *closure = NULL;
1469 g_return_if_fail (instance_and_params != NULL);
1470 instance = g_value_peek_pointer (instance_and_params);
1471 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1474 emission = emission_find_innermost (instance);
1477 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1479 g_assert (node != NULL); /* paranoid */
1481 /* we should probably do the same parameter checks as g_signal_emit() here.
1483 if (emission->chain_type != G_TYPE_NONE)
1485 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1487 g_assert (cc != NULL); /* closure currently in call stack */
1489 n_params = node->n_params;
1490 restore_type = cc->instance_type;
1491 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1492 if (cc && cc->instance_type != restore_type)
1494 closure = cc->closure;
1495 chain_type = cc->instance_type;
1499 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1502 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1505 emission->chain_type = chain_type;
1507 g_closure_invoke (closure,
1510 instance_and_params,
1513 emission->chain_type = restore_type;
1518 GSignalInvocationHint*
1519 g_signal_get_invocation_hint (gpointer instance)
1521 Emission *emission = NULL;
1523 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1526 emission = emission_find_innermost (instance);
1529 return emission ? &emission->ihint : NULL;
1533 g_signal_connect_closure_by_id (gpointer instance,
1540 gulong handler_seq_no = 0;
1542 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1543 g_return_val_if_fail (signal_id > 0, 0);
1544 g_return_val_if_fail (closure != NULL, 0);
1547 node = LOOKUP_SIGNAL_NODE (signal_id);
1550 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1551 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1552 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1553 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1556 Handler *handler = handler_new (after);
1558 handler_seq_no = handler->sequential_number;
1559 handler->detail = detail;
1560 handler->closure = g_closure_ref (closure);
1561 g_closure_sink (closure);
1562 handler_insert (signal_id, instance, handler);
1563 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1564 g_closure_set_marshal (closure, node->c_marshaller);
1568 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1571 return handler_seq_no;
1575 g_signal_connect_closure (gpointer instance,
1576 const gchar *detailed_signal,
1581 gulong handler_seq_no = 0;
1585 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1586 g_return_val_if_fail (detailed_signal != NULL, 0);
1587 g_return_val_if_fail (closure != NULL, 0);
1590 itype = G_TYPE_FROM_INSTANCE (instance);
1591 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1594 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1596 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1597 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1598 else if (!g_type_is_a (itype, node->itype))
1599 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1602 Handler *handler = handler_new (after);
1604 handler_seq_no = handler->sequential_number;
1605 handler->detail = detail;
1606 handler->closure = g_closure_ref (closure);
1607 g_closure_sink (closure);
1608 handler_insert (signal_id, instance, handler);
1609 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1610 g_closure_set_marshal (handler->closure, node->c_marshaller);
1614 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1617 return handler_seq_no;
1621 g_signal_connect_data (gpointer instance,
1622 const gchar *detailed_signal,
1623 GCallback c_handler,
1625 GClosureNotify destroy_data,
1626 GConnectFlags connect_flags)
1629 gulong handler_seq_no = 0;
1632 gboolean swapped, after;
1634 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1635 g_return_val_if_fail (detailed_signal != NULL, 0);
1636 g_return_val_if_fail (c_handler != NULL, 0);
1638 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1639 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1642 itype = G_TYPE_FROM_INSTANCE (instance);
1643 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1646 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1648 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1649 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1650 else if (!g_type_is_a (itype, node->itype))
1651 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1654 Handler *handler = handler_new (after);
1656 handler_seq_no = handler->sequential_number;
1657 handler->detail = detail;
1658 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1659 g_closure_sink (handler->closure);
1660 handler_insert (signal_id, instance, handler);
1661 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1662 g_closure_set_marshal (handler->closure, node->c_marshaller);
1666 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1669 return handler_seq_no;
1673 g_signal_handler_block (gpointer instance,
1678 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1679 g_return_if_fail (handler_id > 0);
1682 handler = handler_lookup (instance, handler_id, NULL);
1685 #ifndef G_DISABLE_CHECKS
1686 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1687 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1689 handler->block_count += 1;
1692 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1697 g_signal_handler_unblock (gpointer instance,
1702 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1703 g_return_if_fail (handler_id > 0);
1706 handler = handler_lookup (instance, handler_id, NULL);
1709 if (handler->block_count)
1710 handler->block_count -= 1;
1712 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1715 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1720 g_signal_handler_disconnect (gpointer instance,
1726 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1727 g_return_if_fail (handler_id > 0);
1730 handler = handler_lookup (instance, handler_id, &signal_id);
1733 handler->sequential_number = 0;
1734 handler->block_count = 1;
1735 handler_unref_R (signal_id, instance, handler);
1738 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1743 g_signal_handler_is_connected (gpointer instance,
1749 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1750 g_return_val_if_fail (handler_id > 0, 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"