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)
40 #define REPORT_BUG "please report occourance circumstances to gtk-devel-list@gnome.org"
42 #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond)
43 static volatile gpointer g_trace_instance_signals = NULL;
44 static volatile gpointer g_trap_instance_signals = NULL;
45 #endif /* G_ENABLE_DEBUG */
48 /* --- generic allocation --- */
49 /* we special case allocations generically by replacing
50 * these functions with more speed/memory aware variants
52 #ifndef DISABLE_MEM_POOLS
53 static inline gpointer
54 g_generic_node_alloc (GTrashStack **trash_stack_p,
56 guint nodes_pre_alloc)
58 gpointer node = g_trash_stack_pop (trash_stack_p);
64 nodes_pre_alloc = MAX (nodes_pre_alloc, 1);
65 block = g_malloc (sizeof_node * nodes_pre_alloc);
66 while (--nodes_pre_alloc)
68 g_trash_stack_push (trash_stack_p, block);
76 #define g_generic_node_free(trash_stack_p, node) g_trash_stack_push (trash_stack_p, node)
77 #else /* !DISABLE_MEM_POOLS */
78 #define g_generic_node_alloc(t,sizeof_node,p) g_malloc (sizeof_node)
79 #define g_generic_node_free(t,node) g_free (node)
80 #endif /* !DISABLE_MEM_POOLS */
83 /* --- typedefs --- */
84 typedef struct _SignalNode SignalNode;
85 typedef struct _SignalKey SignalKey;
86 typedef struct _Emission Emission;
87 typedef struct _Handler Handler;
88 typedef struct _HandlerList HandlerList;
89 typedef struct _HandlerMatch HandlerMatch;
99 /* --- prototypes --- */
100 static inline guint signal_id_lookup (GQuark quark,
102 static void signal_destroy_R (SignalNode *signal_node);
103 static inline HandlerList* handler_list_ensure (guint signal_id,
105 static inline HandlerList* handler_list_lookup (guint signal_id,
107 static inline Handler* handler_new (gboolean after);
108 static void handler_insert (guint signal_id,
111 static Handler* handler_lookup (gpointer instance,
114 static inline HandlerMatch* handler_match_prepend (HandlerMatch *list,
117 static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node,
119 static HandlerMatch* handlers_find (gpointer instance,
120 GSignalMatchType mask,
126 gboolean one_and_only);
127 static inline void handler_ref (Handler *handler);
128 static inline void handler_unref_R (guint signal_id,
131 static gint handler_lists_cmp (gconstpointer node1,
132 gconstpointer node2);
133 static inline void emission_push (Emission **emission_list_p,
135 static inline void emission_pop (Emission **emission_list_p,
137 static inline Emission* emission_find (Emission *emission_list,
141 static gint class_closures_cmp (gconstpointer node1,
142 gconstpointer node2);
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 GBSearchArray *class_closure_bsa;
179 SignalAccumulator *accumulator;
180 GSignalCMarshaller c_marshaller;
181 GHookList *emission_hooks;
195 GSignalInvocationHint ihint;
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)
230 GType instance_type; /* 0 for default closure */
235 /* --- variables --- */
236 static GBSearchArray *g_signal_key_bsa = NULL;
237 static GBSearchConfig g_signal_key_bconfig = G_STATIC_BCONFIG (sizeof (SignalKey),
239 G_BSEARCH_ARRAY_ALIGN_POWER2);
240 static GBSearchConfig g_signal_hlbsa_bconfig = G_STATIC_BCONFIG (sizeof (HandlerList),
242 G_BSEARCH_ARRAY_DEFER_SHRINK);
243 static GBSearchConfig g_class_closure_bconfig = G_STATIC_BCONFIG (sizeof (ClassClosure),
246 static GHashTable *g_handler_list_bsa_ht = NULL;
247 static Emission *g_recursive_emissions = NULL;
248 static Emission *g_restart_emissions = NULL;
249 static GTrashStack *g_handler_ts = NULL;
250 static gulong g_handler_sequential_number = 1;
251 G_LOCK_DEFINE_STATIC (g_signal_mutex);
252 #define SIGNAL_LOCK() G_LOCK (g_signal_mutex)
253 #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex)
256 /* --- signal nodes --- */
257 static guint g_n_signal_nodes = 0;
258 static SignalNode **g_signal_nodes = NULL;
260 static inline SignalNode*
261 LOOKUP_SIGNAL_NODE (register guint signal_id)
263 if (signal_id < g_n_signal_nodes)
264 return g_signal_nodes[signal_id];
270 /* --- functions --- */
272 signal_id_lookup (GQuark quark,
275 GType *ifaces, type = itype;
281 /* try looking up signals for this type and its anchestors */
284 SignalKey *signal_key;
287 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
290 return signal_key->signal_id;
292 type = g_type_parent (type);
296 /* no luck, try interfaces it exports */
297 ifaces = g_type_interfaces (itype, &n_ifaces);
300 SignalKey *signal_key;
302 key.itype = ifaces[n_ifaces];
303 signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key);
308 return signal_key->signal_id;
317 class_closures_cmp (gconstpointer node1,
320 const ClassClosure *c1 = node1, *c2 = node2;
322 return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type);
326 handler_lists_cmp (gconstpointer node1,
329 const HandlerList *hlist1 = node1, *hlist2 = node2;
331 return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id);
334 static inline HandlerList*
335 handler_list_ensure (guint signal_id,
338 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
341 key.signal_id = signal_id;
345 hlbsa = g_bsearch_array_new (&g_signal_hlbsa_bconfig);
346 hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key, FALSE);
347 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
351 GBSearchArray *o = hlbsa;
353 hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key, FALSE);
355 g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa);
357 return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key);
360 static inline HandlerList*
361 handler_list_lookup (guint signal_id,
364 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
367 key.signal_id = signal_id;
369 return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL;
373 handler_lookup (gpointer instance,
377 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
383 for (i = 0; i < hlbsa->n_nodes; i++)
385 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
388 for (handler = hlist->handlers; handler; handler = handler->next)
389 if (handler->sequential_number == handler_id)
392 *signal_id_p = hlist->signal_id;
402 static inline HandlerMatch*
403 handler_match_prepend (HandlerMatch *list,
409 /* yeah, we could use our own memchunk here, introducing yet more
410 * rarely used cached nodes and extra allocation overhead.
411 * instead, we use GList* nodes, since they are exactly the size
412 * we need and are already cached. g_signal_init() asserts this.
414 node = (HandlerMatch*) g_list_alloc ();
415 node->handler = handler;
417 node->d.signal_id = signal_id;
418 handler_ref (handler);
422 static inline HandlerMatch*
423 handler_match_free1_R (HandlerMatch *node,
426 HandlerMatch *next = node->next;
428 handler_unref_R (node->d.signal_id, instance, node->handler);
429 g_list_free_1 ((GList*) node);
435 handlers_find (gpointer instance,
436 GSignalMatchType mask,
442 gboolean one_and_only)
444 HandlerMatch *mlist = NULL;
446 if (mask & G_SIGNAL_MATCH_ID)
448 HandlerList *hlist = handler_list_lookup (signal_id, instance);
450 SignalNode *node = NULL;
452 if (mask & G_SIGNAL_MATCH_FUNC)
454 node = LOOKUP_SIGNAL_NODE (signal_id);
455 if (!node || !node->c_marshaller)
460 for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next)
461 if (handler->sequential_number &&
462 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
463 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
464 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
465 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
466 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
467 handler->closure->meta_marshal == 0 &&
468 ((GCClosure*) handler->closure)->callback == func)))
470 mlist = handler_match_prepend (mlist, handler, signal_id);
477 GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
484 for (i = 0; i < hlbsa->n_nodes; i++)
486 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
487 SignalNode *node = NULL;
490 if (!(mask & G_SIGNAL_MATCH_FUNC))
492 node = LOOKUP_SIGNAL_NODE (hlist->signal_id);
493 if (!node->c_marshaller)
497 for (handler = hlist->handlers; handler; handler = handler->next)
498 if (handler->sequential_number &&
499 ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) &&
500 ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) &&
501 ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) &&
502 ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) &&
503 ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller &&
504 handler->closure->meta_marshal == 0 &&
505 ((GCClosure*) handler->closure)->callback == func)))
507 mlist = handler_match_prepend (mlist, handler, hlist->signal_id);
518 static inline Handler*
519 handler_new (gboolean after)
521 Handler *handler = g_generic_node_alloc (&g_handler_ts,
524 #ifndef G_DISABLE_CHECKS
525 if (g_handler_sequential_number < 1)
526 g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG);
529 handler->sequential_number = g_handler_sequential_number++;
530 handler->prev = NULL;
531 handler->next = NULL;
533 handler->ref_count = 1;
534 handler->block_count = 0;
535 handler->after = after != FALSE;
536 handler->closure = NULL;
542 handler_ref (Handler *handler)
544 g_return_if_fail (handler->ref_count > 0);
546 #ifndef G_DISABLE_CHECKS
547 if (handler->ref_count >= HANDLER_MAX_REF_COUNT - 1)
548 g_error (G_STRLOC ": handler ref_count overflow, %s", REPORT_BUG);
551 handler->ref_count += 1;
555 handler_unref_R (guint signal_id,
559 g_return_if_fail (handler->ref_count > 0);
561 handler->ref_count -= 1;
562 if (!handler->ref_count)
565 handler->next->prev = handler->prev;
566 if (handler->prev) /* watch out for g_signal_handlers_destroy()! */
567 handler->prev->next = handler->next;
570 HandlerList *hlist = handler_list_lookup (signal_id, instance);
572 hlist->handlers = handler->next;
575 g_closure_unref (handler->closure);
577 g_generic_node_free (&g_handler_ts, handler);
582 handler_insert (guint signal_id,
588 g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */
590 hlist = handler_list_ensure (signal_id, instance);
591 if (!hlist->handlers)
592 hlist->handlers = handler;
593 else if (hlist->handlers->after && !handler->after)
595 handler->next = hlist->handlers;
596 hlist->handlers->prev = handler;
597 hlist->handlers = handler;
601 Handler *tmp = hlist->handlers;
607 while (tmp->next && !tmp->next->after)
610 tmp->next->prev = handler;
611 handler->next = tmp->next;
618 emission_push (Emission **emission_list_p,
621 emission->next = *emission_list_p;
622 *emission_list_p = emission;
626 emission_pop (Emission **emission_list_p,
629 Emission *node, *last = NULL;
631 for (node = *emission_list_p; node; last = node, node = last->next)
632 if (node == emission)
635 last->next = node->next;
637 *emission_list_p = node->next;
640 g_assert_not_reached ();
643 static inline Emission*
644 emission_find (Emission *emission_list,
651 for (emission = emission_list; emission; emission = emission->next)
652 if (emission->instance == instance &&
653 emission->ihint.signal_id == signal_id &&
654 emission->ihint.detail == detail)
659 static inline Emission*
660 emission_find_innermost (gpointer instance)
662 Emission *emission, *s = NULL, *c = NULL;
664 for (emission = g_restart_emissions; emission; emission = emission->next)
665 if (emission->instance == instance)
670 for (emission = g_recursive_emissions; emission; emission = emission->next)
671 if (emission->instance == instance)
681 return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s);
685 signal_key_cmp (gconstpointer node1,
688 const SignalKey *key1 = node1, *key2 = node2;
690 if (key1->itype == key2->itype)
691 return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark);
693 return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype);
697 g_signal_init (void) /* sync with gtype.c */
700 if (!g_n_signal_nodes)
702 /* handler_id_node_prepend() requires this */
703 g_assert (sizeof (GList) == sizeof (HandlerMatch));
705 /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */
706 g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL);
707 g_signal_key_bsa = g_bsearch_array_new (&g_signal_key_bconfig);
709 /* invalid (0) signal_id */
710 g_n_signal_nodes = 1;
711 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
712 g_signal_nodes[0] = NULL;
718 _g_signals_destroy (GType itype)
723 for (i = 1; i < g_n_signal_nodes; i++)
725 SignalNode *node = g_signal_nodes[i];
727 if (node->itype == itype)
730 g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed",
732 g_type_name (node->itype));
734 signal_destroy_R (node);
741 g_signal_stop_emission (gpointer instance,
747 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
748 g_return_if_fail (signal_id > 0);
751 node = LOOKUP_SIGNAL_NODE (signal_id);
752 if (node && detail && !(node->flags & G_SIGNAL_DETAILED))
754 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
758 if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
760 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
761 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
765 if (emission->state == EMISSION_HOOK)
766 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
767 node->name, instance);
768 else if (emission->state == EMISSION_RUN)
769 emission->state = EMISSION_STOP;
772 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
773 node->name, instance);
776 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
781 signal_finalize_hook (GHookList *hook_list,
784 GDestroyNotify destroy = hook->destroy;
788 hook->destroy = NULL;
790 destroy (hook->data);
796 g_signal_add_emission_hook (guint signal_id,
798 GSignalEmissionHook hook_func,
800 GDestroyNotify data_destroy)
802 static gulong seq_hook_id = 1;
805 SignalHook *signal_hook;
807 g_return_val_if_fail (signal_id > 0, 0);
808 g_return_val_if_fail (hook_func != NULL, 0);
811 node = LOOKUP_SIGNAL_NODE (signal_id);
812 if (!node || node->destroyed || (node->flags & G_SIGNAL_NO_HOOKS))
814 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
818 if (detail && !(node->flags & G_SIGNAL_DETAILED))
820 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
824 if (!node->emission_hooks)
826 node->emission_hooks = g_new (GHookList, 1);
827 g_hook_list_init (node->emission_hooks, sizeof (SignalHook));
828 node->emission_hooks->finalize_hook = signal_finalize_hook;
830 hook = g_hook_alloc (node->emission_hooks);
831 hook->data = hook_data;
832 hook->func = (gpointer) hook_func;
833 hook->destroy = data_destroy;
834 signal_hook = SIGNAL_HOOK (hook);
835 signal_hook->detail = detail;
836 node->emission_hooks->seq_id = seq_hook_id;
837 g_hook_append (node->emission_hooks, hook);
838 seq_hook_id = node->emission_hooks->seq_id;
841 return hook->hook_id;
845 g_signal_remove_emission_hook (guint signal_id,
850 g_return_if_fail (signal_id > 0);
851 g_return_if_fail (hook_id > 0);
854 node = LOOKUP_SIGNAL_NODE (signal_id);
855 if (!node || node->destroyed)
856 g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id);
857 else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id))
858 g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id);
863 signal_parse_name (const gchar *name,
866 gboolean force_quark)
868 const gchar *colon = strchr (name, ':');
873 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
874 if (signal_id && detail_p)
877 else if (colon[1] == ':')
880 guint l = colon - name;
884 memcpy (buffer, name, l);
886 signal_id = signal_id_lookup (g_quark_try_string (buffer), itype);
890 gchar *signal = g_new (gchar, l + 1);
892 memcpy (signal, name, l);
894 signal_id = signal_id_lookup (g_quark_try_string (signal), itype);
898 if (signal_id && detail_p)
899 *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0;
907 g_signal_parse_name (const gchar *detailed_signal,
911 gboolean force_detail_quark)
917 g_return_val_if_fail (detailed_signal != NULL, FALSE);
918 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE);
921 signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark);
924 node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL;
925 if (!node || node->destroyed ||
926 (detail && !(node->flags & G_SIGNAL_DETAILED)))
930 *signal_id_p = signal_id;
938 g_signal_stop_emission_by_name (gpointer instance,
939 const gchar *detailed_signal)
945 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
946 g_return_if_fail (detailed_signal != NULL);
949 itype = G_TYPE_FROM_INSTANCE (instance);
950 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
953 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
955 if (detail && !(node->flags & G_SIGNAL_DETAILED))
956 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
957 else if (!g_type_is_a (itype, node->itype))
958 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
961 Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions;
962 Emission *emission = emission_find (emission_list, signal_id, detail, instance);
966 if (emission->state == EMISSION_HOOK)
967 g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook",
968 node->name, instance);
969 else if (emission->state == EMISSION_RUN)
970 emission->state = EMISSION_STOP;
973 g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'",
974 node->name, instance);
978 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
983 g_signal_lookup (const gchar *name,
988 g_return_val_if_fail (name != NULL, 0);
989 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
992 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
996 /* give elaborate warnings */
997 if (!g_type_name (itype))
998 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%lu'",
1000 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1001 g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'",
1002 name, g_type_name (itype));
1003 else if (!g_type_class_peek (itype))
1004 g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'",
1005 name, g_type_name (itype));
1012 g_signal_list_ids (GType itype,
1020 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL);
1021 g_return_val_if_fail (n_ids != NULL, NULL);
1024 keys = G_BSEARCH_ARRAY_NODES (g_signal_key_bsa);
1025 n_nodes = g_signal_key_bsa->n_nodes;
1026 result = g_array_new (FALSE, FALSE, sizeof (guint));
1028 for (i = 0; i < n_nodes; i++)
1029 if (keys[i].itype == itype)
1031 const gchar *name = g_quark_to_string (keys[i].quark);
1033 /* Signal names with "_" in them are aliases to the same
1034 * name with "-" instead of "_".
1036 if (!strchr (name, '_'))
1037 g_array_append_val (result, keys[i].signal_id);
1039 *n_ids = result->len;
1043 /* give elaborate warnings */
1044 if (!g_type_name (itype))
1045 g_warning (G_STRLOC ": unable to list signals for invalid type id `%lu'",
1047 else if (!G_TYPE_IS_INSTANTIATABLE (itype))
1048 g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'",
1049 g_type_name (itype));
1050 else if (!g_type_class_peek (itype))
1051 g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'",
1052 g_type_name (itype));
1055 return (guint*) g_array_free (result, FALSE);
1058 G_CONST_RETURN gchar*
1059 g_signal_name (guint signal_id)
1065 node = LOOKUP_SIGNAL_NODE (signal_id);
1066 name = node ? node->name : NULL;
1073 g_signal_query (guint signal_id,
1074 GSignalQuery *query)
1078 g_return_if_fail (query != NULL);
1081 node = LOOKUP_SIGNAL_NODE (signal_id);
1082 if (!node || node->destroyed)
1083 query->signal_id = 0;
1086 query->signal_id = node->signal_id;
1087 query->signal_name = node->name;
1088 query->itype = node->itype;
1089 query->signal_flags = node->flags;
1090 query->return_type = node->return_type;
1091 query->n_params = node->n_params;
1092 query->param_types = node->param_types;
1098 g_signal_new (const gchar *signal_name,
1100 GSignalFlags signal_flags,
1102 GSignalAccumulator accumulator,
1104 GSignalCMarshaller c_marshaller,
1112 g_return_val_if_fail (signal_name != NULL, 0);
1114 va_start (args, n_params);
1116 signal_id = g_signal_new_valist (signal_name, itype, signal_flags,
1117 class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL,
1118 accumulator, accu_data, c_marshaller,
1119 return_type, n_params, args);
1126 static inline ClassClosure*
1127 signal_find_class_closure (SignalNode *node,
1130 GBSearchArray *bsa = node->class_closure_bsa;
1137 /* cc->instance_type is 0 for default closure */
1139 key.instance_type = itype;
1140 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1141 while (!cc && key.instance_type)
1143 key.instance_type = g_type_parent (key.instance_type);
1144 cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key);
1152 static inline GClosure*
1153 signal_lookup_closure (SignalNode *node,
1154 GTypeInstance *instance)
1158 if (node->class_closure_bsa && node->class_closure_bsa->n_nodes == 1)
1159 cc = G_BSEARCH_ARRAY_NODES (node->class_closure_bsa);
1161 cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance));
1162 return cc ? cc->closure : NULL;
1166 signal_add_class_closure (SignalNode *node,
1172 if (!node->class_closure_bsa)
1173 node->class_closure_bsa = g_bsearch_array_new (&g_class_closure_bconfig);
1174 key.instance_type = itype;
1175 key.closure = g_closure_ref (closure);
1176 node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa,
1177 &g_class_closure_bconfig,
1180 g_closure_sink (closure);
1181 if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure))
1182 g_closure_set_marshal (closure, node->c_marshaller);
1186 g_signal_newv (const gchar *signal_name,
1188 GSignalFlags signal_flags,
1189 GClosure *class_closure,
1190 GSignalAccumulator accumulator,
1192 GSignalCMarshaller c_marshaller,
1201 g_return_val_if_fail (signal_name != NULL, 0);
1202 g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0);
1204 g_return_val_if_fail (param_types != NULL, 0);
1205 g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0);
1206 if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1207 g_return_val_if_fail (accumulator == NULL, 0);
1209 g_return_val_if_fail (accu_data == NULL, 0);
1211 name = g_strdup (signal_name);
1212 g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */
1216 signal_id = signal_id_lookup (g_quark_try_string (name), itype);
1217 node = LOOKUP_SIGNAL_NODE (signal_id);
1218 if (node && !node->destroyed)
1220 g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s",
1222 g_type_name (node->itype),
1223 G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry");
1228 if (node && node->itype != itype)
1230 g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'",
1232 g_type_name (itype),
1233 g_type_name (node->itype));
1238 for (i = 0; i < n_params; i++)
1239 if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1241 g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type",
1242 i + 1, g_type_name (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1247 if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1249 g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type",
1250 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE), g_type_name (itype), name);
1255 if (return_type != G_TYPE_NONE &&
1256 (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST)
1258 g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST",
1259 g_type_name (itype), name,
1260 g_type_name (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE));
1266 /* setup permanent portion of signal node */
1271 signal_id = g_n_signal_nodes++;
1272 node = g_new (SignalNode, 1);
1273 node->signal_id = signal_id;
1274 g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes);
1275 g_signal_nodes[signal_id] = node;
1276 node->itype = itype;
1279 key.quark = g_quark_from_string (node->name);
1280 key.signal_id = signal_id;
1281 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1282 g_strdelimit (node->name, "_", '-');
1283 key.quark = g_quark_from_static_string (node->name);
1284 g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key, FALSE);
1286 node->destroyed = FALSE;
1288 /* setup reinitializable portion */
1289 node->flags = signal_flags & G_SIGNAL_FLAGS_MASK;
1290 node->n_params = n_params;
1291 node->param_types = g_memdup (param_types, sizeof (GType) * n_params);
1292 node->return_type = return_type;
1293 node->class_closure_bsa = NULL;
1296 node->accumulator = g_new (SignalAccumulator, 1);
1297 node->accumulator->func = accumulator;
1298 node->accumulator->data = accu_data;
1301 node->accumulator = NULL;
1302 node->c_marshaller = c_marshaller;
1303 node->emission_hooks = NULL;
1305 signal_add_class_closure (node, 0, class_closure);
1312 g_signal_new_valist (const gchar *signal_name,
1314 GSignalFlags signal_flags,
1315 GClosure *class_closure,
1316 GSignalAccumulator accumulator,
1318 GSignalCMarshaller c_marshaller,
1329 param_types = g_new (GType, n_params);
1331 for (i = 0; i < n_params; i++)
1332 param_types[i] = va_arg (args, GType);
1337 signal_id = g_signal_newv (signal_name, itype, signal_flags,
1338 class_closure, accumulator, accu_data, c_marshaller,
1339 return_type, n_params, param_types);
1340 g_free (param_types);
1346 signal_destroy_R (SignalNode *signal_node)
1348 SignalNode node = *signal_node;
1350 signal_node->destroyed = TRUE;
1352 /* reentrancy caution, zero out real contents first */
1353 signal_node->n_params = 0;
1354 signal_node->param_types = NULL;
1355 signal_node->return_type = 0;
1356 signal_node->class_closure_bsa = NULL;
1357 signal_node->accumulator = NULL;
1358 signal_node->c_marshaller = NULL;
1359 signal_node->emission_hooks = NULL;
1361 #ifdef G_ENABLE_DEBUG
1362 /* check current emissions */
1366 for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions;
1367 emission; emission = emission->next)
1368 if (emission->ihint.signal_id == node.signal_id)
1369 g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')",
1370 node.name, emission->instance);
1374 /* free contents that need to
1377 g_free (node.param_types);
1378 if (node.class_closure_bsa)
1382 for (i = 0; i < node.class_closure_bsa->n_nodes; i++)
1384 ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i);
1386 g_closure_unref (cc->closure);
1388 g_bsearch_array_destroy (node.class_closure_bsa, &g_class_closure_bconfig);
1390 g_free (node.accumulator);
1391 if (node.emission_hooks)
1393 g_hook_list_clear (node.emission_hooks);
1394 g_free (node.emission_hooks);
1400 g_signal_override_class_closure (guint signal_id,
1401 GType instance_type,
1402 GClosure *class_closure)
1406 g_return_if_fail (signal_id > 0);
1407 g_return_if_fail (class_closure != NULL);
1410 node = LOOKUP_SIGNAL_NODE (signal_id);
1411 if (!g_type_is_a (instance_type, node->itype))
1412 g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, g_type_name (instance_type), signal_id);
1415 ClassClosure *cc = signal_find_class_closure (node, instance_type);
1417 if (cc && cc->instance_type == instance_type)
1418 g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, g_type_name (instance_type), signal_id);
1420 signal_add_class_closure (node, instance_type, class_closure);
1426 g_signal_chain_from_overridden (const GValue *instance_and_params,
1427 GValue *return_value)
1429 GType chain_type = 0, restore_type = 0;
1430 Emission *emission = NULL;
1431 GClosure *closure = NULL;
1435 g_return_if_fail (instance_and_params != NULL);
1436 instance = g_value_peek_pointer (instance_and_params);
1437 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1440 emission = emission_find_innermost (instance);
1443 SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id);
1445 g_assert (node != NULL); /* paranoid */
1447 /* we should probably do the same parameter checks as g_signal_emit() here.
1449 if (emission->chain_type != G_TYPE_NONE)
1451 ClassClosure *cc = signal_find_class_closure (node, emission->chain_type);
1453 g_assert (cc != NULL); /* closure currently in call stack */
1455 n_params = node->n_params;
1456 restore_type = cc->instance_type;
1457 cc = signal_find_class_closure (node, g_type_parent (cc->instance_type));
1458 if (cc && cc->instance_type != restore_type)
1460 closure = cc->closure;
1461 chain_type = cc->instance_type;
1465 g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance);
1468 g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance);
1471 emission->chain_type = chain_type;
1473 g_closure_invoke (closure,
1476 instance_and_params,
1479 emission->chain_type = restore_type;
1484 GSignalInvocationHint*
1485 g_signal_get_invocation_hint (gpointer instance)
1487 Emission *emission = NULL;
1489 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL);
1492 emission = emission_find_innermost (instance);
1495 return emission ? &emission->ihint : NULL;
1499 g_signal_connect_closure_by_id (gpointer instance,
1506 gulong handler_seq_no = 0;
1508 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1509 g_return_val_if_fail (signal_id > 0, 0);
1510 g_return_val_if_fail (closure != NULL, 0);
1513 node = LOOKUP_SIGNAL_NODE (signal_id);
1516 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1517 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1518 else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1519 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1522 Handler *handler = handler_new (after);
1524 handler_seq_no = handler->sequential_number;
1525 handler->detail = detail;
1526 handler->closure = g_closure_ref (closure);
1527 g_closure_sink (closure);
1528 handler_insert (signal_id, instance, handler);
1529 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure))
1530 g_closure_set_marshal (closure, node->c_marshaller);
1534 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1537 return handler_seq_no;
1541 g_signal_connect_closure (gpointer instance,
1542 const gchar *detailed_signal,
1547 gulong handler_seq_no = 0;
1551 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1552 g_return_val_if_fail (detailed_signal != NULL, 0);
1553 g_return_val_if_fail (closure != NULL, 0);
1556 itype = G_TYPE_FROM_INSTANCE (instance);
1557 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1560 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1562 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1563 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1564 else if (!g_type_is_a (itype, node->itype))
1565 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1568 Handler *handler = handler_new (after);
1570 handler_seq_no = handler->sequential_number;
1571 handler->detail = detail;
1572 handler->closure = g_closure_ref (closure);
1573 g_closure_sink (closure);
1574 handler_insert (signal_id, instance, handler);
1575 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1576 g_closure_set_marshal (handler->closure, node->c_marshaller);
1580 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1583 return handler_seq_no;
1587 g_signal_connect_data (gpointer instance,
1588 const gchar *detailed_signal,
1589 GCallback c_handler,
1591 GClosureNotify destroy_data,
1592 GConnectFlags connect_flags)
1595 gulong handler_seq_no = 0;
1598 gboolean swapped, after;
1600 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1601 g_return_val_if_fail (detailed_signal != NULL, 0);
1602 g_return_val_if_fail (c_handler != NULL, 0);
1604 swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE;
1605 after = (connect_flags & G_CONNECT_AFTER) != FALSE;
1608 itype = G_TYPE_FROM_INSTANCE (instance);
1609 signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE);
1612 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1614 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1615 g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal);
1616 else if (!g_type_is_a (itype, node->itype))
1617 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1620 Handler *handler = handler_new (after);
1622 handler_seq_no = handler->sequential_number;
1623 handler->detail = detail;
1624 handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data));
1625 g_closure_sink (handler->closure);
1626 handler_insert (signal_id, instance, handler);
1627 if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure))
1628 g_closure_set_marshal (handler->closure, node->c_marshaller);
1632 g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
1635 return handler_seq_no;
1639 g_signal_handler_block (gpointer instance,
1644 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1645 g_return_if_fail (handler_id > 0);
1648 handler = handler_lookup (instance, handler_id, NULL);
1651 #ifndef G_DISABLE_CHECKS
1652 if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1)
1653 g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG);
1655 handler->block_count += 1;
1658 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1663 g_signal_handler_unblock (gpointer instance,
1668 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1669 g_return_if_fail (handler_id > 0);
1672 handler = handler_lookup (instance, handler_id, NULL);
1675 if (handler->block_count)
1676 handler->block_count -= 1;
1678 g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance);
1681 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1686 g_signal_handler_disconnect (gpointer instance,
1692 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1693 g_return_if_fail (handler_id > 0);
1696 handler = handler_lookup (instance, handler_id, &signal_id);
1699 handler->sequential_number = 0;
1700 handler->block_count = 1;
1701 handler_unref_R (signal_id, instance, handler);
1704 g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id);
1709 g_signal_handler_is_connected (gpointer instance,
1715 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1716 g_return_val_if_fail (handler_id > 0, FALSE);
1719 handler = handler_lookup (instance, handler_id, NULL);
1720 connected = handler != NULL;
1727 g_signal_handlers_destroy (gpointer instance)
1729 GBSearchArray *hlbsa;
1731 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1734 hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance);
1739 /* reentrancy caution, delete instance trace first */
1740 g_hash_table_remove (g_handler_list_bsa_ht, instance);
1742 for (i = 0; i < hlbsa->n_nodes; i++)
1744 HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i);
1745 Handler *handler = hlist->handlers;
1749 Handler *tmp = handler;
1751 handler = tmp->next;
1752 tmp->block_count = 1;
1753 /* cruel unlink, this works because _all_ handlers vanish */
1756 if (tmp->sequential_number)
1758 tmp->sequential_number = 0;
1759 handler_unref_R (0, NULL, tmp);
1763 g_bsearch_array_destroy (hlbsa, &g_signal_hlbsa_bconfig);
1769 g_signal_handler_find (gpointer instance,
1770 GSignalMatchType mask,
1777 gulong handler_seq_no = 0;
1779 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0);
1780 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0);
1782 if (mask & G_SIGNAL_MATCH_MASK)
1784 HandlerMatch *mlist;
1787 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE);
1790 handler_seq_no = mlist->handler->sequential_number;
1791 handler_match_free1_R (mlist, instance);
1796 return handler_seq_no;
1800 signal_handlers_foreach_matched_R (gpointer instance,
1801 GSignalMatchType mask,
1807 void (*callback) (gpointer instance,
1808 gulong handler_seq_no))
1810 HandlerMatch *mlist;
1811 guint n_handlers = 0;
1813 mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE);
1817 if (mlist->handler->sequential_number)
1820 callback (instance, mlist->handler->sequential_number);
1823 mlist = handler_match_free1_R (mlist, instance);
1830 g_signal_handlers_block_matched (gpointer instance,
1831 GSignalMatchType mask,
1838 guint n_handlers = 0;
1840 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1841 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1843 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1846 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1847 closure, func, data,
1848 g_signal_handler_block);
1856 g_signal_handlers_unblock_matched (gpointer instance,
1857 GSignalMatchType mask,
1864 guint n_handlers = 0;
1866 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1867 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1869 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1872 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1873 closure, func, data,
1874 g_signal_handler_unblock);
1882 g_signal_handlers_disconnect_matched (gpointer instance,
1883 GSignalMatchType mask,
1890 guint n_handlers = 0;
1892 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1893 g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, FALSE);
1895 if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA))
1898 n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail,
1899 closure, func, data,
1900 g_signal_handler_disconnect);
1908 g_signal_has_handler_pending (gpointer instance,
1911 gboolean may_be_blocked)
1913 HandlerMatch *mlist;
1914 gboolean has_pending;
1916 g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE);
1917 g_return_val_if_fail (signal_id > 0, FALSE);
1922 SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id);
1924 if (!(node->flags & G_SIGNAL_DETAILED))
1926 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1931 mlist = handlers_find (instance,
1932 (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)),
1933 signal_id, detail, NULL, NULL, NULL, TRUE);
1937 handler_match_free1_R (mlist, instance);
1940 has_pending = FALSE;
1947 g_signal_emitv (const GValue *instance_and_params,
1950 GValue *return_value)
1952 const GValue *param_values;
1955 #ifdef G_ENABLE_DEBUG
1959 g_return_if_fail (instance_and_params != NULL);
1960 instance = g_value_peek_pointer (instance_and_params);
1961 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
1962 g_return_if_fail (signal_id > 0);
1964 param_values = instance_and_params + 1;
1967 node = LOOKUP_SIGNAL_NODE (signal_id);
1968 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
1970 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
1974 #ifdef G_ENABLE_DEBUG
1975 if (detail && !(node->flags & G_SIGNAL_DETAILED))
1977 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
1981 for (i = 0; i < node->n_params; i++)
1982 if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE))
1984 g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'",
1986 g_type_name (node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE),
1989 G_VALUE_TYPE_NAME (param_values + i));
1993 if (node->return_type != G_TYPE_NONE)
1997 g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)",
1999 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
2004 else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE))
2006 g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'",
2008 g_type_name (node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE),
2010 G_VALUE_TYPE_NAME (return_value));
2016 return_value = NULL;
2017 #endif /* G_ENABLE_DEBUG */
2020 signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params);
2024 g_signal_emit_valist (gpointer instance,
2029 GValue *instance_and_params, stack_values[MAX_STACK_VALUES], *free_me = NULL;
2030 GType signal_return_type;
2031 GValue *param_values;
2035 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2036 g_return_if_fail (signal_id > 0);
2039 node = LOOKUP_SIGNAL_NODE (signal_id);
2040 if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype))
2042 g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance);
2046 #ifndef G_DISABLE_CHECKS
2047 if (detail && !(node->flags & G_SIGNAL_DETAILED))
2049 g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail);
2053 #endif /* !G_DISABLE_CHECKS */
2055 n_params = node->n_params;
2056 signal_return_type = node->return_type;
2057 if (node->n_params < MAX_STACK_VALUES)
2058 instance_and_params = stack_values;
2061 free_me = g_new (GValue, node->n_params + 1);
2062 instance_and_params = free_me;
2064 param_values = instance_and_params + 1;
2065 for (i = 0; i < node->n_params; i++)
2068 GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2069 gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE;
2071 param_values[i].g_type = 0;
2073 g_value_init (param_values + i, ptype);
2074 G_VALUE_COLLECT (param_values + i,
2076 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2080 g_warning ("%s: %s", G_STRLOC, error);
2083 /* we purposely leak the value here, it might not be
2084 * in a sane state if an error condition occoured
2087 g_value_unset (param_values + i);
2095 instance_and_params->g_type = 0;
2096 g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance));
2097 g_value_set_instance (instance_and_params, instance);
2098 if (signal_return_type == G_TYPE_NONE)
2099 signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params);
2102 GValue return_value = { 0, };
2103 gchar *error = NULL;
2104 GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
2105 gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE;
2107 g_value_init (&return_value, rtype);
2109 signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params);
2111 G_VALUE_LCOPY (&return_value,
2113 static_scope ? G_VALUE_NOCOPY_CONTENTS : 0,
2116 g_value_unset (&return_value);
2119 g_warning ("%s: %s", G_STRLOC, error);
2122 /* we purposely leak the value here, it might not be
2123 * in a sane state if an error condition occoured
2127 for (i = 0; i < n_params; i++)
2128 g_value_unset (param_values + i);
2129 g_value_unset (instance_and_params);
2135 g_signal_emit (gpointer instance,
2142 va_start (var_args, detail);
2143 g_signal_emit_valist (instance, signal_id, detail, var_args);
2148 g_signal_emit_by_name (gpointer instance,
2149 const gchar *detailed_signal,
2155 g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance));
2156 g_return_if_fail (detailed_signal != NULL);
2159 signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE);
2166 va_start (var_args, detailed_signal);
2167 g_signal_emit_valist (instance, signal_id, detail, var_args);
2171 g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance);
2174 static inline gboolean
2175 accumulate (GSignalInvocationHint *ihint,
2176 GValue *return_accu,
2177 GValue *handler_return,
2178 SignalAccumulator *accumulator)
2180 gboolean continue_emission;
2185 continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data);
2186 g_value_reset (handler_return);
2188 return continue_emission;
2192 signal_emit_unlocked_R (SignalNode *node,
2195 GValue *emission_return,
2196 const GValue *instance_and_params)
2198 SignalAccumulator *accumulator;
2200 GClosure *class_closure;
2202 Handler *handler_list = NULL;
2203 GValue *return_accu, accu = { 0, };
2205 gulong max_sequential_handler_number;
2206 gboolean return_value_altered = FALSE;
2208 #ifdef G_ENABLE_DEBUG
2209 IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance)
2211 g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)",
2212 g_type_name (G_TYPE_FROM_INSTANCE (instance)),
2215 if (g_trap_instance_signals == instance)
2218 #endif /* G_ENABLE_DEBUG */
2221 signal_id = node->signal_id;
2222 if (node->flags & G_SIGNAL_NO_RECURSE)
2224 Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance);
2228 node->state = EMISSION_RESTART;
2230 return return_value_altered;
2233 accumulator = node->accumulator;
2237 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2238 return_accu = &accu;
2242 return_accu = emission_return;
2243 emission.instance = instance;
2244 emission.ihint.signal_id = node->signal_id;
2245 emission.ihint.detail = detail;
2246 emission.ihint.run_type = 0;
2248 emission.chain_type = G_TYPE_NONE;
2249 emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2250 class_closure = signal_lookup_closure (node, instance);
2255 handler_unref_R (signal_id, instance, handler_list);
2256 max_sequential_handler_number = g_handler_sequential_number;
2257 hlist = handler_list_lookup (signal_id, instance);
2258 handler_list = hlist ? hlist->handlers : NULL;
2260 handler_ref (handler_list);
2262 emission.ihint.run_type = G_SIGNAL_RUN_FIRST;
2264 if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure)
2266 emission.state = EMISSION_RUN;
2268 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2270 g_closure_invoke (class_closure,
2273 instance_and_params,
2275 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2276 emission.state == EMISSION_RUN)
2277 emission.state = EMISSION_STOP;
2279 emission.chain_type = G_TYPE_NONE;
2280 return_value_altered = TRUE;
2282 if (emission.state == EMISSION_STOP)
2284 else if (emission.state == EMISSION_RESTART)
2288 if (node->emission_hooks)
2290 gboolean need_destroy, was_in_call, may_recurse = TRUE;
2293 emission.state = EMISSION_HOOK;
2294 hook = g_hook_first_valid (node->emission_hooks, may_recurse);
2297 SignalHook *signal_hook = SIGNAL_HOOK (hook);
2299 if (!signal_hook->detail || signal_hook->detail == detail)
2301 GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func;
2303 was_in_call = G_HOOK_IN_CALL (hook);
2304 hook->flags |= G_HOOK_FLAG_IN_CALL;
2306 need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data);
2309 hook->flags &= ~G_HOOK_FLAG_IN_CALL;
2311 g_hook_destroy_link (node->emission_hooks, hook);
2313 hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse);
2316 if (emission.state == EMISSION_RESTART)
2322 Handler *handler = handler_list;
2324 emission.state = EMISSION_RUN;
2325 handler_ref (handler);
2332 handler_unref_R (signal_id, instance, handler_list);
2333 handler_list = handler;
2336 else if (!handler->block_count && (!handler->detail || handler->detail == detail) &&
2337 handler->sequential_number < max_sequential_handler_number)
2340 g_closure_invoke (handler->closure,
2343 instance_and_params,
2345 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2346 emission.state == EMISSION_RUN)
2347 emission.state = EMISSION_STOP;
2349 return_value_altered = TRUE;
2351 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2354 tmp = handler->next;
2358 handler_unref_R (signal_id, instance, handler_list);
2359 handler_list = handler;
2364 if (emission.state == EMISSION_STOP)
2366 else if (emission.state == EMISSION_RESTART)
2370 emission.ihint.run_type = G_SIGNAL_RUN_LAST;
2372 if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure)
2374 emission.state = EMISSION_RUN;
2376 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2378 g_closure_invoke (class_closure,
2381 instance_and_params,
2383 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2384 emission.state == EMISSION_RUN)
2385 emission.state = EMISSION_STOP;
2387 emission.chain_type = G_TYPE_NONE;
2388 return_value_altered = TRUE;
2390 if (emission.state == EMISSION_STOP)
2392 else if (emission.state == EMISSION_RESTART)
2398 Handler *handler = handler_list;
2400 emission.state = EMISSION_RUN;
2401 handler_ref (handler);
2406 if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) &&
2407 handler->sequential_number < max_sequential_handler_number)
2410 g_closure_invoke (handler->closure,
2413 instance_and_params,
2415 if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) &&
2416 emission.state == EMISSION_RUN)
2417 emission.state = EMISSION_STOP;
2419 return_value_altered = TRUE;
2421 tmp = emission.state == EMISSION_RUN ? handler->next : NULL;
2424 tmp = handler->next;
2428 handler_unref_R (signal_id, instance, handler);
2433 if (emission.state == EMISSION_STOP)
2435 else if (emission.state == EMISSION_RESTART)
2441 emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP;
2443 if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure)
2445 gboolean need_unset = FALSE;
2447 emission.state = EMISSION_STOP;
2449 emission.chain_type = G_TYPE_FROM_INSTANCE (instance);
2451 if (node->return_type != G_TYPE_NONE && !accumulator)
2453 g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE);
2456 g_closure_invoke (class_closure,
2457 node->return_type != G_TYPE_NONE ? &accu : NULL,
2459 instance_and_params,
2462 g_value_unset (&accu);
2464 emission.chain_type = G_TYPE_NONE;
2466 if (emission.state == EMISSION_RESTART)
2471 handler_unref_R (signal_id, instance, handler_list);
2473 emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission);
2476 g_value_unset (&accu);
2478 return return_value_altered;
2482 /* --- compile standard marshallers --- */
2483 #include "gobject.h"
2485 #include "gmarshal.c"