1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
28 #include "gbsearcharray.h"
29 #include "gvaluecollector.h"
30 #include "gvaluetypes.h"
36 /* pre allocation configurations
38 #define MAX_STACK_VALUES (16)
39 #define HANDLER_PRE_ALLOC (48)
41 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
43 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
44 static volatile gpointer g_trace_instance_signals = NULL;
45 static volatile gpointer g_trap_instance_signals = NULL;
46 #endif /* G_ENABLE_DEBUG */
49 /* --- generic allocation --- */
50 /* we special case allocations generically by replacing
51 * these functions with more speed/memory aware variants
53 #ifndef DISABLE_MEM_POOLS
54 static inline gpointer
55 g_generic_node_alloc (GTrashStack **trash_stack_p,
57 guint nodes_pre_alloc)
59 gpointer node = g_trash_stack_pop (trash_stack_p);
65 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
66 block = g_malloc (sizeof_node * nodes_pre_alloc);
67 while (--nodes_pre_alloc)
69 g_trash_stack_push (trash_stack_p, block);
77 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
78 #else /* !DISABLE_MEM_POOLS */
79 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
80 #define g_generic_node_free(t,node) g_free (node)
81 #endif /* !DISABLE_MEM_POOLS */
84 /* --- typedefs --- */
85 typedef struct _SignalNode SignalNode;
86 typedef struct _SignalKey SignalKey;
87 typedef struct _Emission Emission;
88 typedef struct _Handler Handler;
89 typedef struct _HandlerList HandlerList;
90 typedef struct _HandlerMatch HandlerMatch;
100 /* --- prototypes --- */
101 static inline guint signal_id_lookup (GQuark quark,
103 static void signal_destroy_R (SignalNode *signal_node);
104 static inline HandlerList* handler_list_ensure (guint signal_id,
106 static inline HandlerList* handler_list_lookup (guint signal_id,
108 static inline Handler* handler_new (gboolean after);
109 static void handler_insert (guint signal_id,
112 static Handler* handler_lookup (gpointer instance,
115 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
118 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
120 static HandlerMatch* handlers_find (gpointer instance,
121 GSignalMatchType mask,
127 gboolean one_and_only);
128 static inline void handler_ref (Handler *handler);
129 static inline void handler_unref_R (guint signal_id,
132 static gint handler_lists_cmp (gconstpointer node1,
133 gconstpointer node2);
134 static inline void emission_push (Emission **emission_list_p,
136 static inline void emission_pop (Emission **emission_list_p,
138 static inline Emission* emission_find (Emission *emission_list,
142 static gint class_closures_cmp (gconstpointer node1,
143 gconstpointer node2);
144 static gint signal_key_cmp (gconstpointer node1,
145 gconstpointer node2);
146 static gboolean signal_emit_unlocked_R (SignalNode *node,
149 GValue *return_value,
150 const GValue *instance_and_params);
151 static const gchar * type_debug_name (GType type);
154 /* --- structures --- */
157 GSignalAccumulator func;
165 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
169 /* permanent portion */
175 /* reinitializable portion */
178 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
179 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
180 GBSearchArray *class_closure_bsa;
181 SignalAccumulator *accumulator;
182 GSignalCMarshaller c_marshaller;
183 GHookList *emission_hooks;
197 GSignalInvocationHint ihint;
209 gulong sequential_number;
213 guint ref_count : 16;
214 #define HANDLER_MAX_REF_COUNT (1 << 16)
215 guint block_count : 12;
216 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
232 GType instance_type; /* 0 for default closure */
237 /* --- variables --- */
238 static GBSearchArray *g_signal_key_bsa = NULL;
239 static GBSearchConfig g_signal_key_bconfig = G_STATIC_BCONFIG (sizeof (SignalKey),
241 G_BSEARCH_ARRAY_ALIGN_POWER2);
242 static GBSearchConfig g_signal_hlbsa_bconfig = G_STATIC_BCONFIG (sizeof (HandlerList),
244 G_BSEARCH_ARRAY_DEFER_SHRINK);
245 static GBSearchConfig g_class_closure_bconfig = G_STATIC_BCONFIG (sizeof (ClassClosure),
248 static GHashTable *g_handler_list_bsa_ht = NULL;
249 static Emission *g_recursive_emissions = NULL;
250 static Emission *g_restart_emissions = NULL;
251 static GTrashStack *g_handler_ts = NULL;
252 static gulong g_handler_sequential_number = 1;
253 G_LOCK_DEFINE_STATIC (g_signal_mutex);
254 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
255 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
258 /* --- signal nodes --- */
259 static guint g_n_signal_nodes = 0;
260 static SignalNode **g_signal_nodes = NULL;
262 static inline SignalNode*
263 LOOKUP_SIGNAL_NODE (register guint signal_id)
265 if (signal_id < g_n_signal_nodes)
266 return g_signal_nodes[signal_id];
272 /* --- functions --- */
274 signal_id_lookup (GQuark quark,
277 GType *ifaces, type = itype;
283 /* try looking up signals for this type and its anchestors */
286 SignalKey *signal_key;
289 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
292 return signal_key->signal_id;
294 type = g_type_parent (type);
298 /* no luck, try interfaces it exports */
299 ifaces = g_type_interfaces (itype, &n_ifaces);
302 SignalKey *signal_key;
304 key.itype = ifaces[n_ifaces];
305 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
310 return signal_key->signal_id;
319 class_closures_cmp (gconstpointer node1,
322 const ClassClosure *c1 = node1, *c2 = node2;
324 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
328 handler_lists_cmp (gconstpointer node1,
331 const HandlerList *hlist1 = node1, *hlist2 = node2;
333 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
336 static inline HandlerList*
337 handler_list_ensure (guint signal_id,
340 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
343 key.signal_id = signal_id;
347 hlbsa = g_bsearch_array_new (&g_signal_hlbsa_bconfig);
348 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key, FALSE);
349 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
353 GBSearchArray *o = hlbsa;
355 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key, FALSE);
357 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
359 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
362 static inline HandlerList*
363 handler_list_lookup (guint signal_id,
366 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
369 key.signal_id = signal_id;
371 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
375 handler_lookup (gpointer instance,
379 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
385 for (i = 0; i < hlbsa->n_nodes; i++)
387 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
390 for (handler = hlist->handlers; handler; handler = handler->next)
391 if (handler->sequential_number == handler_id)
394 *signal_id_p = hlist->signal_id;
404 static inline HandlerMatch*
405 handler_match_prepend (HandlerMatch *list,
411 /* yeah, we could use our own memchunk here, introducing yet more
412 * rarely used cached nodes and extra allocation overhead.
413 * instead, we use GList* nodes, since they are exactly the size
414 * we need and are already cached. g_signal_init() asserts this.
416 node = (HandlerMatch*) g_list_alloc ();
417 node->handler = handler;
419 node->d.signal_id = signal_id;
420 handler_ref (handler);
424 static inline HandlerMatch*
425 handler_match_free1_R (HandlerMatch *node,
428 HandlerMatch *next = node->next;
430 handler_unref_R (node->d.signal_id, instance, node->handler);
431 g_list_free_1 ((GList*) node);
437 handlers_find (gpointer instance,
438 GSignalMatchType mask,
444 gboolean one_and_only)
446 HandlerMatch *mlist = NULL;
448 if (mask & G_SIGNAL_MATCH_ID)
450 HandlerList *hlist = handler_list_lookup (signal_id, instance);
452 SignalNode *node = NULL;
454 if (mask & G_SIGNAL_MATCH_FUNC)
456 node = LOOKUP_SIGNAL_NODE (signal_id);
457 if (!node || !node->c_marshaller)
462 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
463 if (handler->sequential_number &&
464 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
465 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
466 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
467 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
468 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
469 handler->closure->meta_marshal == 0 &&
470 ((GCClosure*) handler->closure)->callback == func)))
472 mlist = handler_match_prepend (mlist, handler, signal_id);
479 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
486 for (i = 0; i < hlbsa->n_nodes; i++)
488 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
489 SignalNode *node = NULL;
492 if (!(mask & G_SIGNAL_MATCH_FUNC))
494 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
495 if (!node->c_marshaller)
499 for (handler = hlist->handlers; handler; handler = handler->next)
500 if (handler->sequential_number &&
501 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
502 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
503 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
504 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
505 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
506 handler->closure->meta_marshal == 0 &&
507 ((GCClosure*) handler->closure)->callback == func)))
509 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
520 static inline Handler*
521 handler_new (gboolean after)
523 Handler *handler = g_generic_node_alloc (&g_handler_ts,
526 #ifndef G_DISABLE_CHECKS
527 if (g_handler_sequential_number < 1)
528 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
531 handler->sequential_number = g_handler_sequential_number++;
532 handler->prev = NULL;
533 handler->next = NULL;
535 handler->ref_count = 1;
536 handler->block_count = 0;
537 handler->after = after != FALSE;
538 handler->closure = NULL;
544 handler_ref (Handler *handler)
546 g_return_if_fail (handler->ref_count > 0);
548 #ifndef G_DISABLE_CHECKS
549 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
550 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
553 handler->ref_count += 1;
557 handler_unref_R (guint signal_id,
561 g_return_if_fail (handler->ref_count > 0);
563 handler->ref_count -= 1;
564 if (!handler->ref_count)
567 handler->next->prev = handler->prev;
568 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
569 handler->prev->next = handler->next;
572 HandlerList *hlist = handler_list_lookup (signal_id, instance);
574 hlist->handlers = handler->next;
577 g_closure_unref (handler->closure);
579 g_generic_node_free (&g_handler_ts, handler);
584 handler_insert (guint signal_id,
590 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
592 hlist = handler_list_ensure (signal_id, instance);
593 if (!hlist->handlers)
594 hlist->handlers = handler;
595 else if (hlist->handlers->after && !handler->after)
597 handler->next = hlist->handlers;
598 hlist->handlers->prev = handler;
599 hlist->handlers = handler;
603 Handler *tmp = hlist->handlers;
609 while (tmp->next && !tmp->next->after)
612 tmp->next->prev = handler;
613 handler->next = tmp->next;
620 emission_push (Emission **emission_list_p,
623 emission->next = *emission_list_p;
624 *emission_list_p = emission;
628 emission_pop (Emission **emission_list_p,
631 Emission *node, *last = NULL;
633 for (node = *emission_list_p; node; last = node, node = last->next)
634 if (node == emission)
637 last->next = node->next;
639 *emission_list_p = node->next;
642 g_assert_not_reached ();
645 static inline Emission*
646 emission_find (Emission *emission_list,
653 for (emission = emission_list; emission; emission = emission->next)
654 if (emission->instance == instance &&
655 emission->ihint.signal_id == signal_id &&
656 emission->ihint.detail == detail)
661 static inline Emission*
662 emission_find_innermost (gpointer instance)
664 Emission *emission, *s = NULL, *c = NULL;
666 for (emission = g_restart_emissions; emission; emission = emission->next)
667 if (emission->instance == instance)
672 for (emission = g_recursive_emissions; emission; emission = emission->next)
673 if (emission->instance == instance)
683 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
687 signal_key_cmp (gconstpointer node1,
690 const SignalKey *key1 = node1, *key2 = node2;
692 if (key1->itype == key2->itype)
693 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
695 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
699 g_signal_init (void) /* sync with gtype.c */
702 if (!g_n_signal_nodes)
704 /* handler_id_node_prepend() requires this */
705 g_assert (sizeof (GList) == sizeof (HandlerMatch));
707 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
708 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
709 g_signal_key_bsa = g_bsearch_array_new (&g_signal_key_bconfig);
711 /* invalid (0) signal_id */
712 g_n_signal_nodes = 1;
713 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
714 g_signal_nodes[0] = NULL;
720 _g_signals_destroy (GType itype)
725 for (i = 1; i < g_n_signal_nodes; i++)
727 SignalNode *node = g_signal_nodes[i];
729 if (node->itype == itype)
732 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
734 type_debug_name (node->itype));
736 signal_destroy_R (node);
743 g_signal_stop_emission (gpointer instance,
749 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
750 g_return_if_fail (signal_id > 0);
753 node = LOOKUP_SIGNAL_NODE (signal_id);
754 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
756 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
760 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
762 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
763 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
767 if (emission->state == EMISSION_HOOK)
768 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
769 node->name, instance);
770 else if (emission->state == EMISSION_RUN)
771 emission->state = EMISSION_STOP;
774 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
775 node->name, instance);
778 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
783 signal_finalize_hook (GHookList *hook_list,
786 GDestroyNotify destroy = hook->destroy;
790 hook->destroy = NULL;
792 destroy (hook->data);
798 g_signal_add_emission_hook (guint signal_id,
800 GSignalEmissionHook hook_func,
802 GDestroyNotify data_destroy)
804 static gulong seq_hook_id = 1;
807 SignalHook *signal_hook;
809 g_return_val_if_fail (signal_id > 0, 0);
810 g_return_val_if_fail (hook_func != NULL, 0);
813 node = LOOKUP_SIGNAL_NODE (signal_id);
814 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
816 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
820 if (detail && !(node->flags & G_SIGNAL_DETAILED))
822 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
826 if (!node->emission_hooks)
828 node->emission_hooks = g_new (GHookList, 1);
829 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
830 node->emission_hooks->finalize_hook = signal_finalize_hook;
832 hook = g_hook_alloc (node->emission_hooks);
833 hook->data = hook_data;
834 hook->func = (gpointer) hook_func;
835 hook->destroy = data_destroy;
836 signal_hook = SIGNAL_HOOK (hook);
837 signal_hook->detail = detail;
838 node->emission_hooks->seq_id = seq_hook_id;
839 g_hook_append (node->emission_hooks, hook);
840 seq_hook_id = node->emission_hooks->seq_id;
843 return hook->hook_id;
847 g_signal_remove_emission_hook (guint signal_id,
852 g_return_if_fail (signal_id > 0);
853 g_return_if_fail (hook_id > 0);
856 node = LOOKUP_SIGNAL_NODE (signal_id);
857 if (!node || node->destroyed)
858 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
859 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
860 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
865 signal_parse_name (const gchar *name,
868 gboolean force_quark)
870 const gchar *colon = strchr (name, ':');
875 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
876 if (signal_id && detail_p)
879 else if (colon[1] == ':')
882 guint l = colon - name;
886 memcpy (buffer, name, l);
888 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
892 gchar *signal = g_new (gchar, l + 1);
894 memcpy (signal, name, l);
896 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
900 if (signal_id && detail_p)
901 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
909 g_signal_parse_name (const gchar *detailed_signal,
913 gboolean force_detail_quark)
919 g_return_val_if_fail (detailed_signal != NULL, FALSE);
920 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
923 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
926 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
927 if (!node || node->destroyed ||
928 (detail && !(node->flags & G_SIGNAL_DETAILED)))
932 *signal_id_p = signal_id;
940 g_signal_stop_emission_by_name (gpointer instance,
941 const gchar *detailed_signal)
947 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
948 g_return_if_fail (detailed_signal != NULL);
951 itype = G_TYPE_FROM_INSTANCE (instance);
952 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
955 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
957 if (detail && !(node->flags & G_SIGNAL_DETAILED))
958 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
959 else if (!g_type_is_a (itype, node->itype))
960 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
963 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
964 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
968 if (emission->state == EMISSION_HOOK)
969 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
970 node->name, instance);
971 else if (emission->state == EMISSION_RUN)
972 emission->state = EMISSION_STOP;
975 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
976 node->name, instance);
980 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
985 g_signal_lookup (const gchar *name,
990 g_return_val_if_fail (name != NULL, 0);
991 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
994 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
998 /* give elaborate warnings */
999 if (!g_type_name (itype))
1000 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1002 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1003 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1004 name, g_type_name (itype));
1005 else if (!g_type_class_peek (itype))
1006 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1007 name, g_type_name (itype));
1014 g_signal_list_ids (GType itype,
1022 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1023 g_return_val_if_fail (n_ids != NULL, NULL);
1026 keys = G_BSEARCH_ARRAY_NODES (g_signal_key_bsa);
1027 n_nodes = g_signal_key_bsa->n_nodes;
1028 result = g_array_new (FALSE, FALSE, sizeof (guint));
1030 for (i = 0; i < n_nodes; i++)
1031 if (keys[i].itype == itype)
1033 const gchar *name = g_quark_to_string (keys[i].quark);
1035 /* Signal names with "_" in them are aliases to the same
1036 * name with "-" instead of "_".
1038 if (!strchr (name, '_'))
1039 g_array_append_val (result, keys[i].signal_id);
1041 *n_ids = result->len;
1045 /* give elaborate warnings */
1046 if (!g_type_name (itype))
1047 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1049 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1050 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1051 g_type_name (itype));
1052 else if (!g_type_class_peek (itype))
1053 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1054 g_type_name (itype));
1057 return (guint*) g_array_free (result, FALSE);
1060 G_CONST_RETURN gchar*
1061 g_signal_name (guint signal_id)
1067 node = LOOKUP_SIGNAL_NODE (signal_id);
1068 name = node ? node->name : NULL;
1075 g_signal_query (guint signal_id,
1076 GSignalQuery *query)
1080 g_return_if_fail (query != NULL);
1083 node = LOOKUP_SIGNAL_NODE (signal_id);
1084 if (!node || node->destroyed)
1085 query->signal_id = 0;
1088 query->signal_id = node->signal_id;
1089 query->signal_name = node->name;
1090 query->itype = node->itype;
1091 query->signal_flags = node->flags;
1092 query->return_type = node->return_type;
1093 query->n_params = node->n_params;
1094 query->param_types = node->param_types;
1100 g_signal_new (const gchar *signal_name,
1102 GSignalFlags signal_flags,
1104 GSignalAccumulator accumulator,
1106 GSignalCMarshaller c_marshaller,
1114 g_return_val_if_fail (signal_name != NULL, 0);
1116 va_start (args, n_params);
1118 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1119 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1120 accumulator, accu_data, c_marshaller,
1121 return_type, n_params, args);
1128 static inline ClassClosure*
1129 signal_find_class_closure (SignalNode *node,
1132 GBSearchArray *bsa = node->class_closure_bsa;
1139 /* cc->instance_type is 0 for default closure */
1141 key.instance_type = itype;
1142 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1143 while (!cc && key.instance_type)
1145 key.instance_type = g_type_parent (key.instance_type);
1146 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1154 static inline GClosure*
1155 signal_lookup_closure (SignalNode *node,
1156 GTypeInstance *instance)
1160 if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1161 cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1163 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1164 return cc ? cc->closure : NULL;
1168 signal_add_class_closure (SignalNode *node,
1174 if (!node->class_closure_bsa)
1175 node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1176 key.instance_type = itype;
1177 key.closure = g_closure_ref (closure);
1178 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1179 &g_class_closure_bconfig,
1182 g_closure_sink (closure);
1183 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1184 g_closure_set_marshal (closure, node->c_marshaller);
1188 g_signal_newv (const gchar *signal_name,
1190 GSignalFlags signal_flags,
1191 GClosure *class_closure,
1192 GSignalAccumulator accumulator,
1194 GSignalCMarshaller c_marshaller,
1203 g_return_val_if_fail (signal_name != NULL, 0);
1204 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1206 g_return_val_if_fail (param_types != NULL, 0);
1207 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1208 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1209 g_return_val_if_fail (accumulator == NULL, 0);
1211 g_return_val_if_fail (accu_data == NULL, 0);
1213 name = g_strdup (signal_name);
1214 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1218 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1219 node = LOOKUP_SIGNAL_NODE (signal_id);
1220 if (node && !node->destroyed)
1222 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1224 type_debug_name (node->itype),
1225 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1230 if (node && node->itype != itype)
1232 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1234 type_debug_name (itype),
1235 type_debug_name (node->itype));
1240 for (i = 0; i < n_params; i++)
1241 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1243 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1244 i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name);
1249 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1251 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1252 type_debug_name (return_type), type_debug_name (itype), name);
1257 if (return_type != G_TYPE_NONE &&
1258 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1260 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1261 type_debug_name (itype), name, type_debug_name (return_type));
1267 /* setup permanent portion of signal node */
1272 signal_id = g_n_signal_nodes++;
1273 node = g_new (SignalNode, 1);
1274 node->signal_id = signal_id;
1275 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1276 g_signal_nodes[signal_id] = node;
1277 node->itype = itype;
1280 key.quark = g_quark_from_string (node->name);
1281 key.signal_id = signal_id;
1282 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1283 g_strdelimit (node->name, "_", '-');
1284 key.quark = g_quark_from_static_string (node->name);
1285 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1287 node->destroyed = FALSE;
1289 /* setup reinitializable portion */
1290 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1291 node->n_params = n_params;
1292 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1293 node->return_type = return_type;
1294 node->class_closure_bsa = NULL;
1297 node->accumulator = g_new (SignalAccumulator, 1);
1298 node->accumulator->func = accumulator;
1299 node->accumulator->data = accu_data;
1302 node->accumulator = NULL;
1303 node->c_marshaller = c_marshaller;
1304 node->emission_hooks = NULL;
1306 signal_add_class_closure (node, 0, class_closure);
1313 g_signal_new_valist (const gchar *signal_name,
1315 GSignalFlags signal_flags,
1316 GClosure *class_closure,
1317 GSignalAccumulator accumulator,
1319 GSignalCMarshaller c_marshaller,
1330 param_types = g_new (GType, n_params);
1332 for (i = 0; i < n_params; i++)
1333 param_types[i] = va_arg (args, GType);
1338 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1339 class_closure, accumulator, accu_data, c_marshaller,
1340 return_type, n_params, param_types);
1341 g_free (param_types);
1347 signal_destroy_R (SignalNode *signal_node)
1349 SignalNode node = *signal_node;
1351 signal_node->destroyed = TRUE;
1353 /* reentrancy caution, zero out real contents first */
1354 signal_node->n_params = 0;
1355 signal_node->param_types = NULL;
1356 signal_node->return_type = 0;
1357 signal_node->class_closure_bsa = NULL;
1358 signal_node->accumulator = NULL;
1359 signal_node->c_marshaller = NULL;
1360 signal_node->emission_hooks = NULL;
1362 #ifdef G_ENABLE_DEBUG
1363 /* check current emissions */
1367 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1368 emission; emission = emission->next)
1369 if (emission->ihint.signal_id == node.signal_id)
1370 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1371 node.name, emission->instance);
1375 /* free contents that need to
1378 g_free (node.param_types);
1379 if (node.class_closure_bsa)
1383 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1385 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1387 g_closure_unref (cc->closure);
1389 g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1391 g_free (node.accumulator);
1392 if (node.emission_hooks)
1394 g_hook_list_clear (node.emission_hooks);
1395 g_free (node.emission_hooks);
1401 g_signal_override_class_closure (guint signal_id,
1402 GType instance_type,
1403 GClosure *class_closure)
1407 g_return_if_fail (signal_id > 0);
1408 g_return_if_fail (class_closure != NULL);
1411 node = LOOKUP_SIGNAL_NODE (signal_id);
1412 if (!g_type_is_a (instance_type, node->itype))
1413 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1416 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1418 if (cc && cc->instance_type == instance_type)
1419 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id);
1421 signal_add_class_closure (node, instance_type, class_closure);
1427 g_signal_chain_from_overridden (const GValue *instance_and_params,
1428 GValue *return_value)
1430 GType chain_type = 0, restore_type = 0;
1431 Emission *emission = NULL;
1432 GClosure *closure = NULL;
1436 g_return_if_fail (instance_and_params != NULL);
1437 instance = g_value_peek_pointer (instance_and_params);
1438 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1441 emission = emission_find_innermost (instance);
1444 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1446 g_assert (node != NULL); /* paranoid */
1448 /* we should probably do the same parameter checks as g_signal_emit() here.
1450 if (emission->chain_type != G_TYPE_NONE)
1452 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1454 g_assert (cc != NULL); /* closure currently in call stack */
1456 n_params = node->n_params;
1457 restore_type = cc->instance_type;
1458 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1459 if (cc && cc->instance_type != restore_type)
1461 closure = cc->closure;
1462 chain_type = cc->instance_type;
1466 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1469 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1472 emission->chain_type = chain_type;
1474 g_closure_invoke (closure,
1477 instance_and_params,
1480 emission->chain_type = restore_type;
1485 GSignalInvocationHint*
1486 g_signal_get_invocation_hint (gpointer instance)
1488 Emission *emission = NULL;
1490 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1493 emission = emission_find_innermost (instance);
1496 return emission ? &emission->ihint : NULL;
1500 g_signal_connect_closure_by_id (gpointer instance,
1507 gulong handler_seq_no = 0;
1509 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1510 g_return_val_if_fail (signal_id > 0, 0);
1511 g_return_val_if_fail (closure != NULL, 0);
1514 node = LOOKUP_SIGNAL_NODE (signal_id);
1517 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1518 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1519 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1520 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1523 Handler *handler = handler_new (after);
1525 handler_seq_no = handler->sequential_number;
1526 handler->detail = detail;
1527 handler->closure = g_closure_ref (closure);
1528 g_closure_sink (closure);
1529 handler_insert (signal_id, instance, handler);
1530 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1531 g_closure_set_marshal (closure, node->c_marshaller);
1535 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1538 return handler_seq_no;
1542 g_signal_connect_closure (gpointer instance,
1543 const gchar *detailed_signal,
1548 gulong handler_seq_no = 0;
1552 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1553 g_return_val_if_fail (detailed_signal != NULL, 0);
1554 g_return_val_if_fail (closure != NULL, 0);
1557 itype = G_TYPE_FROM_INSTANCE (instance);
1558 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1561 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1563 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1564 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1565 else if (!g_type_is_a (itype, node->itype))
1566 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1569 Handler *handler = handler_new (after);
1571 handler_seq_no = handler->sequential_number;
1572 handler->detail = detail;
1573 handler->closure = g_closure_ref (closure);
1574 g_closure_sink (closure);
1575 handler_insert (signal_id, instance, handler);
1576 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1577 g_closure_set_marshal (handler->closure, node->c_marshaller);
1581 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1584 return handler_seq_no;
1588 g_signal_connect_data (gpointer instance,
1589 const gchar *detailed_signal,
1590 GCallback c_handler,
1592 GClosureNotify destroy_data,
1593 GConnectFlags connect_flags)
1596 gulong handler_seq_no = 0;
1599 gboolean swapped, after;
1601 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1602 g_return_val_if_fail (detailed_signal != NULL, 0);
1603 g_return_val_if_fail (c_handler != NULL, 0);
1605 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1606 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1609 itype = G_TYPE_FROM_INSTANCE (instance);
1610 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1613 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1615 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1616 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1617 else if (!g_type_is_a (itype, node->itype))
1618 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1621 Handler *handler = handler_new (after);
1623 handler_seq_no = handler->sequential_number;
1624 handler->detail = detail;
1625 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1626 g_closure_sink (handler->closure);
1627 handler_insert (signal_id, instance, handler);
1628 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1629 g_closure_set_marshal (handler->closure, node->c_marshaller);
1633 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1636 return handler_seq_no;
1640 g_signal_handler_block (gpointer instance,
1645 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1646 g_return_if_fail (handler_id > 0);
1649 handler = handler_lookup (instance, handler_id, NULL);
1652 #ifndef G_DISABLE_CHECKS
1653 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1654 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1656 handler->block_count += 1;
1659 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1664 g_signal_handler_unblock (gpointer instance,
1669 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1670 g_return_if_fail (handler_id > 0);
1673 handler = handler_lookup (instance, handler_id, NULL);
1676 if (handler->block_count)
1677 handler->block_count -= 1;
1679 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1682 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1687 g_signal_handler_disconnect (gpointer instance,
1693 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1694 g_return_if_fail (handler_id > 0);
1697 handler = handler_lookup (instance, handler_id, &signal_id);
1700 handler->sequential_number = 0;
1701 handler->block_count = 1;
1702 handler_unref_R (signal_id, instance, handler);
1705 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1710 g_signal_handler_is_connected (gpointer instance,
1716 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1717 g_return_val_if_fail (handler_id > 0, FALSE);
1720 handler = handler_lookup (instance, handler_id, NULL);
1721 connected = handler != NULL;
1728 g_signal_handlers_destroy (gpointer instance)
1730 GBSearchArray *hlbsa;
1732 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1735 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1740 /* reentrancy caution, delete instance trace first */
1741 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1743 for (i = 0; i < hlbsa->n_nodes; i++)
1745 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1746 Handler *handler = hlist->handlers;
1750 Handler *tmp = handler;
1752 handler = tmp->next;
1753 tmp->block_count = 1;
1754 /* cruel unlink, this works because _all_ handlers vanish */
1757 if (tmp->sequential_number)
1759 tmp->sequential_number = 0;
1760 handler_unref_R (0, NULL, tmp);
1764 g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
1770 g_signal_handler_find (gpointer instance,
1771 GSignalMatchType mask,
1778 gulong handler_seq_no = 0;
1780 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1781 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1783 if (mask & G_SIGNAL_MATCH_MASK)
1785 HandlerMatch *mlist;
1788 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1791 handler_seq_no = mlist->handler->sequential_number;
1792 handler_match_free1_R (mlist, instance);
1797 return handler_seq_no;
1801 signal_handlers_foreach_matched_R (gpointer instance,
1802 GSignalMatchType mask,
1808 void (*callback) (gpointer instance,
1809 gulong handler_seq_no))
1811 HandlerMatch *mlist;
1812 guint n_handlers = 0;
1814 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1818 if (mlist->handler->sequential_number)
1821 callback (instance, mlist->handler->sequential_number);
1824 mlist = handler_match_free1_R (mlist, instance);
1831 g_signal_handlers_block_matched (gpointer instance,
1832 GSignalMatchType mask,
1839 guint n_handlers = 0;
1841 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1842 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1844 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1847 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1848 closure, func, data,
1849 g_signal_handler_block);
1857 g_signal_handlers_unblock_matched (gpointer instance,
1858 GSignalMatchType mask,
1865 guint n_handlers = 0;
1867 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1868 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1870 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1873 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1874 closure, func, data,
1875 g_signal_handler_unblock);
1883 g_signal_handlers_disconnect_matched (gpointer instance,
1884 GSignalMatchType mask,
1891 guint n_handlers = 0;
1893 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1894 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1896 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1899 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1900 closure, func, data,
1901 g_signal_handler_disconnect);
1909 g_signal_has_handler_pending (gpointer instance,
1912 gboolean may_be_blocked)
1914 HandlerMatch *mlist;
1915 gboolean has_pending;
1917 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1918 g_return_val_if_fail (signal_id > 0, FALSE);
1923 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1925 if (!(node->flags & G_SIGNAL_DETAILED))
1927 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1932 mlist = handlers_find (instance,
1933 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1934 signal_id, detail, NULL, NULL, NULL, TRUE);
1938 handler_match_free1_R (mlist, instance);
1941 has_pending = FALSE;
1948 g_signal_emitv (const GValue *instance_and_params,
1951 GValue *return_value)
1953 const GValue *param_values;
1956 #ifdef G_ENABLE_DEBUG
1960 g_return_if_fail (instance_and_params != NULL);
1961 instance = g_value_peek_pointer (instance_and_params);
1962 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1963 g_return_if_fail (signal_id > 0);
1965 param_values = instance_and_params + 1;
1968 node = LOOKUP_SIGNAL_NODE (signal_id);
1969 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1971 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1975 #ifdef G_ENABLE_DEBUG
1976 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1978 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1982 for (i = 0; i < node->n_params; i++)
1983 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1985 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1987 type_debug_name (node->param_types[i]),
1990 G_VALUE_TYPE_NAME (param_values + i));
1994 if (node->return_type != G_TYPE_NONE)
1998 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
2000 type_debug_name (node->return_type),
2005 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2007 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2009 type_debug_name (node->return_type),
2011 G_VALUE_TYPE_NAME (return_value));
2017 return_value = NULL;
2018 #endif /* G_ENABLE_DEBUG */
2021 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2025 g_signal_emit_valist (gpointer instance,
2030 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2031 GType signal_return_type;
2032 GValue *param_values;
2036 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2037 g_return_if_fail (signal_id > 0);
2040 node = LOOKUP_SIGNAL_NODE (signal_id);
2041 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2043 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2047 #ifndef G_DISABLE_CHECKS
2048 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2050 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2054 #endif /* !G_DISABLE_CHECKS */
2056 n_params = node->n_params;
2057 signal_return_type = node->return_type;
2058 if (node->n_params < MAX_STACK_VALUES)
2059 instance_and_params = stack_values;
2062 free_me = g_new (GValue, node->n_params + 1);
2063 instance_and_params = free_me;
2065 param_values = instance_and_params + 1;
2066 for (i = 0; i < node->n_params; i++)
2069 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2070 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2072 param_values[i].g_type = 0;
2074 g_value_init (param_values + i, ptype);
2075 G_VALUE_COLLECT (param_values + i,
2077 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2081 g_warning ("%s: %s", G_STRLOC, error);
2084 /* we purposely leak the value here, it might not be
2085 * in a sane state if an error condition occoured
2088 g_value_unset (param_values + i);
2096 instance_and_params->g_type = 0;
2097 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2098 g_value_set_instance (instance_and_params, instance);
2099 if (signal_return_type == G_TYPE_NONE)
2100 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2103 GValue return_value = { 0, };
2104 gchar *error = NULL;
2105 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2106 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2108 g_value_init (&return_value, rtype);
2110 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2112 G_VALUE_LCOPY (&return_value,
2114 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2117 g_value_unset (&return_value);
2120 g_warning ("%s: %s", G_STRLOC, error);
2123 /* we purposely leak the value here, it might not be
2124 * in a sane state if an error condition occoured
2128 for (i = 0; i < n_params; i++)
2129 g_value_unset (param_values + i);
2130 g_value_unset (instance_and_params);
2136 g_signal_emit (gpointer instance,
2143 va_start (var_args, detail);
2144 g_signal_emit_valist (instance, signal_id, detail, var_args);
2149 g_signal_emit_by_name (gpointer instance,
2150 const gchar *detailed_signal,
2156 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2157 g_return_if_fail (detailed_signal != NULL);
2160 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2167 va_start (var_args, detailed_signal);
2168 g_signal_emit_valist (instance, signal_id, detail, var_args);
2172 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2175 static inline gboolean
2176 accumulate (GSignalInvocationHint *ihint,
2177 GValue *return_accu,
2178 GValue *handler_return,
2179 SignalAccumulator *accumulator)
2181 gboolean continue_emission;
2186 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2187 g_value_reset (handler_return);
2189 return continue_emission;
2193 signal_emit_unlocked_R (SignalNode *node,
2196 GValue *emission_return,
2197 const GValue *instance_and_params)
2199 SignalAccumulator *accumulator;
2201 GClosure *class_closure;
2203 Handler *handler_list = NULL;
2204 GValue *return_accu, accu = { 0, };
2206 gulong max_sequential_handler_number;
2207 gboolean return_value_altered = FALSE;
2209 #ifdef G_ENABLE_DEBUG
2210 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2212 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2213 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2216 if (g_trap_instance_signals == instance)
2219 #endif /* G_ENABLE_DEBUG */
2222 signal_id = node->signal_id;
2223 if (node->flags & G_SIGNAL_NO_RECURSE)
2225 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2229 node->state = EMISSION_RESTART;
2231 return return_value_altered;
2234 accumulator = node->accumulator;
2238 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2239 return_accu = &accu;
2243 return_accu = emission_return;
2244 emission.instance = instance;
2245 emission.ihint.signal_id = node->signal_id;
2246 emission.ihint.detail = detail;
2247 emission.ihint.run_type = 0;
2249 emission.chain_type = G_TYPE_NONE;
2250 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2251 class_closure = signal_lookup_closure (node, instance);
2256 handler_unref_R (signal_id, instance, handler_list);
2257 max_sequential_handler_number = g_handler_sequential_number;
2258 hlist = handler_list_lookup (signal_id, instance);
2259 handler_list = hlist ? hlist->handlers : NULL;
2261 handler_ref (handler_list);
2263 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2265 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2267 emission.state = EMISSION_RUN;
2269 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2271 g_closure_invoke (class_closure,
2274 instance_and_params,
2276 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2277 emission.state == EMISSION_RUN)
2278 emission.state = EMISSION_STOP;
2280 emission.chain_type = G_TYPE_NONE;
2281 return_value_altered = TRUE;
2283 if (emission.state == EMISSION_STOP)
2285 else if (emission.state == EMISSION_RESTART)
2289 if (node->emission_hooks)
2291 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2294 emission.state = EMISSION_HOOK;
2295 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2298 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2300 if (!signal_hook->detail || signal_hook->detail == detail)
2302 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2304 was_in_call = G_HOOK_IN_CALL (hook);
2305 hook->flags |= G_HOOK_FLAG_IN_CALL;
2307 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2310 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2312 g_hook_destroy_link (node->emission_hooks, hook);
2314 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2317 if (emission.state == EMISSION_RESTART)
2323 Handler *handler = handler_list;
2325 emission.state = EMISSION_RUN;
2326 handler_ref (handler);
2333 handler_unref_R (signal_id, instance, handler_list);
2334 handler_list = handler;
2337 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2338 handler->sequential_number < max_sequential_handler_number)
2341 g_closure_invoke (handler->closure,
2344 instance_and_params,
2346 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2347 emission.state == EMISSION_RUN)
2348 emission.state = EMISSION_STOP;
2350 return_value_altered = TRUE;
2352 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2355 tmp = handler->next;
2359 handler_unref_R (signal_id, instance, handler_list);
2360 handler_list = handler;
2365 if (emission.state == EMISSION_STOP)
2367 else if (emission.state == EMISSION_RESTART)
2371 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2373 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2375 emission.state = EMISSION_RUN;
2377 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2379 g_closure_invoke (class_closure,
2382 instance_and_params,
2384 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2385 emission.state == EMISSION_RUN)
2386 emission.state = EMISSION_STOP;
2388 emission.chain_type = G_TYPE_NONE;
2389 return_value_altered = TRUE;
2391 if (emission.state == EMISSION_STOP)
2393 else if (emission.state == EMISSION_RESTART)
2399 Handler *handler = handler_list;
2401 emission.state = EMISSION_RUN;
2402 handler_ref (handler);
2407 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2408 handler->sequential_number < max_sequential_handler_number)
2411 g_closure_invoke (handler->closure,
2414 instance_and_params,
2416 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2417 emission.state == EMISSION_RUN)
2418 emission.state = EMISSION_STOP;
2420 return_value_altered = TRUE;
2422 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2425 tmp = handler->next;
2429 handler_unref_R (signal_id, instance, handler);
2434 if (emission.state == EMISSION_STOP)
2436 else if (emission.state == EMISSION_RESTART)
2442 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2444 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2446 gboolean need_unset = FALSE;
2448 emission.state = EMISSION_STOP;
2450 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2452 if (node->return_type != G_TYPE_NONE && !accumulator)
2454 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2457 g_closure_invoke (class_closure,
2458 node->return_type != G_TYPE_NONE ? &accu : NULL,
2460 instance_and_params,
2463 g_value_unset (&accu);
2465 emission.chain_type = G_TYPE_NONE;
2467 if (emission.state == EMISSION_RESTART)
2472 handler_unref_R (signal_id, instance, handler_list);
2474 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2477 g_value_unset (&accu);
2479 return return_value_altered;
2483 type_debug_name (GType type)
2487 const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2488 return name ? name : "<unknown>";
2494 /* --- compile standard marshallers --- */
2495 #include "gobject.h"
2497 #include "gmarshal.c"