1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2000-2001 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * this code is based on the original GtkSignal implementation
20 * for the Gtk+ library by Peter Mattis <petm@xcf.berkeley.edu>
28 #include "gbsearcharray.h"
29 #include "gvaluecollector.h"
30 #include "gvaluetypes.h"
35 /* pre allocation configurations
37 #define MAX_STACK_VALUES (16)
38 #define HANDLER_PRE_ALLOC (48)
39 #define EMISSION_PRE_ALLOC (16)
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 inline void emission_push (Emission **emission_list_p,
136 EmissionState *state_p);
137 static inline void emission_pop (Emission **emission_list_p,
138 EmissionState *state_p);
139 static inline Emission* emission_find (Emission *emission_list,
143 static gint signal_key_cmp (gconstpointer node1,
144 gconstpointer node2);
145 static gboolean signal_emit_unlocked_R (SignalNode *node,
148 GValue *return_value,
149 const GValue *instance_and_params);
152 /* --- structures --- */
155 GSignalAccumulator func;
163 #define SIGNAL_HOOK(hook) ((SignalHook*) (hook))
167 /* permanent portion */
173 /* reinitializable portion */
176 GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
177 GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */
178 GClosure *class_closure;
179 SignalAccumulator *accumulator;
180 GSignalCMarshaller c_marshaller;
181 GHookList *emission_hooks;
197 EmissionState *state_p;
207 gulong sequential_number;
211 guint ref_count : 16;
212 #define HANDLER_MAX_REF_COUNT (1 << 16)
213 guint block_count : 12;
214 #define HANDLER_MAX_BLOCK_COUNT (1 << 12)
229 /* --- variables --- */
230 static GBSearchArray g_signal_key_bsa = G_STATIC_BSEARCH_ARRAY_INIT (sizeof (SignalKey),
232 G_BSEARCH_ARRAY_ALIGN_POWER2);
233 static GHashTable *g_handler_list_bsa_ht = NULL;
234 static Emission *g_recursive_emissions = NULL;
235 static Emission *g_restart_emissions = NULL;
236 static GTrashStack *g_handler_ts = NULL;
237 static GTrashStack *g_emission_ts = NULL;
238 static gulong g_handler_sequential_number = 1;
239 G_LOCK_DEFINE_STATIC (g_signal_mutex);
240 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
241 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
244 /* --- signal nodes --- */
245 static guint g_n_signal_nodes = 0;
246 static SignalNode **g_signal_nodes = NULL;
248 static inline SignalNode*
249 LOOKUP_SIGNAL_NODE (register guint signal_id)
251 if (signal_id < g_n_signal_nodes)
252 return g_signal_nodes[signal_id];
258 /* --- functions --- */
260 signal_id_lookup (GQuark quark,
263 GType *ifaces, type = itype;
269 /* try looking up signals for this type and its anchestors */
272 SignalKey *signal_key;
275 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
278 return signal_key->signal_id;
280 type = g_type_parent (type);
284 /* no luck, try interfaces it exports */
285 ifaces = g_type_interfaces (itype, &n_ifaces);
288 SignalKey *signal_key;
290 key.itype = ifaces[n_ifaces];
291 signal_key = g_bsearch_array_lookup (&g_signal_key_bsa, &key);
296 return signal_key->signal_id;
305 handler_lists_cmp (gconstpointer node1,
308 const HandlerList *hlist1 = node1, *hlist2 = node2;
310 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
313 static inline HandlerList*
314 handler_list_ensure (guint signal_id,
317 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
322 hlbsa = g_bsearch_array_new (sizeof (HandlerList), handler_lists_cmp, G_BSEARCH_ARRAY_DEFER_SHRINK);
323 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
325 key.signal_id = signal_id;
328 return g_bsearch_array_insert (hlbsa, &key, FALSE);
331 static inline HandlerList*
332 handler_list_lookup (guint signal_id,
335 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
338 key.signal_id = signal_id;
340 return hlbsa ? g_bsearch_array_lookup (hlbsa, &key) : NULL;
344 handler_lookup (gpointer instance,
348 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
354 for (i = 0; i < hlbsa->n_nodes; i++)
356 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
359 for (handler = hlist->handlers; handler; handler = handler->next)
360 if (handler->sequential_number == handler_id)
363 *signal_id_p = hlist->signal_id;
373 static inline HandlerMatch*
374 handler_match_prepend (HandlerMatch *list,
380 /* yeah, we could use our own memchunk here, introducing yet more
381 * rarely used cached nodes and extra allocation overhead.
382 * instead, we use GList* nodes, since they are exactly the size
383 * we need and are already cached. g_signal_init() asserts this.
385 node = (HandlerMatch*) g_list_alloc ();
386 node->handler = handler;
388 node->d.signal_id = signal_id;
389 handler_ref (handler);
393 static inline HandlerMatch*
394 handler_match_free1_R (HandlerMatch *node,
397 HandlerMatch *next = node->next;
399 handler_unref_R (node->d.signal_id, instance, node->handler);
400 g_list_free_1 ((GList*) node);
406 handlers_find (gpointer instance,
407 GSignalMatchType mask,
413 gboolean one_and_only)
415 HandlerMatch *mlist = NULL;
417 if (mask & G_SIGNAL_MATCH_ID)
419 HandlerList *hlist = handler_list_lookup (signal_id, instance);
421 SignalNode *node = NULL;
423 if (mask & G_SIGNAL_MATCH_FUNC)
425 node = LOOKUP_SIGNAL_NODE (signal_id);
426 if (!node || !node->c_marshaller)
431 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
432 if (handler->sequential_number &&
433 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
434 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
435 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
436 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
437 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
438 handler->closure->meta_marshal == 0 &&
439 ((GCClosure*) handler->closure)->callback == func)))
441 mlist = handler_match_prepend (mlist, handler, signal_id);
448 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
455 for (i = 0; i < hlbsa->n_nodes; i++)
457 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
458 SignalNode *node = NULL;
461 if (!(mask & G_SIGNAL_MATCH_FUNC))
463 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
464 if (!node->c_marshaller)
468 for (handler = hlist->handlers; handler; handler = handler->next)
469 if (handler->sequential_number &&
470 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
471 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
472 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
473 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
474 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
475 handler->closure->meta_marshal == 0 &&
476 ((GCClosure*) handler->closure)->callback == func)))
478 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
489 static inline Handler*
490 handler_new (gboolean after)
492 Handler *handler = g_generic_node_alloc (&g_handler_ts,
495 #ifndef G_DISABLE_CHECKS
496 if (g_handler_sequential_number < 1)
497 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
500 handler->sequential_number = g_handler_sequential_number++;
501 handler->prev = NULL;
502 handler->next = NULL;
504 handler->ref_count = 1;
505 handler->block_count = 0;
506 handler->after = after != FALSE;
507 handler->closure = NULL;
513 handler_ref (Handler *handler)
515 g_return_if_fail (handler->ref_count > 0);
517 #ifndef G_DISABLE_CHECKS
518 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
519 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
522 handler->ref_count += 1;
526 handler_unref_R (guint signal_id,
530 g_return_if_fail (handler->ref_count > 0);
532 handler->ref_count -= 1;
533 if (!handler->ref_count)
536 handler->next->prev = handler->prev;
537 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
538 handler->prev->next = handler->next;
541 HandlerList *hlist = handler_list_lookup (signal_id, instance);
543 hlist->handlers = handler->next;
546 g_closure_unref (handler->closure);
548 g_generic_node_free (&g_handler_ts, handler);
553 handler_insert (guint signal_id,
559 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
561 hlist = handler_list_ensure (signal_id, instance);
562 if (!hlist->handlers)
563 hlist->handlers = handler;
564 else if (hlist->handlers->after && !handler->after)
566 handler->next = hlist->handlers;
567 hlist->handlers->prev = handler;
568 hlist->handlers = handler;
572 Handler *tmp = hlist->handlers;
578 while (tmp->next && !tmp->next->after)
581 tmp->next->prev = handler;
582 handler->next = tmp->next;
589 emission_push (Emission **emission_list_p,
593 EmissionState *state_p)
595 Emission *emission = g_generic_node_alloc (&g_emission_ts,
598 emission->next = *emission_list_p;
599 emission->signal_id = signal_id;
600 emission->detail = detail;
601 emission->instance = instance;
602 emission->state_p = state_p;
603 *emission_list_p = emission;
607 emission_pop (Emission **emission_list_p,
608 EmissionState *state_p)
610 Emission **loc = emission_list_p, *emission = *loc;
612 while (emission->state_p != state_p)
614 loc = &emission->next;
617 *loc = emission->next;
618 g_generic_node_free (&g_emission_ts, emission);
621 static inline Emission*
622 emission_find (Emission *emission_list,
629 for (emission = emission_list; emission; emission = emission->next)
630 if (emission->instance == instance &&
631 emission->signal_id == signal_id &&
632 emission->detail == detail)
638 signal_key_cmp (gconstpointer node1,
641 const SignalKey *key1 = node1, *key2 = node2;
643 if (key1->itype == key2->itype)
644 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
646 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
650 g_signal_init (void) /* sync with gtype.c */
653 if (!g_n_signal_nodes)
655 /* handler_id_node_prepend() requires this */
656 g_assert (sizeof (GList) == sizeof (HandlerMatch));
658 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
659 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
661 /* invalid (0) signal_id */
662 g_n_signal_nodes = 1;
663 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
664 g_signal_nodes[0] = NULL;
670 _g_signals_destroy (GType itype)
675 for (i = 1; i < g_n_signal_nodes; i++)
677 SignalNode *node = g_signal_nodes[i];
679 if (node->itype == itype)
682 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
684 g_type_name (node->itype));
686 signal_destroy_R (node);
693 g_signal_stop_emission (gpointer instance,
699 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
700 g_return_if_fail (signal_id > 0);
703 node = LOOKUP_SIGNAL_NODE (signal_id);
704 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
706 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
710 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
712 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
713 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
717 if (*emission->state_p == EMISSION_HOOK)
718 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
719 node->name, instance);
720 else if (*emission->state_p == EMISSION_RUN)
721 *emission->state_p = EMISSION_STOP;
724 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
725 node->name, instance);
728 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
733 signal_finalize_hook (GHookList *hook_list,
736 GDestroyNotify destroy = hook->destroy;
740 hook->destroy = NULL;
742 destroy (hook->data);
748 g_signal_add_emission_hook (guint signal_id,
750 GSignalEmissionHook hook_func,
752 GDestroyNotify data_destroy)
754 static gulong seq_hook_id = 1;
757 SignalHook *signal_hook;
759 g_return_val_if_fail (signal_id > 0, 0);
760 g_return_val_if_fail (hook_func != NULL, 0);
763 node = LOOKUP_SIGNAL_NODE (signal_id);
764 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
766 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
770 if (detail && !(node->flags & G_SIGNAL_DETAILED))
772 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
776 if (!node->emission_hooks)
778 node->emission_hooks = g_new (GHookList, 1);
779 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
780 node->emission_hooks->finalize_hook = signal_finalize_hook;
782 hook = g_hook_alloc (node->emission_hooks);
783 hook->data = hook_data;
784 hook->func = hook_func;
785 hook->destroy = data_destroy;
786 signal_hook = SIGNAL_HOOK (hook);
787 signal_hook->detail = detail;
788 node->emission_hooks->seq_id = seq_hook_id;
789 g_hook_append (node->emission_hooks, hook);
790 seq_hook_id = node->emission_hooks->seq_id;
793 return hook->hook_id;
797 g_signal_remove_emission_hook (guint signal_id,
802 g_return_if_fail (signal_id > 0);
803 g_return_if_fail (hook_id > 0);
806 node = LOOKUP_SIGNAL_NODE (signal_id);
807 if (!node || node->destroyed)
808 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
809 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
810 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
815 signal_parse_name (const gchar *name,
818 gboolean force_quark)
820 const gchar *colon = strchr (name, ':');
825 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
826 if (signal_id && detail_p)
829 else if (colon[1] == ':')
832 guint l = colon - name;
836 memcpy (buffer, name, l);
838 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
842 gchar *signal = g_new (gchar, l + 1);
844 memcpy (signal, name, l);
846 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
850 if (signal_id && detail_p)
851 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
859 g_signal_parse_name (const gchar *detailed_signal,
863 gboolean force_detail_quark)
869 g_return_val_if_fail (detailed_signal != NULL, FALSE);
870 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
873 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
876 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
877 if (!node || node->destroyed ||
878 (detail && !(node->flags & G_SIGNAL_DETAILED)))
882 *signal_id_p = signal_id;
890 g_signal_stop_emission_by_name (gpointer instance,
891 const gchar *detailed_signal)
897 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
898 g_return_if_fail (detailed_signal != NULL);
901 itype = G_TYPE_FROM_INSTANCE (instance);
902 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
905 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
907 if (detail && !(node->flags & G_SIGNAL_DETAILED))
908 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
909 else if (!g_type_is_a (itype, node->itype))
910 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
913 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
914 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
918 if (*emission->state_p == EMISSION_HOOK)
919 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
920 node->name, instance);
921 else if (*emission->state_p == EMISSION_RUN)
922 *emission->state_p = EMISSION_STOP;
925 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
926 node->name, instance);
930 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
935 g_signal_lookup (const gchar *name,
940 g_return_val_if_fail (name != NULL, 0);
941 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
944 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
948 /* give elaborate warnings */
949 if (!g_type_name (itype))
950 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%u'",
952 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
953 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
954 name, g_type_name (itype));
955 else if (!g_type_class_peek (itype))
956 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
957 name, g_type_name (itype));
964 g_signal_list_ids (GType itype,
972 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
973 g_return_val_if_fail (n_ids != NULL, NULL);
976 keys = g_signal_key_bsa.nodes;
977 n_nodes = g_signal_key_bsa.n_nodes;
978 result = g_array_new (FALSE, FALSE, sizeof (guint));
980 for (i = 0; i < n_nodes; i++)
981 if (keys[i].itype == itype)
983 const gchar *name = g_quark_to_string (keys[i].quark);
985 /* Signal names with "_" in them are aliases to the same
986 * name with "-" instead of "_".
988 if (!strchr (name, '_'))
989 g_array_append_val (result, keys[i].signal_id);
991 *n_ids = result->len;
995 /* give elaborate warnings */
996 if (!g_type_name (itype))
997 g_warning (G_STRLOC ": unable to list signals for invalid type id `%u'",
999 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1000 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1001 g_type_name (itype));
1002 else if (!g_type_class_peek (itype))
1003 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1004 g_type_name (itype));
1007 return (guint*) g_array_free (result, FALSE);
1010 G_CONST_RETURN gchar*
1011 g_signal_name (guint signal_id)
1017 node = LOOKUP_SIGNAL_NODE (signal_id);
1018 name = node ? node->name : NULL;
1025 g_signal_query (guint signal_id,
1026 GSignalQuery *query)
1030 g_return_if_fail (query != NULL);
1033 node = LOOKUP_SIGNAL_NODE (signal_id);
1034 if (!node || node->destroyed)
1035 query->signal_id = 0;
1038 query->signal_id = node->signal_id;
1039 query->signal_name = node->name;
1040 query->itype = node->itype;
1041 query->signal_flags = node->flags;
1042 query->return_type = node->return_type;
1043 query->n_params = node->n_params;
1044 query->param_types = node->param_types;
1050 g_signal_new_valist (const gchar *signal_name,
1052 GSignalFlags signal_flags,
1053 GClosure *class_closure,
1054 GSignalAccumulator accumulator,
1056 GSignalCMarshaller c_marshaller,
1067 param_types = g_new (GType, n_params);
1069 for (i = 0; i < n_params; i++)
1070 param_types[i] = va_arg (args, GType);
1075 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1076 class_closure, accumulator, accu_data, c_marshaller,
1077 return_type, n_params, param_types);
1078 g_free (param_types);
1084 g_signal_new (const gchar *signal_name,
1086 GSignalFlags signal_flags,
1088 GSignalAccumulator accumulator,
1090 GSignalCMarshaller c_marshaller,
1098 g_return_val_if_fail (signal_name != NULL, 0);
1100 va_start (args, n_params);
1102 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1103 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1104 accumulator, accu_data, c_marshaller,
1105 return_type, n_params, args);
1113 g_signal_newv (const gchar *signal_name,
1115 GSignalFlags signal_flags,
1116 GClosure *class_closure,
1117 GSignalAccumulator accumulator,
1119 GSignalCMarshaller c_marshaller,
1128 g_return_val_if_fail (signal_name != NULL, 0);
1129 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1131 g_return_val_if_fail (param_types != NULL, 0);
1132 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1133 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1134 g_return_val_if_fail (accumulator == NULL, 0);
1136 g_return_val_if_fail (accu_data == NULL, 0);
1138 name = g_strdup (signal_name);
1139 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1143 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1144 node = LOOKUP_SIGNAL_NODE (signal_id);
1145 if (node && !node->destroyed)
1147 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1149 g_type_name (node->itype),
1150 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1155 if (node && node->itype != itype)
1157 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1159 g_type_name (itype),
1160 g_type_name (node->itype));
1165 for (i = 0; i < n_params; i++)
1166 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1168 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1169 i + 1, g_type_name (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1174 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1176 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1177 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1182 if (return_type != G_TYPE_NONE &&
1183 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1185 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1186 g_type_name (itype), name,
1187 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE));
1193 /* setup permanent portion of signal node */
1198 signal_id = g_n_signal_nodes++;
1199 node = g_new (SignalNode, 1);
1200 node->signal_id = signal_id;
1201 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1202 g_signal_nodes[signal_id] = node;
1203 node->itype = itype;
1206 key.quark = g_quark_from_string (node->name);
1207 key.signal_id = signal_id;
1208 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1209 g_strdelimit (node->name, "_", '-');
1210 key.quark = g_quark_from_static_string (node->name);
1211 g_bsearch_array_insert (&g_signal_key_bsa, &key, FALSE);
1213 node->destroyed = FALSE;
1215 /* setup reinitializable portion */
1216 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1217 node->n_params = n_params;
1218 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1219 node->return_type = return_type;
1220 node->class_closure = class_closure ? g_closure_ref (class_closure) : NULL;
1222 g_closure_sink (class_closure);
1225 node->accumulator = g_new (SignalAccumulator, 1);
1226 node->accumulator->func = accumulator;
1227 node->accumulator->data = accu_data;
1230 node->accumulator = NULL;
1231 node->c_marshaller = c_marshaller;
1232 node->emission_hooks = NULL;
1233 if (node->c_marshaller && class_closure && G_CLOSURE_NEEDS_MARSHAL (class_closure))
1234 g_closure_set_marshal (class_closure, node->c_marshaller);
1240 signal_destroy_R (SignalNode *signal_node)
1242 SignalNode node = *signal_node;
1244 signal_node->destroyed = TRUE;
1246 /* reentrancy caution, zero out real contents first */
1247 signal_node->n_params = 0;
1248 signal_node->param_types = NULL;
1249 signal_node->return_type = 0;
1250 signal_node->class_closure = NULL;
1251 signal_node->accumulator = NULL;
1252 signal_node->c_marshaller = NULL;
1253 signal_node->emission_hooks = NULL;
1255 #ifdef G_ENABLE_DEBUG
1256 /* check current emissions */
1260 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1261 emission; emission = emission->next)
1262 if (emission->signal_id == node.signal_id)
1263 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1264 node.name, emission->instance);
1268 /* free contents that need to
1271 g_free (node.param_types);
1272 g_closure_unref (node.class_closure);
1273 g_free (node.accumulator);
1274 if (node.emission_hooks)
1276 g_hook_list_clear (node.emission_hooks);
1277 g_free (node.emission_hooks);
1283 g_signal_override_class_closure (guint signal_id,
1284 GType instance_type,
1285 GClosure *class_closure)
1290 g_signal_chain_from_overridden (const GValue *instance_and_params,
1292 GValue *return_value)
1297 g_signal_connect_closure_by_id (gpointer instance,
1304 gulong handler_seq_no = 0;
1306 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1307 g_return_val_if_fail (signal_id > 0, 0);
1308 g_return_val_if_fail (closure != NULL, 0);
1311 node = LOOKUP_SIGNAL_NODE (signal_id);
1314 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1315 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1316 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1317 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1320 Handler *handler = handler_new (after);
1322 handler_seq_no = handler->sequential_number;
1323 handler->detail = detail;
1324 handler->closure = g_closure_ref (closure);
1325 g_closure_sink (closure);
1326 handler_insert (signal_id, instance, handler);
1327 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1328 g_closure_set_marshal (closure, node->c_marshaller);
1332 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1335 return handler_seq_no;
1339 g_signal_connect_closure (gpointer instance,
1340 const gchar *detailed_signal,
1345 gulong handler_seq_no = 0;
1349 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1350 g_return_val_if_fail (detailed_signal != NULL, 0);
1351 g_return_val_if_fail (closure != NULL, 0);
1354 itype = G_TYPE_FROM_INSTANCE (instance);
1355 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1358 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1360 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1361 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1362 else if (!g_type_is_a (itype, node->itype))
1363 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1366 Handler *handler = handler_new (after);
1368 handler_seq_no = handler->sequential_number;
1369 handler->detail = detail;
1370 handler->closure = g_closure_ref (closure);
1371 g_closure_sink (closure);
1372 handler_insert (signal_id, instance, handler);
1373 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1374 g_closure_set_marshal (handler->closure, node->c_marshaller);
1378 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1381 return handler_seq_no;
1385 g_signal_connect_data (gpointer instance,
1386 const gchar *detailed_signal,
1387 GCallback c_handler,
1389 GClosureNotify destroy_data,
1390 GConnectFlags connect_flags)
1393 gulong handler_seq_no = 0;
1396 gboolean swapped, after;
1398 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1399 g_return_val_if_fail (detailed_signal != NULL, 0);
1400 g_return_val_if_fail (c_handler != NULL, 0);
1402 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1403 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1406 itype = G_TYPE_FROM_INSTANCE (instance);
1407 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1410 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1412 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1413 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1414 else if (!g_type_is_a (itype, node->itype))
1415 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1418 Handler *handler = handler_new (after);
1420 handler_seq_no = handler->sequential_number;
1421 handler->detail = detail;
1422 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1423 g_closure_sink (handler->closure);
1424 handler_insert (signal_id, instance, handler);
1425 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1426 g_closure_set_marshal (handler->closure, node->c_marshaller);
1430 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1433 return handler_seq_no;
1437 g_signal_handler_block (gpointer instance,
1442 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1443 g_return_if_fail (handler_id > 0);
1446 handler = handler_lookup (instance, handler_id, NULL);
1449 #ifndef G_DISABLE_CHECKS
1450 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1451 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1453 handler->block_count += 1;
1456 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1461 g_signal_handler_unblock (gpointer instance,
1466 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1467 g_return_if_fail (handler_id > 0);
1470 handler = handler_lookup (instance, handler_id, NULL);
1473 if (handler->block_count)
1474 handler->block_count -= 1;
1476 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1479 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1484 g_signal_handler_disconnect (gpointer instance,
1490 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1491 g_return_if_fail (handler_id > 0);
1494 handler = handler_lookup (instance, handler_id, &signal_id);
1497 handler->sequential_number = 0;
1498 handler->block_count = 1;
1499 handler_unref_R (signal_id, instance, handler);
1502 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1507 g_signal_handler_is_connected (gpointer instance,
1513 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1514 g_return_val_if_fail (handler_id > 0, FALSE);
1517 handler = handler_lookup (instance, handler_id, NULL);
1518 connected = handler != NULL;
1525 g_signal_handlers_destroy (gpointer instance)
1527 GBSearchArray *hlbsa;
1529 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1532 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1537 /* reentrancy caution, delete instance trace first */
1538 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1540 for (i = 0; i < hlbsa->n_nodes; i++)
1542 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1543 Handler *handler = hlist->handlers;
1547 Handler *tmp = handler;
1549 handler = tmp->next;
1550 tmp->block_count = 1;
1551 /* cruel unlink, this works because _all_ handlers vanish */
1554 if (tmp->sequential_number)
1556 tmp->sequential_number = 0;
1557 handler_unref_R (0, NULL, tmp);
1561 g_bsearch_array_destroy (hlbsa);
1567 g_signal_handler_find (gpointer instance,
1568 GSignalMatchType mask,
1575 gulong handler_seq_no = 0;
1577 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1578 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1580 if (mask & G_SIGNAL_MATCH_MASK)
1582 HandlerMatch *mlist;
1585 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1588 handler_seq_no = mlist->handler->sequential_number;
1589 handler_match_free1_R (mlist, instance);
1594 return handler_seq_no;
1598 signal_handlers_foreach_matched_R (gpointer instance,
1599 GSignalMatchType mask,
1605 void (*callback) (gpointer instance,
1606 gulong handler_seq_no))
1608 HandlerMatch *mlist;
1609 guint n_handlers = 0;
1611 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1615 if (mlist->handler->sequential_number)
1618 callback (instance, mlist->handler->sequential_number);
1621 mlist = handler_match_free1_R (mlist, instance);
1628 g_signal_handlers_block_matched (gpointer instance,
1629 GSignalMatchType mask,
1636 guint n_handlers = 0;
1638 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1639 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1641 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1644 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1645 closure, func, data,
1646 g_signal_handler_block);
1654 g_signal_handlers_unblock_matched (gpointer instance,
1655 GSignalMatchType mask,
1662 guint n_handlers = 0;
1664 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1665 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1667 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1670 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1671 closure, func, data,
1672 g_signal_handler_unblock);
1680 g_signal_handlers_disconnect_matched (gpointer instance,
1681 GSignalMatchType mask,
1688 guint n_handlers = 0;
1690 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1691 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1693 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1696 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1697 closure, func, data,
1698 g_signal_handler_disconnect);
1706 g_signal_has_handler_pending (gpointer instance,
1709 gboolean may_be_blocked)
1711 HandlerMatch *mlist;
1712 gboolean has_pending;
1714 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1715 g_return_val_if_fail (signal_id > 0, FALSE);
1720 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1722 if (!(node->flags & G_SIGNAL_DETAILED))
1724 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1729 mlist = handlers_find (instance,
1730 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1731 signal_id, detail, NULL, NULL, NULL, TRUE);
1735 handler_match_free1_R (mlist, instance);
1738 has_pending = FALSE;
1745 g_signal_emitv (const GValue *instance_and_params,
1748 GValue *return_value)
1750 const GValue *param_values;
1753 #ifdef G_ENABLE_DEBUG
1757 g_return_if_fail (instance_and_params != NULL);
1758 instance = g_value_peek_pointer (instance_and_params);
1759 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1760 g_return_if_fail (signal_id > 0);
1762 param_values = instance_and_params + 1;
1765 node = LOOKUP_SIGNAL_NODE (signal_id);
1766 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1768 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1772 #ifdef G_ENABLE_DEBUG
1773 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1775 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1779 for (i = 0; i < node->n_params; i++)
1780 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1782 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1784 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1787 G_VALUE_TYPE_NAME (param_values + i));
1791 if (node->return_type != G_TYPE_NONE)
1795 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1797 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1802 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1804 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1806 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1808 G_VALUE_TYPE_NAME (return_value));
1814 return_value = NULL;
1815 #endif /* G_ENABLE_DEBUG */
1818 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
1822 g_signal_emit_valist (gpointer instance,
1827 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1828 GType signal_return_type;
1829 GValue *param_values;
1833 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1834 g_return_if_fail (signal_id > 0);
1837 node = LOOKUP_SIGNAL_NODE (signal_id);
1838 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1840 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1844 #ifndef G_DISABLE_CHECKS
1845 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1847 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1851 #endif /* !G_DISABLE_CHECKS */
1853 n_params = node->n_params;
1854 signal_return_type = node->return_type;
1855 if (node->n_params < MAX_STACK_VALUES)
1856 instance_and_params = stack_values;
1859 free_me = g_new (GValue, node->n_params + 1);
1860 instance_and_params = free_me;
1862 param_values = instance_and_params + 1;
1863 for (i = 0; i < node->n_params; i++)
1866 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1867 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1869 param_values[i].g_type = 0;
1871 g_value_init (param_values + i, ptype);
1872 G_VALUE_COLLECT (param_values + i,
1874 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1878 g_warning ("%s: %s", G_STRLOC, error);
1881 /* we purposely leak the value here, it might not be
1882 * in a sane state if an error condition occoured
1885 g_value_unset (param_values + i);
1893 instance_and_params->g_type = 0;
1894 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1895 g_value_set_instance (instance_and_params, instance);
1896 if (signal_return_type == G_TYPE_NONE)
1897 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
1900 GValue return_value = { 0, };
1901 gchar *error = NULL;
1902 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1903 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
1905 g_value_init (&return_value, rtype);
1907 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
1909 G_VALUE_LCOPY (&return_value,
1911 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1914 g_value_unset (&return_value);
1917 g_warning ("%s: %s", G_STRLOC, error);
1920 /* we purposely leak the value here, it might not be
1921 * in a sane state if an error condition occoured
1925 for (i = 0; i < n_params; i++)
1926 g_value_unset (param_values + i);
1927 g_value_unset (instance_and_params);
1933 g_signal_emit (gpointer instance,
1940 va_start (var_args, detail);
1941 g_signal_emit_valist (instance, signal_id, detail, var_args);
1946 g_signal_emit_by_name (gpointer instance,
1947 const gchar *detailed_signal,
1953 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1954 g_return_if_fail (detailed_signal != NULL);
1957 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1964 va_start (var_args, detailed_signal);
1965 g_signal_emit_valist (instance, signal_id, detail, var_args);
1969 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1972 static inline gboolean
1973 accumulate (GSignalInvocationHint *ihint,
1974 GValue *return_accu,
1975 GValue *handler_return,
1976 SignalAccumulator *accumulator)
1978 gboolean continue_emission;
1983 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1984 g_value_reset (handler_return);
1986 return continue_emission;
1990 signal_emit_unlocked_R (SignalNode *node,
1993 GValue *emission_return,
1994 const GValue *instance_and_params)
1996 EmissionState emission_state = 0;
1997 SignalAccumulator *accumulator;
1998 GSignalInvocationHint ihint;
1999 GClosure *class_closure;
2001 Handler *handler_list = NULL;
2002 GValue *return_accu, accu = { 0, };
2004 gulong max_sequential_handler_number;
2005 gboolean return_value_altered = FALSE;
2007 #ifdef G_ENABLE_DEBUG
2008 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2010 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2011 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2014 if (g_trap_instance_signals == instance)
2017 #endif /* G_ENABLE_DEBUG */
2020 signal_id = node->signal_id;
2021 if (node->flags & G_SIGNAL_NO_RECURSE)
2023 Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
2027 *emission->state_p = EMISSION_RESTART;
2029 return return_value_altered;
2032 ihint.signal_id = node->signal_id;
2033 ihint.detail = detail;
2034 accumulator = node->accumulator;
2038 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2039 return_accu = &accu;
2043 return_accu = emission_return;
2044 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
2045 signal_id, detail, instance, &emission_state);
2046 class_closure = node->class_closure;
2051 handler_unref_R (signal_id, instance, handler_list);
2052 max_sequential_handler_number = g_handler_sequential_number;
2053 hlist = handler_list_lookup (signal_id, instance);
2054 handler_list = hlist ? hlist->handlers : NULL;
2056 handler_ref (handler_list);
2058 ihint.run_type = G_SIGNAL_RUN_FIRST;
2060 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2062 emission_state = EMISSION_RUN;
2065 g_closure_invoke (class_closure,
2068 instance_and_params,
2070 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2071 emission_state == EMISSION_RUN)
2072 emission_state = EMISSION_STOP;
2074 return_value_altered = TRUE;
2076 if (emission_state == EMISSION_STOP)
2078 else if (emission_state == EMISSION_RESTART)
2082 if (node->emission_hooks)
2084 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2087 emission_state = EMISSION_HOOK;
2088 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2091 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2093 if (!signal_hook->detail || signal_hook->detail == detail)
2095 GSignalEmissionHook hook_func = hook->func;
2097 was_in_call = G_HOOK_IN_CALL (hook);
2098 hook->flags |= G_HOOK_FLAG_IN_CALL;
2100 need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
2103 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2105 g_hook_destroy_link (node->emission_hooks, hook);
2107 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2110 if (emission_state == EMISSION_RESTART)
2116 Handler *handler = handler_list;
2118 emission_state = EMISSION_RUN;
2119 handler_ref (handler);
2126 handler_unref_R (signal_id, instance, handler_list);
2127 handler_list = handler;
2130 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2131 handler->sequential_number < max_sequential_handler_number)
2134 g_closure_invoke (handler->closure,
2137 instance_and_params,
2139 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2140 emission_state == EMISSION_RUN)
2141 emission_state = EMISSION_STOP;
2143 return_value_altered = TRUE;
2145 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2148 tmp = handler->next;
2152 handler_unref_R (signal_id, instance, handler_list);
2153 handler_list = handler;
2158 if (emission_state == EMISSION_STOP)
2160 else if (emission_state == EMISSION_RESTART)
2164 ihint.run_type = G_SIGNAL_RUN_LAST;
2166 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2168 emission_state = EMISSION_RUN;
2171 g_closure_invoke (class_closure,
2174 instance_and_params,
2176 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2177 emission_state == EMISSION_RUN)
2178 emission_state = EMISSION_STOP;
2180 return_value_altered = TRUE;
2182 if (emission_state == EMISSION_STOP)
2184 else if (emission_state == EMISSION_RESTART)
2190 Handler *handler = handler_list;
2192 emission_state = EMISSION_RUN;
2193 handler_ref (handler);
2198 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2199 handler->sequential_number < max_sequential_handler_number)
2202 g_closure_invoke (handler->closure,
2205 instance_and_params,
2207 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2208 emission_state == EMISSION_RUN)
2209 emission_state = EMISSION_STOP;
2211 return_value_altered = TRUE;
2213 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2216 tmp = handler->next;
2220 handler_unref_R (signal_id, instance, handler);
2225 if (emission_state == EMISSION_STOP)
2227 else if (emission_state == EMISSION_RESTART)
2233 ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2235 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2237 gboolean need_unset = FALSE;
2239 emission_state = EMISSION_STOP;
2242 if (node->return_type != G_TYPE_NONE && !accumulator)
2244 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2247 g_closure_invoke (class_closure,
2248 node->return_type != G_TYPE_NONE ? &accu : NULL,
2250 instance_and_params,
2253 g_value_unset (&accu);
2256 if (emission_state == EMISSION_RESTART)
2261 handler_unref_R (signal_id, instance, handler_list);
2263 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2266 g_value_unset (&accu);
2268 return return_value_altered;
2272 /* --- compile standard marshallers --- */
2273 #include "gobject.h"
2275 #include "gmarshal.c"