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_connect_closure_by_id (gpointer instance,
1290 gulong handler_seq_no = 0;
1292 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1293 g_return_val_if_fail (signal_id > 0, 0);
1294 g_return_val_if_fail (closure != NULL, 0);
1297 node = LOOKUP_SIGNAL_NODE (signal_id);
1300 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1301 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1302 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1303 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1306 Handler *handler = handler_new (after);
1308 handler_seq_no = handler->sequential_number;
1309 handler->detail = detail;
1310 handler->closure = g_closure_ref (closure);
1311 g_closure_sink (closure);
1312 handler_insert (signal_id, instance, handler);
1313 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1314 g_closure_set_marshal (closure, node->c_marshaller);
1318 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1321 return handler_seq_no;
1325 g_signal_connect_closure (gpointer instance,
1326 const gchar *detailed_signal,
1331 gulong handler_seq_no = 0;
1335 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1336 g_return_val_if_fail (detailed_signal != NULL, 0);
1337 g_return_val_if_fail (closure != NULL, 0);
1340 itype = G_TYPE_FROM_INSTANCE (instance);
1341 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1344 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1346 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1347 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1348 else if (!g_type_is_a (itype, node->itype))
1349 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1352 Handler *handler = handler_new (after);
1354 handler_seq_no = handler->sequential_number;
1355 handler->detail = detail;
1356 handler->closure = g_closure_ref (closure);
1357 g_closure_sink (closure);
1358 handler_insert (signal_id, instance, handler);
1359 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1360 g_closure_set_marshal (handler->closure, node->c_marshaller);
1364 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1367 return handler_seq_no;
1371 g_signal_connect_data (gpointer instance,
1372 const gchar *detailed_signal,
1373 GCallback c_handler,
1375 GClosureNotify destroy_data,
1376 GConnectFlags connect_flags)
1379 gulong handler_seq_no = 0;
1382 gboolean swapped, after;
1384 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1385 g_return_val_if_fail (detailed_signal != NULL, 0);
1386 g_return_val_if_fail (c_handler != NULL, 0);
1388 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1389 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1392 itype = G_TYPE_FROM_INSTANCE (instance);
1393 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1396 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1398 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1399 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1400 else if (!g_type_is_a (itype, node->itype))
1401 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1404 Handler *handler = handler_new (after);
1406 handler_seq_no = handler->sequential_number;
1407 handler->detail = detail;
1408 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1409 g_closure_sink (handler->closure);
1410 handler_insert (signal_id, instance, handler);
1411 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1412 g_closure_set_marshal (handler->closure, node->c_marshaller);
1416 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1419 return handler_seq_no;
1423 g_signal_handler_block (gpointer instance,
1428 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1429 g_return_if_fail (handler_id > 0);
1432 handler = handler_lookup (instance, handler_id, NULL);
1435 #ifndef G_DISABLE_CHECKS
1436 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1437 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1439 handler->block_count += 1;
1442 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1447 g_signal_handler_unblock (gpointer instance,
1452 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1453 g_return_if_fail (handler_id > 0);
1456 handler = handler_lookup (instance, handler_id, NULL);
1459 if (handler->block_count)
1460 handler->block_count -= 1;
1462 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1465 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1470 g_signal_handler_disconnect (gpointer instance,
1476 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1477 g_return_if_fail (handler_id > 0);
1480 handler = handler_lookup (instance, handler_id, &signal_id);
1483 handler->sequential_number = 0;
1484 handler->block_count = 1;
1485 handler_unref_R (signal_id, instance, handler);
1488 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1493 g_signal_handler_is_connected (gpointer instance,
1499 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1500 g_return_val_if_fail (handler_id > 0, FALSE);
1503 handler = handler_lookup (instance, handler_id, NULL);
1504 connected = handler != NULL;
1511 g_signal_handlers_destroy (gpointer instance)
1513 GBSearchArray *hlbsa;
1515 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1518 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1523 /* reentrancy caution, delete instance trace first */
1524 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1526 for (i = 0; i < hlbsa->n_nodes; i++)
1528 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, i);
1529 Handler *handler = hlist->handlers;
1533 Handler *tmp = handler;
1535 handler = tmp->next;
1536 tmp->block_count = 1;
1537 /* cruel unlink, this works because _all_ handlers vanish */
1540 if (tmp->sequential_number)
1542 tmp->sequential_number = 0;
1543 handler_unref_R (0, NULL, tmp);
1547 g_bsearch_array_destroy (hlbsa);
1553 g_signal_handler_find (gpointer instance,
1554 GSignalMatchType mask,
1561 gulong handler_seq_no = 0;
1563 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1564 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1566 if (mask & G_SIGNAL_MATCH_MASK)
1568 HandlerMatch *mlist;
1571 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1574 handler_seq_no = mlist->handler->sequential_number;
1575 handler_match_free1_R (mlist, instance);
1580 return handler_seq_no;
1584 signal_handlers_foreach_matched_R (gpointer instance,
1585 GSignalMatchType mask,
1591 void (*callback) (gpointer instance,
1592 gulong handler_seq_no))
1594 HandlerMatch *mlist;
1595 guint n_handlers = 0;
1597 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1601 if (mlist->handler->sequential_number)
1604 callback (instance, mlist->handler->sequential_number);
1607 mlist = handler_match_free1_R (mlist, instance);
1614 g_signal_handlers_block_matched (gpointer instance,
1615 GSignalMatchType mask,
1622 guint n_handlers = 0;
1624 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1625 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1627 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1630 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1631 closure, func, data,
1632 g_signal_handler_block);
1640 g_signal_handlers_unblock_matched (gpointer instance,
1641 GSignalMatchType mask,
1648 guint n_handlers = 0;
1650 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1651 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1653 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1656 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1657 closure, func, data,
1658 g_signal_handler_unblock);
1666 g_signal_handlers_disconnect_matched (gpointer instance,
1667 GSignalMatchType mask,
1674 guint n_handlers = 0;
1676 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1677 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1679 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1682 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1683 closure, func, data,
1684 g_signal_handler_disconnect);
1692 g_signal_has_handler_pending (gpointer instance,
1695 gboolean may_be_blocked)
1697 HandlerMatch *mlist;
1698 gboolean has_pending;
1700 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1701 g_return_val_if_fail (signal_id > 0, FALSE);
1706 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1708 if (!(node->flags & G_SIGNAL_DETAILED))
1710 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1715 mlist = handlers_find (instance,
1716 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1717 signal_id, detail, NULL, NULL, NULL, TRUE);
1721 handler_match_free1_R (mlist, instance);
1724 has_pending = FALSE;
1731 g_signal_emitv (const GValue *instance_and_params,
1734 GValue *return_value)
1736 const GValue *param_values;
1739 #ifdef G_ENABLE_DEBUG
1743 g_return_if_fail (instance_and_params != NULL);
1744 instance = g_value_peek_pointer (instance_and_params);
1745 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1746 g_return_if_fail (signal_id > 0);
1748 param_values = instance_and_params + 1;
1751 node = LOOKUP_SIGNAL_NODE (signal_id);
1752 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1754 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1758 #ifdef G_ENABLE_DEBUG
1759 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1761 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1765 for (i = 0; i < node->n_params; i++)
1766 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1768 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1770 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1773 G_VALUE_TYPE_NAME (param_values + i));
1777 if (node->return_type != G_TYPE_NONE)
1781 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1783 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1788 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1790 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
1792 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1794 G_VALUE_TYPE_NAME (return_value));
1800 return_value = NULL;
1801 #endif /* G_ENABLE_DEBUG */
1804 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
1808 g_signal_emit_valist (gpointer instance,
1813 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
1814 GType signal_return_type;
1815 GValue *param_values;
1819 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1820 g_return_if_fail (signal_id > 0);
1823 node = LOOKUP_SIGNAL_NODE (signal_id);
1824 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1826 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1830 #ifndef G_DISABLE_CHECKS
1831 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1833 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1837 #endif /* !G_DISABLE_CHECKS */
1839 n_params = node->n_params;
1840 signal_return_type = node->return_type;
1841 if (node->n_params < MAX_STACK_VALUES)
1842 instance_and_params = stack_values;
1845 free_me = g_new (GValue, node->n_params + 1);
1846 instance_and_params = free_me;
1848 param_values = instance_and_params + 1;
1849 for (i = 0; i < node->n_params; i++)
1852 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1853 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
1855 param_values[i].g_type = 0;
1857 g_value_init (param_values + i, ptype);
1858 G_VALUE_COLLECT (param_values + i,
1860 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1864 g_warning ("%s: %s", G_STRLOC, error);
1867 /* we purposely leak the value here, it might not be
1868 * in a sane state if an error condition occoured
1871 g_value_unset (param_values + i);
1879 instance_and_params->g_type = 0;
1880 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
1881 g_value_set_instance (instance_and_params, instance);
1882 if (signal_return_type == G_TYPE_NONE)
1883 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
1886 GValue return_value = { 0, };
1887 gchar *error = NULL;
1888 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
1889 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
1891 g_value_init (&return_value, rtype);
1893 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
1895 G_VALUE_LCOPY (&return_value,
1897 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
1900 g_value_unset (&return_value);
1903 g_warning ("%s: %s", G_STRLOC, error);
1906 /* we purposely leak the value here, it might not be
1907 * in a sane state if an error condition occoured
1911 for (i = 0; i < n_params; i++)
1912 g_value_unset (param_values + i);
1913 g_value_unset (instance_and_params);
1919 g_signal_emit (gpointer instance,
1926 va_start (var_args, detail);
1927 g_signal_emit_valist (instance, signal_id, detail, var_args);
1932 g_signal_emit_by_name (gpointer instance,
1933 const gchar *detailed_signal,
1939 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1940 g_return_if_fail (detailed_signal != NULL);
1943 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
1950 va_start (var_args, detailed_signal);
1951 g_signal_emit_valist (instance, signal_id, detail, var_args);
1955 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1958 static inline gboolean
1959 accumulate (GSignalInvocationHint *ihint,
1960 GValue *return_accu,
1961 GValue *handler_return,
1962 SignalAccumulator *accumulator)
1964 gboolean continue_emission;
1969 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
1970 g_value_reset (handler_return);
1972 return continue_emission;
1976 signal_emit_unlocked_R (SignalNode *node,
1979 GValue *emission_return,
1980 const GValue *instance_and_params)
1982 EmissionState emission_state = 0;
1983 SignalAccumulator *accumulator;
1984 GSignalInvocationHint ihint;
1985 GClosure *class_closure;
1987 Handler *handler_list = NULL;
1988 GValue *return_accu, accu = { 0, };
1990 gulong max_sequential_handler_number;
1991 gboolean return_value_altered = FALSE;
1993 #ifdef G_ENABLE_DEBUG
1994 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
1996 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
1997 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2000 if (g_trap_instance_signals == instance)
2003 #endif /* G_ENABLE_DEBUG */
2006 signal_id = node->signal_id;
2007 if (node->flags & G_SIGNAL_NO_RECURSE)
2009 Emission *emission = emission_find (g_restart_emissions, signal_id, detail, instance);
2013 *emission->state_p = EMISSION_RESTART;
2015 return return_value_altered;
2018 ihint.signal_id = node->signal_id;
2019 ihint.detail = detail;
2020 accumulator = node->accumulator;
2024 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2025 return_accu = &accu;
2029 return_accu = emission_return;
2030 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions,
2031 signal_id, detail, instance, &emission_state);
2032 class_closure = node->class_closure;
2037 handler_unref_R (signal_id, instance, handler_list);
2038 max_sequential_handler_number = g_handler_sequential_number;
2039 hlist = handler_list_lookup (signal_id, instance);
2040 handler_list = hlist ? hlist->handlers : NULL;
2042 handler_ref (handler_list);
2044 ihint.run_type = G_SIGNAL_RUN_FIRST;
2046 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2048 emission_state = EMISSION_RUN;
2051 g_closure_invoke (class_closure,
2054 instance_and_params,
2056 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2057 emission_state == EMISSION_RUN)
2058 emission_state = EMISSION_STOP;
2060 return_value_altered = TRUE;
2062 if (emission_state == EMISSION_STOP)
2064 else if (emission_state == EMISSION_RESTART)
2068 if (node->emission_hooks)
2070 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2073 emission_state = EMISSION_HOOK;
2074 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2077 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2079 if (!signal_hook->detail || signal_hook->detail == detail)
2081 GSignalEmissionHook hook_func = hook->func;
2083 was_in_call = G_HOOK_IN_CALL (hook);
2084 hook->flags |= G_HOOK_FLAG_IN_CALL;
2086 need_destroy = !hook_func (&ihint, node->n_params + 1, instance_and_params, hook->data);
2089 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2091 g_hook_destroy_link (node->emission_hooks, hook);
2093 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2096 if (emission_state == EMISSION_RESTART)
2102 Handler *handler = handler_list;
2104 emission_state = EMISSION_RUN;
2105 handler_ref (handler);
2112 handler_unref_R (signal_id, instance, handler_list);
2113 handler_list = handler;
2116 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2117 handler->sequential_number < max_sequential_handler_number)
2120 g_closure_invoke (handler->closure,
2123 instance_and_params,
2125 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2126 emission_state == EMISSION_RUN)
2127 emission_state = EMISSION_STOP;
2129 return_value_altered = TRUE;
2131 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2134 tmp = handler->next;
2138 handler_unref_R (signal_id, instance, handler_list);
2139 handler_list = handler;
2144 if (emission_state == EMISSION_STOP)
2146 else if (emission_state == EMISSION_RESTART)
2150 ihint.run_type = G_SIGNAL_RUN_LAST;
2152 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2154 emission_state = EMISSION_RUN;
2157 g_closure_invoke (class_closure,
2160 instance_and_params,
2162 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2163 emission_state == EMISSION_RUN)
2164 emission_state = EMISSION_STOP;
2166 return_value_altered = TRUE;
2168 if (emission_state == EMISSION_STOP)
2170 else if (emission_state == EMISSION_RESTART)
2176 Handler *handler = handler_list;
2178 emission_state = EMISSION_RUN;
2179 handler_ref (handler);
2184 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2185 handler->sequential_number < max_sequential_handler_number)
2188 g_closure_invoke (handler->closure,
2191 instance_and_params,
2193 if (!accumulate (&ihint, emission_return, &accu, accumulator) &&
2194 emission_state == EMISSION_RUN)
2195 emission_state = EMISSION_STOP;
2197 return_value_altered = TRUE;
2199 tmp = emission_state == EMISSION_RUN ? handler->next : NULL;
2202 tmp = handler->next;
2206 handler_unref_R (signal_id, instance, handler);
2211 if (emission_state == EMISSION_STOP)
2213 else if (emission_state == EMISSION_RESTART)
2219 ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2221 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2223 gboolean need_unset = FALSE;
2225 emission_state = EMISSION_STOP;
2228 if (node->return_type != G_TYPE_NONE && !accumulator)
2230 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2233 g_closure_invoke (class_closure,
2234 node->return_type != G_TYPE_NONE ? &accu : NULL,
2236 instance_and_params,
2239 g_value_unset (&accu);
2242 if (emission_state == EMISSION_RESTART)
2247 handler_unref_R (signal_id, instance, handler_list);
2249 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission_state);
2252 g_value_unset (&accu);
2254 return return_value_altered;
2258 /* --- compile standard marshallers --- */
2259 #include "gobject.h"
2261 #include "gmarshal.c"