sl@0: /* GObject - GLib Type, Object, Parameter and Signal Library sl@0: * Copyright (C) 2000-2001 Red Hat, Inc. sl@0: * Portions copyright (c) 2006-2009 Nokia Corporation. All rights reserved. sl@0: * sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General sl@0: * Public License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place, Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: * sl@0: * this code is based on the original GtkSignal implementation sl@0: * for the Gtk+ library by Peter Mattis sl@0: */ sl@0: sl@0: /* sl@0: * MT safe sl@0: */ sl@0: sl@0: #include "config.h" sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include "gsignal.h" sl@0: #include "gbsearcharray.h" sl@0: #include "gvaluecollector.h" sl@0: #include "gvaluetypes.h" sl@0: #include "gboxed.h" sl@0: #include "gobject.h" sl@0: #include "genums.h" sl@0: #include "gobjectalias.h" sl@0: #ifdef __SYMBIAN32__ sl@0: #include sl@0: #include "gobject_wsd.h" sl@0: #endif /* __SYMBIAN32__ */ sl@0: sl@0: /** sl@0: * SECTION:signals sl@0: * @short_description: A means for customization of object behaviour sl@0: * and a general purpose notification mechanism sl@0: * @title: Signals sl@0: * sl@0: * The basic concept of the signal system is that of the sl@0: * emission of a signal. Signals are introduced sl@0: * per-type and are identified through strings. Signals introduced sl@0: * for a parent type are available in derived types as well, so sl@0: * basically they are a per-type facility that is inherited. A signal sl@0: * emission mainly involves invocation of a certain set of callbacks sl@0: * in precisely defined manner. There are two main categories of such sl@0: * callbacks, per-object sl@0: * Although signals can deal with any kind of instantiatable sl@0: * type, i'm referring to those types as "object types" in the following, sl@0: * simply because that is the context most users will encounter signals in. sl@0: * sl@0: * ones and user provided ones. sl@0: * The per-object callbacks are most often referred to as "object method sl@0: * handler" or "default (signal) handler", while user provided callbacks are sl@0: * usually just called "signal handler". sl@0: * The object method handler is provided at signal creation time (this most sl@0: * frequently happens at the end of an object class' creation), while user sl@0: * provided handlers are frequently connected and disconnected to/from a certain sl@0: * signal on certain object instances. sl@0: * sl@0: * A signal emission consists of five stages, unless prematurely stopped: sl@0: * sl@0: * sl@0: * 1 - Invocation of the object method handler for %G_SIGNAL_RUN_FIRST signals sl@0: * sl@0: * sl@0: * 2 - Invocation of normal user-provided signal handlers (after flag %FALSE) sl@0: * sl@0: * sl@0: * 3 - Invocation of the object method handler for %G_SIGNAL_RUN_LAST signals sl@0: * sl@0: * sl@0: * 4 - Invocation of user provided signal handlers, connected with an after flag of %TRUE sl@0: * sl@0: * sl@0: * 5 - Invocation of the object method handler for %G_SIGNAL_RUN_CLEANUP signals sl@0: * sl@0: * sl@0: * The user-provided signal handlers are called in the order they were sl@0: * connected in. sl@0: * All handlers may prematurely stop a signal emission, and any number of sl@0: * handlers may be connected, disconnected, blocked or unblocked during sl@0: * a signal emission. sl@0: * There are certain criteria for skipping user handlers in stages 2 and 4 sl@0: * of a signal emission. sl@0: * First, user handlers may be blocked, blocked handlers are omitted sl@0: * during callback invocation, to return from the "blocked" state, a sl@0: * handler has to get unblocked exactly the same amount of times sl@0: * it has been blocked before. sl@0: * Second, upon emission of a %G_SIGNAL_DETAILED signal, an additional sl@0: * "detail" argument passed in to g_signal_emit() has to match the detail sl@0: * argument of the signal handler currently subject to invocation. sl@0: * Specification of no detail argument for signal handlers (omission of the sl@0: * detail part of the signal specification upon connection) serves as a sl@0: * wildcard and matches any detail argument passed in to emission. sl@0: */ sl@0: sl@0: sl@0: #define REPORT_BUG "please report occurrence circumstances to gtk-devel-list@gnome.org" sl@0: #ifdef G_ENABLE_DEBUG sl@0: #define IF_DEBUG(debug_type, cond) if ((_g_type_debug_flags & G_TYPE_DEBUG_ ## debug_type) || cond) sl@0: sl@0: #if EMULATOR sl@0: sl@0: PLS(g_trace_instance_signals,gsignal,volatile gpointer ) sl@0: PLS(g_trap_instance_signals,gsignal,volatile gpointer ) sl@0: sl@0: #define g_trace_instance_signals (*FUNCTION_NAME(g_trace_instance_signals,gsignal)()) sl@0: #define g_trap_instance_signals (*FUNCTION_NAME(g_trap_instance_signals,gsignal)()) sl@0: sl@0: #else sl@0: sl@0: static volatile gpointer g_trace_instance_signals = NULL; sl@0: static volatile gpointer g_trap_instance_signals = NULL; sl@0: sl@0: #endif /* EMULATOR */ sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: sl@0: sl@0: /* --- typedefs --- */ sl@0: typedef struct _SignalNode SignalNode; sl@0: typedef struct _SignalKey SignalKey; sl@0: typedef struct _Emission Emission; sl@0: typedef struct _Handler Handler; sl@0: typedef struct _HandlerList HandlerList; sl@0: typedef struct _HandlerMatch HandlerMatch; sl@0: typedef enum sl@0: { sl@0: EMISSION_STOP, sl@0: EMISSION_RUN, sl@0: EMISSION_HOOK, sl@0: EMISSION_RESTART sl@0: } EmissionState; sl@0: sl@0: sl@0: /* --- prototypes --- */ sl@0: static inline guint signal_id_lookup (GQuark quark, sl@0: GType itype); sl@0: static void signal_destroy_R (SignalNode *signal_node); sl@0: static inline HandlerList* handler_list_ensure (guint signal_id, sl@0: gpointer instance); sl@0: static inline HandlerList* handler_list_lookup (guint signal_id, sl@0: gpointer instance); sl@0: static inline Handler* handler_new (gboolean after); sl@0: static void handler_insert (guint signal_id, sl@0: gpointer instance, sl@0: Handler *handler); sl@0: static Handler* handler_lookup (gpointer instance, sl@0: gulong handler_id, sl@0: guint *signal_id_p); sl@0: static inline HandlerMatch* handler_match_prepend (HandlerMatch *list, sl@0: Handler *handler, sl@0: guint signal_id); sl@0: static inline HandlerMatch* handler_match_free1_R (HandlerMatch *node, sl@0: gpointer instance); sl@0: static HandlerMatch* handlers_find (gpointer instance, sl@0: GSignalMatchType mask, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gpointer func, sl@0: gpointer data, sl@0: gboolean one_and_only); sl@0: static inline void handler_ref (Handler *handler); sl@0: static inline void handler_unref_R (guint signal_id, sl@0: gpointer instance, sl@0: Handler *handler); sl@0: static gint handler_lists_cmp (gconstpointer node1, sl@0: gconstpointer node2); sl@0: static inline void emission_push (Emission **emission_list_p, sl@0: Emission *emission); sl@0: static inline void emission_pop (Emission **emission_list_p, sl@0: Emission *emission); sl@0: static inline Emission* emission_find (Emission *emission_list, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: gpointer instance); sl@0: static gint class_closures_cmp (gconstpointer node1, sl@0: gconstpointer node2); sl@0: static gint signal_key_cmp (gconstpointer node1, sl@0: gconstpointer node2); sl@0: static gboolean signal_emit_unlocked_R (SignalNode *node, sl@0: GQuark detail, sl@0: gpointer instance, sl@0: GValue *return_value, sl@0: const GValue *instance_and_params); sl@0: static const gchar * type_debug_name (GType type); sl@0: sl@0: sl@0: /* --- structures --- */ sl@0: typedef struct sl@0: { sl@0: GSignalAccumulator func; sl@0: gpointer data; sl@0: } SignalAccumulator; sl@0: typedef struct sl@0: { sl@0: GHook hook; sl@0: GQuark detail; sl@0: } SignalHook; sl@0: #define SIGNAL_HOOK(hook) ((SignalHook*) (hook)) sl@0: sl@0: struct _SignalNode sl@0: { sl@0: /* permanent portion */ sl@0: guint signal_id; sl@0: GType itype; sl@0: const gchar *name; sl@0: guint destroyed : 1; sl@0: sl@0: /* reinitializable portion */ sl@0: guint test_class_offset : 12; sl@0: guint flags : 8; sl@0: guint n_params : 8; sl@0: GType *param_types; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */ sl@0: GType return_type; /* mangled with G_SIGNAL_TYPE_STATIC_SCOPE flag */ sl@0: GBSearchArray *class_closure_bsa; sl@0: SignalAccumulator *accumulator; sl@0: GSignalCMarshaller c_marshaller; sl@0: GHookList *emission_hooks; sl@0: }; sl@0: #define MAX_TEST_CLASS_OFFSET (4096) /* 2^12, 12 bits for test_class_offset */ sl@0: #define TEST_CLASS_MAGIC (1) /* indicates NULL class closure, candidate for NOP optimization */ sl@0: sl@0: struct _SignalKey sl@0: { sl@0: GType itype; sl@0: GQuark quark; sl@0: guint signal_id; sl@0: }; sl@0: sl@0: struct _Emission sl@0: { sl@0: Emission *next; sl@0: gpointer instance; sl@0: GSignalInvocationHint ihint; sl@0: EmissionState state; sl@0: GType chain_type; sl@0: }; sl@0: sl@0: struct _HandlerList sl@0: { sl@0: guint signal_id; sl@0: Handler *handlers; sl@0: Handler *tail_before; /* normal signal handlers are appended here */ sl@0: Handler *tail_after; /* CONNECT_AFTER handlers are appended here */ sl@0: }; sl@0: sl@0: struct _Handler sl@0: { sl@0: gulong sequential_number; sl@0: Handler *next; sl@0: Handler *prev; sl@0: GQuark detail; sl@0: guint ref_count; sl@0: guint block_count : 16; sl@0: #define HANDLER_MAX_BLOCK_COUNT (1 << 16) sl@0: guint after : 1; sl@0: GClosure *closure; sl@0: }; sl@0: struct _HandlerMatch sl@0: { sl@0: Handler *handler; sl@0: HandlerMatch *next; sl@0: guint signal_id; sl@0: }; sl@0: sl@0: typedef struct sl@0: { sl@0: GType instance_type; /* 0 for default closure */ sl@0: GClosure *closure; sl@0: } ClassClosure; sl@0: sl@0: sl@0: /* --- variables --- */ sl@0: #if EMULATOR sl@0: sl@0: PLS(g_signal_key_bsa,gsignal,GBSearchArray *) sl@0: PLS(g_signal_hlbsa_bconfig,gsignal,GBSearchConfig) sl@0: PLS(g_class_closure_bconfig,gsignal,GBSearchConfig) sl@0: PLS(g_handler_list_bsa_ht,gsignal,GHashTable *) sl@0: PLS(g_recursive_emissions,gsignal,Emission *) sl@0: PLS(g_restart_emissions ,gsignal,Emission *) sl@0: PLS(g_handler_sequential_number ,gsignal,gulong) sl@0: PLS_MACRO(g_signal_mutex,gsignal,GStaticMutex) sl@0: PLS(g_n_signal_nodes,gsignal,guint) sl@0: PLS(g_signal_nodes ,gsignal,SignalNode **) sl@0: sl@0: #define g_signal_key_bsa (*FUNCTION_NAME(g_signal_key_bsa,gsignal)()) sl@0: #define g_signal_hlbsa_bconfig (*FUNCTION_NAME(g_signal_hlbsa_bconfig,gsignal)()) sl@0: #define g_class_closure_bconfig (*FUNCTION_NAME(g_class_closure_bconfig,gsignal)()) sl@0: #define g_handler_list_bsa_ht (*FUNCTION_NAME(g_handler_list_bsa_ht,gsignal)()) sl@0: #define g_recursive_emissions (*FUNCTION_NAME(g_recursive_emissions,gsignal)()) sl@0: #define g_restart_emissions (*FUNCTION_NAME(g_restart_emissions ,gsignal)()) sl@0: #define g_handler_sequential_number (*FUNCTION_NAME(g_handler_sequential_number ,gsignal)()) sl@0: #define g__g_signal_mutex_lock (*FUNCTION_NAME_MACRO(g_signal_mutex,gsignal)()) sl@0: #define g_n_signal_nodes (*FUNCTION_NAME(g_n_signal_nodes,gsignal)()) sl@0: #define g_signal_nodes (*FUNCTION_NAME(g_signal_nodes,gsignal)()) sl@0: sl@0: const GBSearchConfig temp_g_signal_hlbsa_bconfig = { sl@0: sizeof (HandlerList), sl@0: handler_lists_cmp, sl@0: 0, sl@0: }; sl@0: sl@0: const GBSearchConfig temp_g_class_closure_bconfig = { sl@0: sizeof (ClassClosure), sl@0: class_closures_cmp, sl@0: 0, sl@0: }; sl@0: sl@0: #else sl@0: static GBSearchArray *g_signal_key_bsa = NULL; sl@0: #endif//EMULATOR sl@0: static const GBSearchConfig g_signal_key_bconfig = { sl@0: sizeof (SignalKey), sl@0: signal_key_cmp, sl@0: G_BSEARCH_ARRAY_ALIGN_POWER2, sl@0: }; sl@0: #if (!EMULATOR) sl@0: static GBSearchConfig g_signal_hlbsa_bconfig = { sl@0: sizeof (HandlerList), sl@0: handler_lists_cmp, sl@0: 0, sl@0: }; sl@0: static GBSearchConfig g_class_closure_bconfig = { sl@0: sizeof (ClassClosure), sl@0: class_closures_cmp, sl@0: 0, sl@0: }; sl@0: static GHashTable *g_handler_list_bsa_ht = NULL; sl@0: static Emission *g_recursive_emissions = NULL; sl@0: static Emission *g_restart_emissions = NULL; sl@0: static gulong g_handler_sequential_number = 1; sl@0: G_LOCK_DEFINE_STATIC (g_signal_mutex); sl@0: #endif//EMULATOR sl@0: sl@0: #define SIGNAL_LOCK() G_LOCK (g_signal_mutex) sl@0: #define SIGNAL_UNLOCK() G_UNLOCK (g_signal_mutex) sl@0: sl@0: /* --- signal nodes --- */ sl@0: #if (!EMULATOR) sl@0: static guint g_n_signal_nodes = 0; sl@0: static SignalNode **g_signal_nodes = NULL; sl@0: sl@0: #endif /* EMULATOR */ sl@0: sl@0: static inline SignalNode* sl@0: LOOKUP_SIGNAL_NODE (register guint signal_id) sl@0: { sl@0: if (signal_id < g_n_signal_nodes) sl@0: return g_signal_nodes[signal_id]; sl@0: else sl@0: return NULL; sl@0: } sl@0: sl@0: sl@0: /* --- functions --- */ sl@0: static inline guint sl@0: signal_id_lookup (GQuark quark, sl@0: GType itype) sl@0: { sl@0: GType *ifaces, type = itype; sl@0: SignalKey key; sl@0: guint n_ifaces; sl@0: sl@0: key.quark = quark; sl@0: sl@0: /* try looking up signals for this type and its ancestors */ sl@0: do sl@0: { sl@0: SignalKey *signal_key; sl@0: sl@0: key.itype = type; sl@0: signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key); sl@0: sl@0: if (signal_key) sl@0: return signal_key->signal_id; sl@0: sl@0: type = g_type_parent (type); sl@0: } sl@0: while (type); sl@0: sl@0: /* no luck, try interfaces it exports */ sl@0: ifaces = g_type_interfaces (itype, &n_ifaces); sl@0: while (n_ifaces--) sl@0: { sl@0: SignalKey *signal_key; sl@0: sl@0: key.itype = ifaces[n_ifaces]; sl@0: signal_key = g_bsearch_array_lookup (g_signal_key_bsa, &g_signal_key_bconfig, &key); sl@0: sl@0: if (signal_key) sl@0: { sl@0: g_free (ifaces); sl@0: return signal_key->signal_id; sl@0: } sl@0: } sl@0: g_free (ifaces); sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: static gint sl@0: class_closures_cmp (gconstpointer node1, sl@0: gconstpointer node2) sl@0: { sl@0: const ClassClosure *c1 = node1, *c2 = node2; sl@0: sl@0: return G_BSEARCH_ARRAY_CMP (c1->instance_type, c2->instance_type); sl@0: } sl@0: sl@0: static gint sl@0: handler_lists_cmp (gconstpointer node1, sl@0: gconstpointer node2) sl@0: { sl@0: const HandlerList *hlist1 = node1, *hlist2 = node2; sl@0: sl@0: return G_BSEARCH_ARRAY_CMP (hlist1->signal_id, hlist2->signal_id); sl@0: } sl@0: sl@0: static inline HandlerList* sl@0: handler_list_ensure (guint signal_id, sl@0: gpointer instance) sl@0: { sl@0: GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); sl@0: HandlerList key; sl@0: sl@0: key.signal_id = signal_id; sl@0: key.handlers = NULL; sl@0: key.tail_before = NULL; sl@0: key.tail_after = NULL; sl@0: if (!hlbsa) sl@0: { sl@0: hlbsa = g_bsearch_array_create (&g_signal_hlbsa_bconfig); sl@0: hlbsa = g_bsearch_array_insert (hlbsa, &g_signal_hlbsa_bconfig, &key); sl@0: g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa); sl@0: } sl@0: else sl@0: { sl@0: GBSearchArray *o = hlbsa; sl@0: sl@0: hlbsa = g_bsearch_array_insert (o, &g_signal_hlbsa_bconfig, &key); sl@0: if (hlbsa != o) sl@0: g_hash_table_insert (g_handler_list_bsa_ht, instance, hlbsa); sl@0: } sl@0: return g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key); sl@0: } sl@0: sl@0: static inline HandlerList* sl@0: handler_list_lookup (guint signal_id, sl@0: gpointer instance) sl@0: { sl@0: GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); sl@0: HandlerList key; sl@0: sl@0: key.signal_id = signal_id; sl@0: sl@0: return hlbsa ? g_bsearch_array_lookup (hlbsa, &g_signal_hlbsa_bconfig, &key) : NULL; sl@0: } sl@0: sl@0: static Handler* sl@0: handler_lookup (gpointer instance, sl@0: gulong handler_id, sl@0: guint *signal_id_p) sl@0: { sl@0: GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); sl@0: sl@0: if (hlbsa) sl@0: { sl@0: guint i; sl@0: sl@0: for (i = 0; i < hlbsa->n_nodes; i++) sl@0: { sl@0: HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i); sl@0: Handler *handler; sl@0: sl@0: for (handler = hlist->handlers; handler; handler = handler->next) sl@0: if (handler->sequential_number == handler_id) sl@0: { sl@0: if (signal_id_p) sl@0: *signal_id_p = hlist->signal_id; sl@0: sl@0: return handler; sl@0: } sl@0: } sl@0: } sl@0: sl@0: return NULL; sl@0: } sl@0: sl@0: static inline HandlerMatch* sl@0: handler_match_prepend (HandlerMatch *list, sl@0: Handler *handler, sl@0: guint signal_id) sl@0: { sl@0: HandlerMatch *node; sl@0: sl@0: node = g_slice_new (HandlerMatch); sl@0: node->handler = handler; sl@0: node->next = list; sl@0: node->signal_id = signal_id; sl@0: handler_ref (handler); sl@0: sl@0: return node; sl@0: } sl@0: static inline HandlerMatch* sl@0: handler_match_free1_R (HandlerMatch *node, sl@0: gpointer instance) sl@0: { sl@0: HandlerMatch *next = node->next; sl@0: sl@0: handler_unref_R (node->signal_id, instance, node->handler); sl@0: g_slice_free (HandlerMatch, node); sl@0: sl@0: return next; sl@0: } sl@0: sl@0: static HandlerMatch* sl@0: handlers_find (gpointer instance, sl@0: GSignalMatchType mask, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gpointer func, sl@0: gpointer data, sl@0: gboolean one_and_only) sl@0: { sl@0: HandlerMatch *mlist = NULL; sl@0: sl@0: if (mask & G_SIGNAL_MATCH_ID) sl@0: { sl@0: HandlerList *hlist = handler_list_lookup (signal_id, instance); sl@0: Handler *handler; sl@0: SignalNode *node = NULL; sl@0: sl@0: if (mask & G_SIGNAL_MATCH_FUNC) sl@0: { sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (!node || !node->c_marshaller) sl@0: return NULL; sl@0: } sl@0: sl@0: mask = ~mask; sl@0: for (handler = hlist ? hlist->handlers : NULL; handler; handler = handler->next) sl@0: if (handler->sequential_number && sl@0: ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) && sl@0: ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) && sl@0: ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) && sl@0: ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) && sl@0: ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller && sl@0: handler->closure->meta_marshal == 0 && sl@0: ((GCClosure*) handler->closure)->callback == func))) sl@0: { sl@0: mlist = handler_match_prepend (mlist, handler, signal_id); sl@0: if (one_and_only) sl@0: return mlist; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: GBSearchArray *hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); sl@0: sl@0: mask = ~mask; sl@0: if (hlbsa) sl@0: { sl@0: guint i; sl@0: sl@0: for (i = 0; i < hlbsa->n_nodes; i++) sl@0: { sl@0: HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i); sl@0: SignalNode *node = NULL; sl@0: Handler *handler; sl@0: sl@0: if (!(mask & G_SIGNAL_MATCH_FUNC)) sl@0: { sl@0: node = LOOKUP_SIGNAL_NODE (hlist->signal_id); sl@0: if (!node->c_marshaller) sl@0: continue; sl@0: } sl@0: sl@0: for (handler = hlist->handlers; handler; handler = handler->next) sl@0: if (handler->sequential_number && sl@0: ((mask & G_SIGNAL_MATCH_DETAIL) || handler->detail == detail) && sl@0: ((mask & G_SIGNAL_MATCH_CLOSURE) || handler->closure == closure) && sl@0: ((mask & G_SIGNAL_MATCH_DATA) || handler->closure->data == data) && sl@0: ((mask & G_SIGNAL_MATCH_UNBLOCKED) || handler->block_count == 0) && sl@0: ((mask & G_SIGNAL_MATCH_FUNC) || (handler->closure->marshal == node->c_marshaller && sl@0: handler->closure->meta_marshal == 0 && sl@0: ((GCClosure*) handler->closure)->callback == func))) sl@0: { sl@0: mlist = handler_match_prepend (mlist, handler, hlist->signal_id); sl@0: if (one_and_only) sl@0: return mlist; sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: return mlist; sl@0: } sl@0: sl@0: static inline Handler* sl@0: handler_new (gboolean after) sl@0: { sl@0: Handler *handler = g_slice_new (Handler); sl@0: #ifndef G_DISABLE_CHECKS sl@0: if (g_handler_sequential_number < 1) sl@0: g_error (G_STRLOC ": handler id overflow, %s", REPORT_BUG); sl@0: #endif sl@0: sl@0: handler->sequential_number = g_handler_sequential_number++; sl@0: handler->prev = NULL; sl@0: handler->next = NULL; sl@0: handler->detail = 0; sl@0: handler->ref_count = 1; sl@0: handler->block_count = 0; sl@0: handler->after = after != FALSE; sl@0: handler->closure = NULL; sl@0: sl@0: return handler; sl@0: } sl@0: sl@0: static inline void sl@0: handler_ref (Handler *handler) sl@0: { sl@0: g_return_if_fail (handler->ref_count > 0); sl@0: sl@0: g_atomic_int_inc ((int *)&handler->ref_count); sl@0: } sl@0: sl@0: static inline void sl@0: handler_unref_R (guint signal_id, sl@0: gpointer instance, sl@0: Handler *handler) sl@0: { sl@0: gboolean is_zero; sl@0: sl@0: g_return_if_fail (handler->ref_count > 0); sl@0: sl@0: is_zero = g_atomic_int_dec_and_test ((int *)&handler->ref_count); sl@0: sl@0: if (G_UNLIKELY (is_zero)) sl@0: { sl@0: HandlerList *hlist = NULL; sl@0: sl@0: if (handler->next) sl@0: handler->next->prev = handler->prev; sl@0: if (handler->prev) /* watch out for g_signal_handlers_destroy()! */ sl@0: handler->prev->next = handler->next; sl@0: else sl@0: { sl@0: hlist = handler_list_lookup (signal_id, instance); sl@0: hlist->handlers = handler->next; sl@0: } sl@0: sl@0: if (instance) sl@0: { sl@0: /* check if we are removing the handler pointed to by tail_before */ sl@0: if (!handler->after && (!handler->next || handler->next->after)) sl@0: { sl@0: if (!hlist) sl@0: hlist = handler_list_lookup (signal_id, instance); sl@0: if (hlist) sl@0: { sl@0: g_assert (hlist->tail_before == handler); /* paranoid */ sl@0: hlist->tail_before = handler->prev; sl@0: } sl@0: } sl@0: sl@0: /* check if we are removing the handler pointed to by tail_after */ sl@0: if (!handler->next) sl@0: { sl@0: if (!hlist) sl@0: hlist = handler_list_lookup (signal_id, instance); sl@0: if (hlist) sl@0: { sl@0: g_assert (hlist->tail_after == handler); /* paranoid */ sl@0: hlist->tail_after = handler->prev; sl@0: } sl@0: } sl@0: } sl@0: sl@0: SIGNAL_UNLOCK (); sl@0: g_closure_unref (handler->closure); sl@0: SIGNAL_LOCK (); sl@0: g_slice_free (Handler, handler); sl@0: } sl@0: } sl@0: sl@0: static void sl@0: handler_insert (guint signal_id, sl@0: gpointer instance, sl@0: Handler *handler) sl@0: { sl@0: HandlerList *hlist; sl@0: sl@0: g_assert (handler->prev == NULL && handler->next == NULL); /* paranoid */ sl@0: sl@0: hlist = handler_list_ensure (signal_id, instance); sl@0: if (!hlist->handlers) sl@0: { sl@0: hlist->handlers = handler; sl@0: if (!handler->after) sl@0: hlist->tail_before = handler; sl@0: } sl@0: else if (handler->after) sl@0: { sl@0: handler->prev = hlist->tail_after; sl@0: hlist->tail_after->next = handler; sl@0: } sl@0: else sl@0: { sl@0: if (hlist->tail_before) sl@0: { sl@0: handler->next = hlist->tail_before->next; sl@0: if (handler->next) sl@0: handler->next->prev = handler; sl@0: handler->prev = hlist->tail_before; sl@0: hlist->tail_before->next = handler; sl@0: } sl@0: else /* insert !after handler into a list of only after handlers */ sl@0: { sl@0: handler->next = hlist->handlers; sl@0: if (handler->next) sl@0: handler->next->prev = handler; sl@0: hlist->handlers = handler; sl@0: } sl@0: hlist->tail_before = handler; sl@0: } sl@0: sl@0: if (!handler->next) sl@0: hlist->tail_after = handler; sl@0: } sl@0: sl@0: static inline void sl@0: emission_push (Emission **emission_list_p, sl@0: Emission *emission) sl@0: { sl@0: emission->next = *emission_list_p; sl@0: *emission_list_p = emission; sl@0: } sl@0: sl@0: static inline void sl@0: emission_pop (Emission **emission_list_p, sl@0: Emission *emission) sl@0: { sl@0: Emission *node, *last = NULL; sl@0: sl@0: for (node = *emission_list_p; node; last = node, node = last->next) sl@0: if (node == emission) sl@0: { sl@0: if (last) sl@0: last->next = node->next; sl@0: else sl@0: *emission_list_p = node->next; sl@0: return; sl@0: } sl@0: g_assert_not_reached (); sl@0: } sl@0: sl@0: static inline Emission* sl@0: emission_find (Emission *emission_list, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: gpointer instance) sl@0: { sl@0: Emission *emission; sl@0: sl@0: for (emission = emission_list; emission; emission = emission->next) sl@0: if (emission->instance == instance && sl@0: emission->ihint.signal_id == signal_id && sl@0: emission->ihint.detail == detail) sl@0: return emission; sl@0: return NULL; sl@0: } sl@0: sl@0: static inline Emission* sl@0: emission_find_innermost (gpointer instance) sl@0: { sl@0: Emission *emission, *s = NULL, *c = NULL; sl@0: sl@0: for (emission = g_restart_emissions; emission; emission = emission->next) sl@0: if (emission->instance == instance) sl@0: { sl@0: s = emission; sl@0: break; sl@0: } sl@0: for (emission = g_recursive_emissions; emission; emission = emission->next) sl@0: if (emission->instance == instance) sl@0: { sl@0: c = emission; sl@0: break; sl@0: } sl@0: if (!s) sl@0: return c; sl@0: else if (!c) sl@0: return s; sl@0: else sl@0: return G_HAVE_GROWING_STACK ? MAX (c, s) : MIN (c, s); sl@0: } sl@0: sl@0: static gint sl@0: signal_key_cmp (gconstpointer node1, sl@0: gconstpointer node2) sl@0: { sl@0: const SignalKey *key1 = node1, *key2 = node2; sl@0: sl@0: if (key1->itype == key2->itype) sl@0: return G_BSEARCH_ARRAY_CMP (key1->quark, key2->quark); sl@0: else sl@0: return G_BSEARCH_ARRAY_CMP (key1->itype, key2->itype); sl@0: } sl@0: sl@0: void sl@0: g_signal_init (void) sl@0: { sl@0: SIGNAL_LOCK (); sl@0: if (!g_n_signal_nodes) sl@0: { sl@0: /* setup handler list binary searchable array hash table (in german, that'd be one word ;) */ sl@0: g_handler_list_bsa_ht = g_hash_table_new (g_direct_hash, NULL); sl@0: g_signal_key_bsa = g_bsearch_array_create (&g_signal_key_bconfig); sl@0: sl@0: /* invalid (0) signal_id */ sl@0: g_n_signal_nodes = 1; sl@0: g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes); sl@0: g_signal_nodes[0] = NULL; sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: void sl@0: _g_signals_destroy (GType itype) sl@0: { sl@0: guint i; sl@0: sl@0: SIGNAL_LOCK (); sl@0: for (i = 1; i < g_n_signal_nodes; i++) sl@0: { sl@0: SignalNode *node = g_signal_nodes[i]; sl@0: sl@0: if (node->itype == itype) sl@0: { sl@0: if (node->destroyed) sl@0: g_warning (G_STRLOC ": signal \"%s\" of type `%s' already destroyed", sl@0: node->name, sl@0: type_debug_name (node->itype)); sl@0: else sl@0: signal_destroy_R (node); sl@0: } sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_stop_emission: sl@0: * @instance: the object whose signal handlers you wish to stop. sl@0: * @signal_id: the signal identifier, as returned by g_signal_lookup(). sl@0: * @detail: the detail which the signal was emitted with. sl@0: * sl@0: * Stops a signal's current emission. sl@0: * sl@0: * This will prevent the default method from running, if the signal was sl@0: * %G_SIGNAL_RUN_LAST and you connected normally (i.e. without the "after" sl@0: * flag). sl@0: * sl@0: * Prints a warning if used on a signal which isn't being emitted. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_stop_emission (gpointer instance, sl@0: guint signal_id, sl@0: GQuark detail) sl@0: { sl@0: SignalNode *node; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (signal_id > 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (node && detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: { sl@0: g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: if (node && g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) sl@0: { sl@0: Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions; sl@0: Emission *emission = emission_find (emission_list, signal_id, detail, instance); sl@0: sl@0: if (emission) sl@0: { sl@0: if (emission->state == EMISSION_HOOK) sl@0: g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook", sl@0: node->name, instance); sl@0: else if (emission->state == EMISSION_RUN) sl@0: emission->state = EMISSION_STOP; sl@0: } sl@0: else sl@0: g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'", sl@0: node->name, instance); sl@0: } sl@0: else sl@0: g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: static void sl@0: signal_finalize_hook (GHookList *hook_list, sl@0: GHook *hook) sl@0: { sl@0: GDestroyNotify destroy = hook->destroy; sl@0: sl@0: if (destroy) sl@0: { sl@0: hook->destroy = NULL; sl@0: SIGNAL_UNLOCK (); sl@0: destroy (hook->data); sl@0: SIGNAL_LOCK (); sl@0: } sl@0: } sl@0: sl@0: #if EMULATOR sl@0: PLS(seq_hook_id ,g_signal_add_emission_hook ,gulong) sl@0: #define seq_hook_id (*FUNCTION_NAME(seq_hook_id ,g_signal_add_emission_hook)()) sl@0: #endif /* EMULATOR */ sl@0: sl@0: /** sl@0: * g_signal_add_emission_hook: sl@0: * @signal_id: the signal identifier, as returned by g_signal_lookup(). sl@0: * @detail: the detail on which to call the hook. sl@0: * @hook_func: a #GSignalEmissionHook function. sl@0: * @hook_data: user data for @hook_func. sl@0: * @data_destroy: a #GDestroyNotify for @hook_data. sl@0: * sl@0: * Adds an emission hook for a signal, which will get called for any emission sl@0: * of that signal, independent of the instance. This is possible only sl@0: * for signals which don't have #G_SIGNAL_NO_HOOKS flag set. sl@0: * sl@0: * Returns: the hook id, for later use with g_signal_remove_emission_hook(). sl@0: */ sl@0: EXPORT_C gulong sl@0: g_signal_add_emission_hook (guint signal_id, sl@0: GQuark detail, sl@0: GSignalEmissionHook hook_func, sl@0: gpointer hook_data, sl@0: GDestroyNotify data_destroy) sl@0: { sl@0: #if !(EMULATOR) sl@0: static gulong seq_hook_id = 1; sl@0: #endif /* EMULATOR */ sl@0: SignalNode *node; sl@0: GHook *hook; sl@0: SignalHook *signal_hook; sl@0: sl@0: g_return_val_if_fail (signal_id > 0, 0); sl@0: g_return_val_if_fail (hook_func != NULL, 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (!node || node->destroyed) sl@0: { sl@0: g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: if (node->flags & G_SIGNAL_NO_HOOKS) sl@0: { sl@0: g_warning ("%s: signal id `%u' does not support emission hooks (G_SIGNAL_NO_HOOKS flag set)", G_STRLOC, signal_id); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: if (detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: { sl@0: g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: if (!node->emission_hooks) sl@0: { sl@0: node->emission_hooks = g_new (GHookList, 1); sl@0: g_hook_list_init (node->emission_hooks, sizeof (SignalHook)); sl@0: node->emission_hooks->finalize_hook = signal_finalize_hook; sl@0: } sl@0: hook = g_hook_alloc (node->emission_hooks); sl@0: hook->data = hook_data; sl@0: hook->func = (gpointer) hook_func; sl@0: hook->destroy = data_destroy; sl@0: signal_hook = SIGNAL_HOOK (hook); sl@0: signal_hook->detail = detail; sl@0: node->emission_hooks->seq_id = seq_hook_id; sl@0: g_hook_append (node->emission_hooks, hook); sl@0: seq_hook_id = node->emission_hooks->seq_id; sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return hook->hook_id; sl@0: } sl@0: sl@0: #if EMULATOR sl@0: #undef seq_hook_id sl@0: #endif /* EMULATOR */ sl@0: sl@0: /** sl@0: * g_signal_remove_emission_hook: sl@0: * @signal_id: the id of the signal sl@0: * @hook_id: the id of the emission hook, as returned by sl@0: * g_signal_add_emission_hook() sl@0: * sl@0: * Deletes an emission hook. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_remove_emission_hook (guint signal_id, sl@0: gulong hook_id) sl@0: { sl@0: SignalNode *node; sl@0: sl@0: g_return_if_fail (signal_id > 0); sl@0: g_return_if_fail (hook_id > 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (!node || node->destroyed) sl@0: g_warning ("%s: invalid signal id `%u'", G_STRLOC, signal_id); sl@0: else if (!node->emission_hooks || !g_hook_destroy (node->emission_hooks, hook_id)) sl@0: g_warning ("%s: signal \"%s\" had no hook (%lu) to remove", G_STRLOC, node->name, hook_id); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: static inline guint sl@0: signal_parse_name (const gchar *name, sl@0: GType itype, sl@0: GQuark *detail_p, sl@0: gboolean force_quark) sl@0: { sl@0: const gchar *colon = strchr (name, ':'); sl@0: guint signal_id; sl@0: sl@0: if (!colon) sl@0: { sl@0: signal_id = signal_id_lookup (g_quark_try_string (name), itype); sl@0: if (signal_id && detail_p) sl@0: *detail_p = 0; sl@0: } sl@0: else if (colon[1] == ':') sl@0: { sl@0: gchar buffer[32]; sl@0: guint l = colon - name; sl@0: sl@0: if (l < 32) sl@0: { sl@0: memcpy (buffer, name, l); sl@0: buffer[l] = 0; sl@0: signal_id = signal_id_lookup (g_quark_try_string (buffer), itype); sl@0: } sl@0: else sl@0: { sl@0: gchar *signal = g_new (gchar, l + 1); sl@0: sl@0: memcpy (signal, name, l); sl@0: signal[l] = 0; sl@0: signal_id = signal_id_lookup (g_quark_try_string (signal), itype); sl@0: g_free (signal); sl@0: } sl@0: sl@0: if (signal_id && detail_p) sl@0: *detail_p = colon[2] ? (force_quark ? g_quark_from_string : g_quark_try_string) (colon + 2) : 0; sl@0: } sl@0: else sl@0: signal_id = 0; sl@0: return signal_id; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_parse_name: sl@0: * @detailed_signal: a string of the form "signal-name::detail". sl@0: * @itype: The interface/instance type that introduced "signal-name". sl@0: * @signal_id_p: Location to store the signal id. sl@0: * @detail_p: Location to store the detail quark. sl@0: * @force_detail_quark: %TRUE forces creation of a #GQuark for the detail. sl@0: * sl@0: * Internal function to parse a signal name into its @signal_id sl@0: * and @detail quark. sl@0: * sl@0: * Returns: Whether the signal name could successfully be parsed and @signal_id_p and @detail_p contain valid return values. sl@0: */ sl@0: EXPORT_C gboolean sl@0: g_signal_parse_name (const gchar *detailed_signal, sl@0: GType itype, sl@0: guint *signal_id_p, sl@0: GQuark *detail_p, sl@0: gboolean force_detail_quark) sl@0: { sl@0: SignalNode *node; sl@0: GQuark detail = 0; sl@0: guint signal_id; sl@0: sl@0: g_return_val_if_fail (detailed_signal != NULL, FALSE); sl@0: g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), FALSE); sl@0: sl@0: SIGNAL_LOCK (); sl@0: signal_id = signal_parse_name (detailed_signal, itype, &detail, force_detail_quark); sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: node = signal_id ? LOOKUP_SIGNAL_NODE (signal_id) : NULL; sl@0: if (!node || node->destroyed || sl@0: (detail && !(node->flags & G_SIGNAL_DETAILED))) sl@0: return FALSE; sl@0: sl@0: if (signal_id_p) sl@0: *signal_id_p = signal_id; sl@0: if (detail_p) sl@0: *detail_p = detail; sl@0: sl@0: return TRUE; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_stop_emission_by_name: sl@0: * @instance: the object whose signal handlers you wish to stop. sl@0: * @detailed_signal: a string of the form "signal-name::detail". sl@0: * sl@0: * Stops a signal's current emission. sl@0: * sl@0: * This is just like g_signal_stop_emission() except it will look up the sl@0: * signal id for you. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_stop_emission_by_name (gpointer instance, sl@0: const gchar *detailed_signal) sl@0: { sl@0: guint signal_id; sl@0: GQuark detail = 0; sl@0: GType itype; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (detailed_signal != NULL); sl@0: sl@0: SIGNAL_LOCK (); sl@0: itype = G_TYPE_FROM_INSTANCE (instance); sl@0: signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE); sl@0: if (signal_id) sl@0: { sl@0: SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: sl@0: if (detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal); sl@0: else if (!g_type_is_a (itype, node->itype)) sl@0: g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance); sl@0: else sl@0: { sl@0: Emission *emission_list = node->flags & G_SIGNAL_NO_RECURSE ? g_restart_emissions : g_recursive_emissions; sl@0: Emission *emission = emission_find (emission_list, signal_id, detail, instance); sl@0: sl@0: if (emission) sl@0: { sl@0: if (emission->state == EMISSION_HOOK) sl@0: g_warning (G_STRLOC ": emission of signal \"%s\" for instance `%p' cannot be stopped from emission hook", sl@0: node->name, instance); sl@0: else if (emission->state == EMISSION_RUN) sl@0: emission->state = EMISSION_STOP; sl@0: } sl@0: else sl@0: g_warning (G_STRLOC ": no emission of signal \"%s\" to stop for instance `%p'", sl@0: node->name, instance); sl@0: } sl@0: } sl@0: else sl@0: g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_lookup: sl@0: * @name: the signal's name. sl@0: * @itype: the type that the signal operates on. sl@0: * sl@0: * Given the name of the signal and the type of object it connects to, gets sl@0: * the signal's identifying integer. Emitting the signal by number is sl@0: * somewhat faster than using the name each time. sl@0: * sl@0: * Also tries the ancestors of the given type. sl@0: * sl@0: * See g_signal_new() for details on allowed signal names. sl@0: * sl@0: * Returns: the signal's identifying number, or 0 if no signal was found. sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_lookup (const gchar *name, sl@0: GType itype) sl@0: { sl@0: guint signal_id; sl@0: g_return_val_if_fail (name != NULL, 0); sl@0: g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: signal_id = signal_id_lookup (g_quark_try_string (name), itype); sl@0: SIGNAL_UNLOCK (); sl@0: if (!signal_id) sl@0: { sl@0: /* give elaborate warnings */ sl@0: if (!g_type_name (itype)) sl@0: g_warning (G_STRLOC ": unable to lookup signal \"%s\" for invalid type id `%"G_GSIZE_FORMAT"'", sl@0: name, itype); sl@0: else if (!G_TYPE_IS_INSTANTIATABLE (itype)) sl@0: g_warning (G_STRLOC ": unable to lookup signal \"%s\" for non instantiatable type `%s'", sl@0: name, g_type_name (itype)); sl@0: else if (!g_type_class_peek (itype)) sl@0: g_warning (G_STRLOC ": unable to lookup signal \"%s\" of unloaded type `%s'", sl@0: name, g_type_name (itype)); sl@0: } sl@0: sl@0: return signal_id; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_list_ids: sl@0: * @itype: Instance or interface type. sl@0: * @n_ids: Location to store the number of signal ids for @itype. sl@0: * sl@0: * Lists the signals by id that a certain instance or interface type sl@0: * created. Further information about the signals can be acquired through sl@0: * g_signal_query(). sl@0: * sl@0: * Returns: Newly allocated array of signal IDs. sl@0: */ sl@0: EXPORT_C guint* sl@0: g_signal_list_ids (GType itype, sl@0: guint *n_ids) sl@0: { sl@0: SignalKey *keys; sl@0: GArray *result; sl@0: guint n_nodes; sl@0: guint i; sl@0: sl@0: g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), NULL); sl@0: g_return_val_if_fail (n_ids != NULL, NULL); sl@0: sl@0: SIGNAL_LOCK (); sl@0: keys = g_bsearch_array_get_nth (g_signal_key_bsa, &g_signal_key_bconfig, 0); sl@0: n_nodes = g_bsearch_array_get_n_nodes (g_signal_key_bsa); sl@0: result = g_array_new (FALSE, FALSE, sizeof (guint)); sl@0: sl@0: for (i = 0; i < n_nodes; i++) sl@0: if (keys[i].itype == itype) sl@0: { sl@0: const gchar *name = g_quark_to_string (keys[i].quark); sl@0: sl@0: /* Signal names with "_" in them are aliases to the same sl@0: * name with "-" instead of "_". sl@0: */ sl@0: if (!strchr (name, '_')) sl@0: g_array_append_val (result, keys[i].signal_id); sl@0: } sl@0: *n_ids = result->len; sl@0: SIGNAL_UNLOCK (); sl@0: if (!n_nodes) sl@0: { sl@0: /* give elaborate warnings */ sl@0: if (!g_type_name (itype)) sl@0: g_warning (G_STRLOC ": unable to list signals for invalid type id `%"G_GSIZE_FORMAT"'", sl@0: itype); sl@0: else if (!G_TYPE_IS_INSTANTIATABLE (itype) && !G_TYPE_IS_INTERFACE (itype)) sl@0: g_warning (G_STRLOC ": unable to list signals of non instantiatable type `%s'", sl@0: g_type_name (itype)); sl@0: else if (!g_type_class_peek (itype) && !G_TYPE_IS_INTERFACE (itype)) sl@0: g_warning (G_STRLOC ": unable to list signals of unloaded type `%s'", sl@0: g_type_name (itype)); sl@0: } sl@0: sl@0: return (guint*) g_array_free (result, FALSE); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_name: sl@0: * @signal_id: the signal's identifying number. sl@0: * sl@0: * Given the signal's identifier, finds its name. sl@0: * sl@0: * Two different signals may have the same name, if they have differing types. sl@0: * sl@0: * Returns: the signal name, or %NULL if the signal number was invalid. sl@0: */ sl@0: EXPORT_C G_CONST_RETURN gchar* sl@0: g_signal_name (guint signal_id) sl@0: { sl@0: SignalNode *node; sl@0: const gchar *name; sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: name = node ? node->name : NULL; sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return (char*) name; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_query: sl@0: * @signal_id: The signal id of the signal to query information for. sl@0: * @query: A user provided structure that is filled in with constant sl@0: * values upon success. sl@0: * sl@0: * Queries the signal system for in-depth information about a sl@0: * specific signal. This function will fill in a user-provided sl@0: * structure to hold signal-specific information. If an invalid sl@0: * signal id is passed in, the @signal_id member of the #GSignalQuery sl@0: * is 0. All members filled into the #GSignalQuery structure should sl@0: * be considered constant and have to be left untouched. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_query (guint signal_id, sl@0: GSignalQuery *query) sl@0: { sl@0: SignalNode *node; sl@0: sl@0: g_return_if_fail (query != NULL); sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (!node || node->destroyed) sl@0: query->signal_id = 0; sl@0: else sl@0: { sl@0: query->signal_id = node->signal_id; sl@0: query->signal_name = node->name; sl@0: query->itype = node->itype; sl@0: query->signal_flags = node->flags; sl@0: query->return_type = node->return_type; sl@0: query->n_params = node->n_params; sl@0: query->param_types = node->param_types; sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_new: sl@0: * @signal_name: the name for the signal sl@0: * @itype: the type this signal pertains to. It will also pertain to sl@0: * types which are derived from this type. sl@0: * @signal_flags: a combination of #GSignalFlags specifying detail of when sl@0: * the default handler is to be invoked. You should at least specify sl@0: * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST. sl@0: * @class_offset: The offset of the function pointer in the class structure sl@0: * for this type. Used to invoke a class method generically. Pass 0 to sl@0: * not associate a class method with this signal. sl@0: * @accumulator: the accumulator for this signal; may be %NULL. sl@0: * @accu_data: user data for the @accumulator. sl@0: * @c_marshaller: the function to translate arrays of parameter values to sl@0: * signal emissions into C language callback invocations. sl@0: * @return_type: the type of return value, or #G_TYPE_NONE for a signal sl@0: * without a return value. sl@0: * @n_params: the number of parameter types to follow. sl@0: * @...: a list of types, one for each parameter. sl@0: * sl@0: * Creates a new signal. (This is usually done in the class initializer.) sl@0: * sl@0: * A signal name consists of segments consisting of ASCII letters and sl@0: * digits, separated by either the '-' or '_' character. The first sl@0: * character of a signal name must be a letter. Names which violate these sl@0: * rules lead to undefined behaviour of the GSignal system. sl@0: * sl@0: * When registering a signal and looking up a signal, either separator can sl@0: * be used, but they cannot be mixed. sl@0: * sl@0: * Returns: the signal id sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_new (const gchar *signal_name, sl@0: GType itype, sl@0: GSignalFlags signal_flags, sl@0: guint class_offset, sl@0: GSignalAccumulator accumulator, sl@0: gpointer accu_data, sl@0: GSignalCMarshaller c_marshaller, sl@0: GType return_type, sl@0: guint n_params, sl@0: ...) sl@0: { sl@0: va_list args; sl@0: guint signal_id; sl@0: sl@0: g_return_val_if_fail (signal_name != NULL, 0); sl@0: sl@0: va_start (args, n_params); sl@0: sl@0: signal_id = g_signal_new_valist (signal_name, itype, signal_flags, sl@0: class_offset ? g_signal_type_cclosure_new (itype, class_offset) : NULL, sl@0: accumulator, accu_data, c_marshaller, sl@0: return_type, n_params, args); sl@0: sl@0: va_end (args); sl@0: sl@0: /* optimize NOP emissions with NULL class handlers */ sl@0: if (signal_id && G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE && sl@0: class_offset && class_offset < MAX_TEST_CLASS_OFFSET) sl@0: { sl@0: SignalNode *node; sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: node->test_class_offset = class_offset; sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: return signal_id; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_new_class_handler: sl@0: * @signal_name: the name for the signal sl@0: * @itype: the type this signal pertains to. It will also pertain to sl@0: * types which are derived from this type. sl@0: * @signal_flags: a combination of #GSignalFlags specifying detail of when sl@0: * the default handler is to be invoked. You should at least specify sl@0: * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST. sl@0: * @class_handler: a #GCallback which acts as class implementation of sl@0: * this signal. Used to invoke a class method generically. Pass %NULL to sl@0: * not associate a class method with this signal. sl@0: * @accumulator: the accumulator for this signal; may be %NULL. sl@0: * @accu_data: user data for the @accumulator. sl@0: * @c_marshaller: the function to translate arrays of parameter values to sl@0: * signal emissions into C language callback invocations. sl@0: * @return_type: the type of return value, or #G_TYPE_NONE for a signal sl@0: * without a return value. sl@0: * @n_params: the number of parameter types to follow. sl@0: * @...: a list of types, one for each parameter. sl@0: * sl@0: * Creates a new signal. (This is usually done in the class initializer.) sl@0: * sl@0: * This is a variant of g_signal_new() that takes a C callback instead sl@0: * off a class offset for the signal's class handler. This function sl@0: * doesn't need a function pointer exposed in the class structure of sl@0: * an object definition, instead the function pointer is passed sl@0: * directly and can be overriden by derived classes with sl@0: * g_signal_override_class_closure() or sl@0: * g_signal_override_class_handler()and chained to with sl@0: * g_signal_chain_from_overridden() or sl@0: * g_signal_chain_from_overridden_handler(). sl@0: * sl@0: * See g_signal_new() for information about signal names. sl@0: * sl@0: * Returns: the signal id sl@0: * sl@0: * Since: 2.18 sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_new_class_handler (const gchar *signal_name, sl@0: GType itype, sl@0: GSignalFlags signal_flags, sl@0: GCallback class_handler, sl@0: GSignalAccumulator accumulator, sl@0: gpointer accu_data, sl@0: GSignalCMarshaller c_marshaller, sl@0: GType return_type, sl@0: guint n_params, sl@0: ...) sl@0: { sl@0: va_list args; sl@0: guint signal_id; sl@0: sl@0: g_return_val_if_fail (signal_name != NULL, 0); sl@0: sl@0: va_start (args, n_params); sl@0: sl@0: signal_id = g_signal_new_valist (signal_name, itype, signal_flags, sl@0: class_handler ? g_cclosure_new (class_handler, NULL, NULL) : NULL, sl@0: accumulator, accu_data, c_marshaller, sl@0: return_type, n_params, args); sl@0: sl@0: va_end (args); sl@0: sl@0: return signal_id; sl@0: } sl@0: sl@0: static inline ClassClosure* sl@0: signal_find_class_closure (SignalNode *node, sl@0: GType itype) sl@0: { sl@0: GBSearchArray *bsa = node->class_closure_bsa; sl@0: ClassClosure *cc; sl@0: sl@0: if (bsa) sl@0: { sl@0: ClassClosure key; sl@0: sl@0: /* cc->instance_type is 0 for default closure */ sl@0: sl@0: key.instance_type = itype; sl@0: cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key); sl@0: while (!cc && key.instance_type) sl@0: { sl@0: key.instance_type = g_type_parent (key.instance_type); sl@0: cc = g_bsearch_array_lookup (bsa, &g_class_closure_bconfig, &key); sl@0: } sl@0: } sl@0: else sl@0: cc = NULL; sl@0: return cc; sl@0: } sl@0: sl@0: static inline GClosure* sl@0: signal_lookup_closure (SignalNode *node, sl@0: GTypeInstance *instance) sl@0: { sl@0: ClassClosure *cc; sl@0: sl@0: if (node->class_closure_bsa && g_bsearch_array_get_n_nodes (node->class_closure_bsa) == 1) sl@0: { sl@0: cc = g_bsearch_array_get_nth (node->class_closure_bsa, &g_class_closure_bconfig, 0); sl@0: if (cc && cc->instance_type == 0) /* check for default closure */ sl@0: return cc->closure; sl@0: } sl@0: cc = signal_find_class_closure (node, G_TYPE_FROM_INSTANCE (instance)); sl@0: return cc ? cc->closure : NULL; sl@0: } sl@0: sl@0: static void sl@0: signal_add_class_closure (SignalNode *node, sl@0: GType itype, sl@0: GClosure *closure) sl@0: { sl@0: ClassClosure key; sl@0: sl@0: /* can't optimize NOP emissions with overridden class closures */ sl@0: node->test_class_offset = 0; sl@0: sl@0: if (!node->class_closure_bsa) sl@0: node->class_closure_bsa = g_bsearch_array_create (&g_class_closure_bconfig); sl@0: key.instance_type = itype; sl@0: key.closure = g_closure_ref (closure); sl@0: node->class_closure_bsa = g_bsearch_array_insert (node->class_closure_bsa, sl@0: &g_class_closure_bconfig, sl@0: &key); sl@0: g_closure_sink (closure); sl@0: if (node->c_marshaller && closure && G_CLOSURE_NEEDS_MARSHAL (closure)) sl@0: g_closure_set_marshal (closure, node->c_marshaller); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_newv: sl@0: * @signal_name: the name for the signal sl@0: * @itype: the type this signal pertains to. It will also pertain to sl@0: * types which are derived from this type sl@0: * @signal_flags: a combination of #GSignalFlags specifying detail of when sl@0: * the default handler is to be invoked. You should at least specify sl@0: * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST sl@0: * @class_closure: The closure to invoke on signal emission; may be %NULL sl@0: * @accumulator: the accumulator for this signal; may be %NULL sl@0: * @accu_data: user data for the @accumulator sl@0: * @c_marshaller: the function to translate arrays of parameter values to sl@0: * signal emissions into C language callback invocations sl@0: * @return_type: the type of return value, or #G_TYPE_NONE for a signal sl@0: * without a return value sl@0: * @n_params: the length of @param_types sl@0: * @param_types: an array of types, one for each parameter sl@0: * sl@0: * Creates a new signal. (This is usually done in the class initializer.) sl@0: * sl@0: * See g_signal_new() for details on allowed signal names. sl@0: * sl@0: * Returns: the signal id sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_newv (const gchar *signal_name, sl@0: GType itype, sl@0: GSignalFlags signal_flags, sl@0: GClosure *class_closure, sl@0: GSignalAccumulator accumulator, sl@0: gpointer accu_data, sl@0: GSignalCMarshaller c_marshaller, sl@0: GType return_type, sl@0: guint n_params, sl@0: GType *param_types) sl@0: { sl@0: gchar *name; sl@0: guint signal_id, i; sl@0: SignalNode *node; sl@0: sl@0: g_return_val_if_fail (signal_name != NULL, 0); sl@0: g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (itype) || G_TYPE_IS_INTERFACE (itype), 0); sl@0: if (n_params) sl@0: g_return_val_if_fail (param_types != NULL, 0); sl@0: g_return_val_if_fail ((return_type & G_SIGNAL_TYPE_STATIC_SCOPE) == 0, 0); sl@0: if (return_type == (G_TYPE_NONE & ~G_SIGNAL_TYPE_STATIC_SCOPE)) sl@0: g_return_val_if_fail (accumulator == NULL, 0); sl@0: if (!accumulator) sl@0: g_return_val_if_fail (accu_data == NULL, 0); sl@0: sl@0: name = g_strdup (signal_name); sl@0: g_strdelimit (name, G_STR_DELIMITERS ":^", '_'); /* FIXME do character checks like for types */ sl@0: sl@0: SIGNAL_LOCK (); sl@0: sl@0: signal_id = signal_id_lookup (g_quark_try_string (name), itype); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (node && !node->destroyed) sl@0: { sl@0: g_warning (G_STRLOC ": signal \"%s\" already exists in the `%s' %s", sl@0: name, sl@0: type_debug_name (node->itype), sl@0: G_TYPE_IS_INTERFACE (node->itype) ? "interface" : "class ancestry"); sl@0: g_free (name); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: if (node && node->itype != itype) sl@0: { sl@0: g_warning (G_STRLOC ": signal \"%s\" for type `%s' was previously created for type `%s'", sl@0: name, sl@0: type_debug_name (itype), sl@0: type_debug_name (node->itype)); sl@0: g_free (name); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: for (i = 0; i < n_params; i++) sl@0: if (!G_TYPE_IS_VALUE (param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE)) sl@0: { sl@0: g_warning (G_STRLOC ": parameter %d of type `%s' for signal \"%s::%s\" is not a value type", sl@0: i + 1, type_debug_name (param_types[i]), type_debug_name (itype), name); sl@0: g_free (name); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: if (return_type != G_TYPE_NONE && !G_TYPE_IS_VALUE (return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE)) sl@0: { sl@0: g_warning (G_STRLOC ": return value of type `%s' for signal \"%s::%s\" is not a value type", sl@0: type_debug_name (return_type), type_debug_name (itype), name); sl@0: g_free (name); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: if (return_type != G_TYPE_NONE && sl@0: (signal_flags & (G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP)) == G_SIGNAL_RUN_FIRST) sl@0: { sl@0: g_warning (G_STRLOC ": signal \"%s::%s\" has return type `%s' and is only G_SIGNAL_RUN_FIRST", sl@0: type_debug_name (itype), name, type_debug_name (return_type)); sl@0: g_free (name); sl@0: SIGNAL_UNLOCK (); sl@0: return 0; sl@0: } sl@0: sl@0: /* setup permanent portion of signal node */ sl@0: if (!node) sl@0: { sl@0: SignalKey key; sl@0: sl@0: signal_id = g_n_signal_nodes++; sl@0: node = g_new (SignalNode, 1); sl@0: node->signal_id = signal_id; sl@0: g_signal_nodes = g_renew (SignalNode*, g_signal_nodes, g_n_signal_nodes); sl@0: g_signal_nodes[signal_id] = node; sl@0: node->itype = itype; sl@0: node->name = name; sl@0: key.itype = itype; sl@0: key.quark = g_quark_from_string (node->name); sl@0: key.signal_id = signal_id; sl@0: g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key); sl@0: g_strdelimit (name, "_", '-'); sl@0: node->name = g_intern_string (name); sl@0: key.quark = g_quark_from_string (name); sl@0: g_signal_key_bsa = g_bsearch_array_insert (g_signal_key_bsa, &g_signal_key_bconfig, &key); sl@0: } sl@0: node->destroyed = FALSE; sl@0: node->test_class_offset = 0; sl@0: sl@0: /* setup reinitializable portion */ sl@0: node->flags = signal_flags & G_SIGNAL_FLAGS_MASK; sl@0: node->n_params = n_params; sl@0: node->param_types = g_memdup (param_types, sizeof (GType) * n_params); sl@0: node->return_type = return_type; sl@0: node->class_closure_bsa = NULL; sl@0: if (accumulator) sl@0: { sl@0: node->accumulator = g_new (SignalAccumulator, 1); sl@0: node->accumulator->func = accumulator; sl@0: node->accumulator->data = accu_data; sl@0: } sl@0: else sl@0: node->accumulator = NULL; sl@0: node->c_marshaller = c_marshaller; sl@0: node->emission_hooks = NULL; sl@0: if (class_closure) sl@0: signal_add_class_closure (node, 0, class_closure); sl@0: else if (G_TYPE_IS_INSTANTIATABLE (itype) && return_type == G_TYPE_NONE) sl@0: { sl@0: /* optimize NOP emissions */ sl@0: node->test_class_offset = TEST_CLASS_MAGIC; sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: g_free (name); sl@0: sl@0: return signal_id; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_new_valist: sl@0: * @signal_name: the name for the signal sl@0: * @itype: the type this signal pertains to. It will also pertain to sl@0: * types which are derived from this type. sl@0: * @signal_flags: a combination of #GSignalFlags specifying detail of when sl@0: * the default handler is to be invoked. You should at least specify sl@0: * %G_SIGNAL_RUN_FIRST or %G_SIGNAL_RUN_LAST. sl@0: * @class_closure: The closure to invoke on signal emission; may be %NULL. sl@0: * @accumulator: the accumulator for this signal; may be %NULL. sl@0: * @accu_data: user data for the @accumulator. sl@0: * @c_marshaller: the function to translate arrays of parameter values to sl@0: * signal emissions into C language callback invocations. sl@0: * @return_type: the type of return value, or #G_TYPE_NONE for a signal sl@0: * without a return value. sl@0: * @n_params: the number of parameter types in @args. sl@0: * @args: va_list of #GType, one for each parameter. sl@0: * sl@0: * Creates a new signal. (This is usually done in the class initializer.) sl@0: * sl@0: * See g_signal_new() for details on allowed signal names. sl@0: * sl@0: * Returns: the signal id sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_new_valist (const gchar *signal_name, sl@0: GType itype, sl@0: GSignalFlags signal_flags, sl@0: GClosure *class_closure, sl@0: GSignalAccumulator accumulator, sl@0: gpointer accu_data, sl@0: GSignalCMarshaller c_marshaller, sl@0: GType return_type, sl@0: guint n_params, sl@0: va_list args) sl@0: { sl@0: GType *param_types; sl@0: guint i; sl@0: guint signal_id; sl@0: sl@0: if (n_params > 0) sl@0: { sl@0: param_types = g_new (GType, n_params); sl@0: sl@0: for (i = 0; i < n_params; i++) sl@0: param_types[i] = va_arg (args, GType); sl@0: } sl@0: else sl@0: param_types = NULL; sl@0: sl@0: signal_id = g_signal_newv (signal_name, itype, signal_flags, sl@0: class_closure, accumulator, accu_data, c_marshaller, sl@0: return_type, n_params, param_types); sl@0: g_free (param_types); sl@0: sl@0: return signal_id; sl@0: } sl@0: sl@0: static void sl@0: signal_destroy_R (SignalNode *signal_node) sl@0: { sl@0: SignalNode node = *signal_node; sl@0: sl@0: signal_node->destroyed = TRUE; sl@0: sl@0: /* reentrancy caution, zero out real contents first */ sl@0: signal_node->test_class_offset = 0; sl@0: signal_node->n_params = 0; sl@0: signal_node->param_types = NULL; sl@0: signal_node->return_type = 0; sl@0: signal_node->class_closure_bsa = NULL; sl@0: signal_node->accumulator = NULL; sl@0: signal_node->c_marshaller = NULL; sl@0: signal_node->emission_hooks = NULL; sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: /* check current emissions */ sl@0: { sl@0: Emission *emission; sl@0: sl@0: for (emission = (node.flags & G_SIGNAL_NO_RECURSE) ? g_restart_emissions : g_recursive_emissions; sl@0: emission; emission = emission->next) sl@0: if (emission->ihint.signal_id == node.signal_id) sl@0: g_critical (G_STRLOC ": signal \"%s\" being destroyed is currently in emission (instance `%p')", sl@0: node.name, emission->instance); sl@0: } sl@0: #endif sl@0: sl@0: /* free contents that need to sl@0: */ sl@0: SIGNAL_UNLOCK (); sl@0: g_free (node.param_types); sl@0: if (node.class_closure_bsa) sl@0: { sl@0: guint i; sl@0: sl@0: for (i = 0; i < node.class_closure_bsa->n_nodes; i++) sl@0: { sl@0: ClassClosure *cc = g_bsearch_array_get_nth (node.class_closure_bsa, &g_class_closure_bconfig, i); sl@0: sl@0: g_closure_unref (cc->closure); sl@0: } sl@0: g_bsearch_array_free (node.class_closure_bsa, &g_class_closure_bconfig); sl@0: } sl@0: g_free (node.accumulator); sl@0: if (node.emission_hooks) sl@0: { sl@0: g_hook_list_clear (node.emission_hooks); sl@0: g_free (node.emission_hooks); sl@0: } sl@0: SIGNAL_LOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_override_class_closure: sl@0: * @signal_id: the signal id sl@0: * @instance_type: the instance type on which to override the class closure sl@0: * for the signal. sl@0: * @class_closure: the closure. sl@0: * sl@0: * Overrides the class closure (i.e. the default handler) for the given signal sl@0: * for emissions on instances of @instance_type. @instance_type must be derived sl@0: * from the type to which the signal belongs. sl@0: * sl@0: * See g_signal_chain_from_overridden() and sl@0: * g_signal_chain_from_overridden_handler() for how to chain up to the sl@0: * parent class closure from inside the overridden one. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_override_class_closure (guint signal_id, sl@0: GType instance_type, sl@0: GClosure *class_closure) sl@0: { sl@0: SignalNode *node; sl@0: sl@0: g_return_if_fail (signal_id > 0); sl@0: g_return_if_fail (class_closure != NULL); sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (!g_type_is_a (instance_type, node->itype)) sl@0: g_warning ("%s: type `%s' cannot be overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id); sl@0: else sl@0: { sl@0: ClassClosure *cc = signal_find_class_closure (node, instance_type); sl@0: sl@0: if (cc && cc->instance_type == instance_type) sl@0: g_warning ("%s: type `%s' is already overridden for signal id `%u'", G_STRLOC, type_debug_name (instance_type), signal_id); sl@0: else sl@0: signal_add_class_closure (node, instance_type, class_closure); sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_override_class_handler: sl@0: * @signal_name: the name for the signal sl@0: * @instance_type: the instance type on which to override the class handler sl@0: * for the signal. sl@0: * @class_handler: the handler. sl@0: * sl@0: * Overrides the class closure (i.e. the default handler) for the sl@0: * given signal for emissions on instances of @instance_type with sl@0: * callabck @class_handler. @instance_type must be derived from the sl@0: * type to which the signal belongs. sl@0: * sl@0: * See g_signal_chain_from_overridden() and sl@0: * g_signal_chain_from_overridden_handler() for how to chain up to the sl@0: * parent class closure from inside the overridden one. sl@0: * sl@0: * Since: 2.18 sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_override_class_handler (const gchar *signal_name, sl@0: GType instance_type, sl@0: GCallback class_handler) sl@0: { sl@0: guint signal_id; sl@0: sl@0: g_return_if_fail (signal_name != NULL); sl@0: g_return_if_fail (instance_type != G_TYPE_NONE); sl@0: g_return_if_fail (class_handler != NULL); sl@0: sl@0: signal_id = g_signal_lookup (signal_name, instance_type); sl@0: sl@0: if (signal_id) sl@0: g_signal_override_class_closure (signal_id, instance_type, sl@0: g_cclosure_new (class_handler, NULL, NULL)); sl@0: else sl@0: g_warning ("%s: signal name '%s' is invalid for type id '%"G_GSIZE_FORMAT"'", sl@0: G_STRLOC, signal_name, instance_type); sl@0: sl@0: } sl@0: sl@0: /** sl@0: * g_signal_chain_from_overridden: sl@0: * @instance_and_params: the argument list of the signal emission. The first sl@0: * element in the array is a #GValue for the instance the signal is being sl@0: * emitted on. The rest are any arguments to be passed to the signal. sl@0: * @return_value: Location for the return value. sl@0: * sl@0: * Calls the original class closure of a signal. This function should only sl@0: * be called from an overridden class closure; see sl@0: * g_signal_override_class_closure() and sl@0: * g_signal_override_class_handler(). sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_chain_from_overridden (const GValue *instance_and_params, sl@0: GValue *return_value) sl@0: { sl@0: GType chain_type = 0, restore_type = 0; sl@0: Emission *emission = NULL; sl@0: GClosure *closure = NULL; sl@0: guint n_params = 0; sl@0: gpointer instance; sl@0: sl@0: g_return_if_fail (instance_and_params != NULL); sl@0: instance = g_value_peek_pointer (instance_and_params); sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: sl@0: SIGNAL_LOCK (); sl@0: emission = emission_find_innermost (instance); sl@0: if (emission) sl@0: { sl@0: SignalNode *node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id); sl@0: sl@0: g_assert (node != NULL); /* paranoid */ sl@0: sl@0: /* we should probably do the same parameter checks as g_signal_emit() here. sl@0: */ sl@0: if (emission->chain_type != G_TYPE_NONE) sl@0: { sl@0: ClassClosure *cc = signal_find_class_closure (node, emission->chain_type); sl@0: sl@0: g_assert (cc != NULL); /* closure currently in call stack */ sl@0: sl@0: n_params = node->n_params; sl@0: restore_type = cc->instance_type; sl@0: cc = signal_find_class_closure (node, g_type_parent (cc->instance_type)); sl@0: if (cc && cc->instance_type != restore_type) sl@0: { sl@0: closure = cc->closure; sl@0: chain_type = cc->instance_type; sl@0: } sl@0: } sl@0: else sl@0: g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance); sl@0: } sl@0: else sl@0: g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance); sl@0: sl@0: if (closure) sl@0: { sl@0: emission->chain_type = chain_type; sl@0: SIGNAL_UNLOCK (); sl@0: g_closure_invoke (closure, sl@0: return_value, sl@0: n_params + 1, sl@0: instance_and_params, sl@0: &emission->ihint); sl@0: SIGNAL_LOCK (); sl@0: emission->chain_type = restore_type; sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_chain_from_overridden_handler: sl@0: * @instance: the instance the signal is being emitted on. sl@0: * @...: parameters to be passed to the parent class closure, followed by a sl@0: * location for the return value. If the return type of the signal sl@0: * is #G_TYPE_NONE, the return value location can be omitted. sl@0: * sl@0: * Calls the original class closure of a signal. This function should sl@0: * only be called from an overridden class closure; see sl@0: * g_signal_override_class_closure() and sl@0: * g_signal_override_class_handler(). sl@0: * sl@0: * Since: 2.18 sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_chain_from_overridden_handler (gpointer instance, sl@0: ...) sl@0: { sl@0: GType chain_type = 0, restore_type = 0; sl@0: Emission *emission = NULL; sl@0: GClosure *closure = NULL; sl@0: SignalNode *node; sl@0: guint n_params = 0; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: sl@0: SIGNAL_LOCK (); sl@0: emission = emission_find_innermost (instance); sl@0: if (emission) sl@0: { sl@0: node = LOOKUP_SIGNAL_NODE (emission->ihint.signal_id); sl@0: sl@0: g_assert (node != NULL); /* paranoid */ sl@0: sl@0: /* we should probably do the same parameter checks as g_signal_emit() here. sl@0: */ sl@0: if (emission->chain_type != G_TYPE_NONE) sl@0: { sl@0: ClassClosure *cc = signal_find_class_closure (node, emission->chain_type); sl@0: sl@0: g_assert (cc != NULL); /* closure currently in call stack */ sl@0: sl@0: n_params = node->n_params; sl@0: restore_type = cc->instance_type; sl@0: cc = signal_find_class_closure (node, g_type_parent (cc->instance_type)); sl@0: if (cc && cc->instance_type != restore_type) sl@0: { sl@0: closure = cc->closure; sl@0: chain_type = cc->instance_type; sl@0: } sl@0: } sl@0: else sl@0: g_warning ("%s: signal id `%u' cannot be chained from current emission stage for instance `%p'", G_STRLOC, node->signal_id, instance); sl@0: } sl@0: else sl@0: g_warning ("%s: no signal is currently being emitted for instance `%p'", G_STRLOC, instance); sl@0: sl@0: if (closure) sl@0: { sl@0: GValue *instance_and_params; sl@0: GType signal_return_type; sl@0: GValue *param_values; sl@0: va_list var_args; sl@0: guint i; sl@0: sl@0: va_start (var_args, instance); sl@0: sl@0: signal_return_type = node->return_type; sl@0: instance_and_params = g_slice_alloc (sizeof (GValue) * (n_params + 1)); sl@0: param_values = instance_and_params + 1; sl@0: sl@0: for (i = 0; i < node->n_params; i++) sl@0: { sl@0: gchar *error; sl@0: GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: sl@0: param_values[i].g_type = 0; sl@0: SIGNAL_UNLOCK (); sl@0: g_value_init (param_values + i, ptype); sl@0: G_VALUE_COLLECT (param_values + i, sl@0: var_args, sl@0: static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, sl@0: &error); sl@0: if (error) sl@0: { sl@0: g_warning ("%s: %s", G_STRLOC, error); sl@0: g_free (error); sl@0: sl@0: /* we purposely leak the value here, it might not be sl@0: * in a sane state if an error condition occoured sl@0: */ sl@0: while (i--) sl@0: g_value_unset (param_values + i); sl@0: sl@0: g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params); sl@0: va_end (var_args); sl@0: return; sl@0: } sl@0: SIGNAL_LOCK (); sl@0: } sl@0: sl@0: SIGNAL_UNLOCK (); sl@0: instance_and_params->g_type = 0; sl@0: g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance)); sl@0: g_value_set_instance (instance_and_params, instance); sl@0: SIGNAL_LOCK (); sl@0: sl@0: emission->chain_type = chain_type; sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: if (signal_return_type == G_TYPE_NONE) sl@0: { sl@0: g_closure_invoke (closure, sl@0: NULL, sl@0: n_params + 1, sl@0: instance_and_params, sl@0: &emission->ihint); sl@0: } sl@0: else sl@0: { sl@0: GValue return_value = { 0, }; sl@0: gchar *error = NULL; sl@0: GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: sl@0: g_value_init (&return_value, rtype); sl@0: sl@0: g_closure_invoke (closure, sl@0: &return_value, sl@0: n_params + 1, sl@0: instance_and_params, sl@0: &emission->ihint); sl@0: sl@0: G_VALUE_LCOPY (&return_value, sl@0: var_args, sl@0: static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, sl@0: &error); sl@0: if (!error) sl@0: { sl@0: g_value_unset (&return_value); sl@0: } sl@0: else sl@0: { sl@0: g_warning ("%s: %s", G_STRLOC, error); sl@0: g_free (error); sl@0: sl@0: /* we purposely leak the value here, it might not be sl@0: * in a sane state if an error condition occured sl@0: */ sl@0: } sl@0: } sl@0: sl@0: for (i = 0; i < n_params; i++) sl@0: g_value_unset (param_values + i); sl@0: g_value_unset (instance_and_params); sl@0: g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params); sl@0: sl@0: va_end (var_args); sl@0: sl@0: SIGNAL_LOCK (); sl@0: emission->chain_type = restore_type; sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_get_invocation_hint: sl@0: * @instance: the instance to query sl@0: * sl@0: * Returns the invocation hint of the innermost signal emission of instance. sl@0: * sl@0: * Returns: the invocation hint of the innermost signal emission. sl@0: */ sl@0: EXPORT_C GSignalInvocationHint* sl@0: g_signal_get_invocation_hint (gpointer instance) sl@0: { sl@0: Emission *emission = NULL; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), NULL); sl@0: sl@0: SIGNAL_LOCK (); sl@0: emission = emission_find_innermost (instance); sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return emission ? &emission->ihint : NULL; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_connect_closure_by_id: sl@0: * @instance: the instance to connect to. sl@0: * @signal_id: the id of the signal. sl@0: * @detail: the detail. sl@0: * @closure: the closure to connect. sl@0: * @after: whether the handler should be called before or after the sl@0: * default handler of the signal. sl@0: * sl@0: * Connects a closure to a signal for a particular object. sl@0: * sl@0: * Returns: the handler id sl@0: */ sl@0: EXPORT_C gulong sl@0: g_signal_connect_closure_by_id (gpointer instance, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gboolean after) sl@0: { sl@0: SignalNode *node; sl@0: gulong handler_seq_no = 0; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail (signal_id > 0, 0); sl@0: g_return_val_if_fail (closure != NULL, 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (node) sl@0: { sl@0: if (detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail); sl@0: else if (!g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) sl@0: g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance); sl@0: else sl@0: { sl@0: Handler *handler = handler_new (after); sl@0: sl@0: handler_seq_no = handler->sequential_number; sl@0: handler->detail = detail; sl@0: handler->closure = g_closure_ref (closure); sl@0: g_closure_sink (closure); sl@0: handler_insert (signal_id, instance, handler); sl@0: if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (closure)) sl@0: g_closure_set_marshal (closure, node->c_marshaller); sl@0: } sl@0: } sl@0: else sl@0: g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance); sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return handler_seq_no; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_connect_closure: sl@0: * @instance: the instance to connect to. sl@0: * @detailed_signal: a string of the form "signal-name::detail". sl@0: * @closure: the closure to connect. sl@0: * @after: whether the handler should be called before or after the sl@0: * default handler of the signal. sl@0: * sl@0: * Connects a closure to a signal for a particular object. sl@0: * sl@0: * Returns: the handler id sl@0: */ sl@0: EXPORT_C gulong sl@0: g_signal_connect_closure (gpointer instance, sl@0: const gchar *detailed_signal, sl@0: GClosure *closure, sl@0: gboolean after) sl@0: { sl@0: guint signal_id; sl@0: gulong handler_seq_no = 0; sl@0: GQuark detail = 0; sl@0: GType itype; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail (detailed_signal != NULL, 0); sl@0: g_return_val_if_fail (closure != NULL, 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: itype = G_TYPE_FROM_INSTANCE (instance); sl@0: signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE); sl@0: if (signal_id) sl@0: { sl@0: SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: sl@0: if (detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal); sl@0: else if (!g_type_is_a (itype, node->itype)) sl@0: g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance); sl@0: else sl@0: { sl@0: Handler *handler = handler_new (after); sl@0: sl@0: handler_seq_no = handler->sequential_number; sl@0: handler->detail = detail; sl@0: handler->closure = g_closure_ref (closure); sl@0: g_closure_sink (closure); sl@0: handler_insert (signal_id, instance, handler); sl@0: if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure)) sl@0: g_closure_set_marshal (handler->closure, node->c_marshaller); sl@0: } sl@0: } sl@0: else sl@0: g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance); sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return handler_seq_no; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_connect_data: sl@0: * @instance: the instance to connect to. sl@0: * @detailed_signal: a string of the form "signal-name::detail". sl@0: * @c_handler: the #GCallback to connect. sl@0: * @data: data to pass to @c_handler calls. sl@0: * @destroy_data: a #GClosureNotify for @data. sl@0: * @connect_flags: a combination of #GConnectFlags. sl@0: * sl@0: * Connects a #GCallback function to a signal for a particular object. Similar sl@0: * to g_signal_connect(), but allows to provide a #GClosureNotify for the data sl@0: * which will be called when the signal handler is disconnected and no longer sl@0: * used. Specify @connect_flags if you need ..._after() or sl@0: * ..._swapped() variants of this function. sl@0: * sl@0: * Returns: the handler id sl@0: */ sl@0: EXPORT_C gulong sl@0: g_signal_connect_data (gpointer instance, sl@0: const gchar *detailed_signal, sl@0: GCallback c_handler, sl@0: gpointer data, sl@0: GClosureNotify destroy_data, sl@0: GConnectFlags connect_flags) sl@0: { sl@0: guint signal_id; sl@0: gulong handler_seq_no = 0; sl@0: GQuark detail = 0; sl@0: GType itype; sl@0: gboolean swapped, after; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail (detailed_signal != NULL, 0); sl@0: g_return_val_if_fail (c_handler != NULL, 0); sl@0: sl@0: swapped = (connect_flags & G_CONNECT_SWAPPED) != FALSE; sl@0: after = (connect_flags & G_CONNECT_AFTER) != FALSE; sl@0: sl@0: SIGNAL_LOCK (); sl@0: itype = G_TYPE_FROM_INSTANCE (instance); sl@0: signal_id = signal_parse_name (detailed_signal, itype, &detail, TRUE); sl@0: if (signal_id) sl@0: { sl@0: SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: sl@0: if (detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: g_warning ("%s: signal `%s' does not support details", G_STRLOC, detailed_signal); sl@0: else if (!g_type_is_a (itype, node->itype)) sl@0: g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance); sl@0: else sl@0: { sl@0: Handler *handler = handler_new (after); sl@0: sl@0: handler_seq_no = handler->sequential_number; sl@0: handler->detail = detail; sl@0: handler->closure = g_closure_ref ((swapped ? g_cclosure_new_swap : g_cclosure_new) (c_handler, data, destroy_data)); sl@0: g_closure_sink (handler->closure); sl@0: handler_insert (signal_id, instance, handler); sl@0: if (node->c_marshaller && G_CLOSURE_NEEDS_MARSHAL (handler->closure)) sl@0: g_closure_set_marshal (handler->closure, node->c_marshaller); sl@0: } sl@0: } sl@0: else sl@0: g_warning ("%s: signal `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance); sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return handler_seq_no; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handler_block: sl@0: * @instance: The instance to block the signal handler of. sl@0: * @handler_id: Handler id of the handler to be blocked. sl@0: * sl@0: * Blocks a handler of an instance so it will not be called during any sl@0: * signal emissions unless it is unblocked again. Thus "blocking" a sl@0: * signal handler means to temporarily deactive it, a signal handler sl@0: * has to be unblocked exactly the same amount of times it has been sl@0: * blocked before to become active again. sl@0: * sl@0: * The @handler_id has to be a valid signal handler id, connected to a sl@0: * signal of @instance. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_handler_block (gpointer instance, sl@0: gulong handler_id) sl@0: { sl@0: Handler *handler; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (handler_id > 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: handler = handler_lookup (instance, handler_id, NULL); sl@0: if (handler) sl@0: { sl@0: #ifndef G_DISABLE_CHECKS sl@0: if (handler->block_count >= HANDLER_MAX_BLOCK_COUNT - 1) sl@0: g_error (G_STRLOC ": handler block_count overflow, %s", REPORT_BUG); sl@0: #endif sl@0: handler->block_count += 1; sl@0: } sl@0: else sl@0: g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handler_unblock: sl@0: * @instance: The instance to unblock the signal handler of. sl@0: * @handler_id: Handler id of the handler to be unblocked. sl@0: * sl@0: * Undoes the effect of a previous g_signal_handler_block() call. A sl@0: * blocked handler is skipped during signal emissions and will not be sl@0: * invoked, unblocking it (for exactly the amount of times it has been sl@0: * blocked before) reverts its "blocked" state, so the handler will be sl@0: * recognized by the signal system and is called upon future or sl@0: * currently ongoing signal emissions (since the order in which sl@0: * handlers are called during signal emissions is deterministic, sl@0: * whether the unblocked handler in question is called as part of a sl@0: * currently ongoing emission depends on how far that emission has sl@0: * proceeded yet). sl@0: * sl@0: * The @handler_id has to be a valid id of a signal handler that is sl@0: * connected to a signal of @instance and is currently blocked. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_handler_unblock (gpointer instance, sl@0: gulong handler_id) sl@0: { sl@0: Handler *handler; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (handler_id > 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: handler = handler_lookup (instance, handler_id, NULL); sl@0: if (handler) sl@0: { sl@0: if (handler->block_count) sl@0: handler->block_count -= 1; sl@0: else sl@0: g_warning (G_STRLOC ": handler `%lu' of instance `%p' is not blocked", handler_id, instance); sl@0: } sl@0: else sl@0: g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handler_disconnect: sl@0: * @instance: The instance to remove the signal handler from. sl@0: * @handler_id: Handler id of the handler to be disconnected. sl@0: * sl@0: * Disconnects a handler from an instance so it will not be called during sl@0: * any future or currently ongoing emissions of the signal it has been sl@0: * connected to. The @handler_id becomes invalid and may be reused. sl@0: * sl@0: * The @handler_id has to be a valid signal handler id, connected to a sl@0: * signal of @instance. sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_handler_disconnect (gpointer instance, sl@0: gulong handler_id) sl@0: { sl@0: Handler *handler; sl@0: guint signal_id; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (handler_id > 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: handler = handler_lookup (instance, handler_id, &signal_id); sl@0: if (handler) sl@0: { sl@0: handler->sequential_number = 0; sl@0: handler->block_count = 1; sl@0: handler_unref_R (signal_id, instance, handler); sl@0: } sl@0: else sl@0: g_warning ("%s: instance `%p' has no handler with id `%lu'", G_STRLOC, instance, handler_id); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handler_is_connected: sl@0: * @instance: The instance where a signal handler is sought. sl@0: * @handler_id: the handler id. sl@0: * sl@0: * Returns whether @handler_id is the id of a handler connected to @instance. sl@0: * sl@0: * Returns: whether @handler_id identifies a handler connected to @instance. sl@0: */ sl@0: EXPORT_C gboolean sl@0: g_signal_handler_is_connected (gpointer instance, sl@0: gulong handler_id) sl@0: { sl@0: Handler *handler; sl@0: gboolean connected; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE); sl@0: sl@0: SIGNAL_LOCK (); sl@0: handler = handler_lookup (instance, handler_id, NULL); sl@0: connected = handler != NULL; sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return connected; sl@0: } sl@0: sl@0: EXPORT_C void sl@0: g_signal_handlers_destroy (gpointer instance) sl@0: { sl@0: GBSearchArray *hlbsa; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: sl@0: SIGNAL_LOCK (); sl@0: hlbsa = g_hash_table_lookup (g_handler_list_bsa_ht, instance); sl@0: if (hlbsa) sl@0: { sl@0: guint i; sl@0: sl@0: /* reentrancy caution, delete instance trace first */ sl@0: g_hash_table_remove (g_handler_list_bsa_ht, instance); sl@0: sl@0: for (i = 0; i < hlbsa->n_nodes; i++) sl@0: { sl@0: HandlerList *hlist = g_bsearch_array_get_nth (hlbsa, &g_signal_hlbsa_bconfig, i); sl@0: Handler *handler = hlist->handlers; sl@0: sl@0: while (handler) sl@0: { sl@0: Handler *tmp = handler; sl@0: sl@0: handler = tmp->next; sl@0: tmp->block_count = 1; sl@0: /* cruel unlink, this works because _all_ handlers vanish */ sl@0: tmp->next = NULL; sl@0: tmp->prev = tmp; sl@0: if (tmp->sequential_number) sl@0: { sl@0: tmp->sequential_number = 0; sl@0: handler_unref_R (0, NULL, tmp); sl@0: } sl@0: } sl@0: } sl@0: g_bsearch_array_free (hlbsa, &g_signal_hlbsa_bconfig); sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handler_find: sl@0: * @instance: The instance owning the signal handler to be found. sl@0: * @mask: Mask indicating which of @signal_id, @detail, @closure, @func sl@0: * and/or @data the handler has to match. sl@0: * @signal_id: Signal the handler has to be connected to. sl@0: * @detail: Signal detail the handler has to be connected to. sl@0: * @closure: The closure the handler will invoke. sl@0: * @func: The C closure callback of the handler (useless for non-C closures). sl@0: * @data: The closure data of the handler's closure. sl@0: * sl@0: * Finds the first signal handler that matches certain selection criteria. sl@0: * The criteria mask is passed as an OR-ed combination of #GSignalMatchType sl@0: * flags, and the criteria values are passed as arguments. sl@0: * The match @mask has to be non-0 for successful matches. sl@0: * If no handler was found, 0 is returned. sl@0: * sl@0: * Returns: A valid non-0 signal handler id for a successful match. sl@0: */ sl@0: EXPORT_C gulong sl@0: g_signal_handler_find (gpointer instance, sl@0: GSignalMatchType mask, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gpointer func, sl@0: gpointer data) sl@0: { sl@0: gulong handler_seq_no = 0; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); sl@0: sl@0: if (mask & G_SIGNAL_MATCH_MASK) sl@0: { sl@0: HandlerMatch *mlist; sl@0: sl@0: SIGNAL_LOCK (); sl@0: mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, TRUE); sl@0: if (mlist) sl@0: { sl@0: handler_seq_no = mlist->handler->sequential_number; sl@0: handler_match_free1_R (mlist, instance); sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: return handler_seq_no; sl@0: } sl@0: sl@0: static guint sl@0: signal_handlers_foreach_matched_R (gpointer instance, sl@0: GSignalMatchType mask, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gpointer func, sl@0: gpointer data, sl@0: void (*callback) (gpointer instance, sl@0: gulong handler_seq_no)) sl@0: { sl@0: HandlerMatch *mlist; sl@0: guint n_handlers = 0; sl@0: sl@0: mlist = handlers_find (instance, mask, signal_id, detail, closure, func, data, FALSE); sl@0: while (mlist) sl@0: { sl@0: n_handlers++; sl@0: if (mlist->handler->sequential_number) sl@0: { sl@0: SIGNAL_UNLOCK (); sl@0: callback (instance, mlist->handler->sequential_number); sl@0: SIGNAL_LOCK (); sl@0: } sl@0: mlist = handler_match_free1_R (mlist, instance); sl@0: } sl@0: sl@0: return n_handlers; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handlers_block_matched: sl@0: * @instance: The instance to block handlers from. sl@0: * @mask: Mask indicating which of @signal_id, @detail, @closure, @func sl@0: * and/or @data the handlers have to match. sl@0: * @signal_id: Signal the handlers have to be connected to. sl@0: * @detail: Signal detail the handlers have to be connected to. sl@0: * @closure: The closure the handlers will invoke. sl@0: * @func: The C closure callback of the handlers (useless for non-C closures). sl@0: * @data: The closure data of the handlers' closures. sl@0: * sl@0: * Blocks all handlers on an instance that match a certain selection criteria. sl@0: * The criteria mask is passed as an OR-ed combination of #GSignalMatchType sl@0: * flags, and the criteria values are passed as arguments. sl@0: * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC sl@0: * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches. sl@0: * If no handlers were found, 0 is returned, the number of blocked handlers sl@0: * otherwise. sl@0: * sl@0: * Returns: The number of handlers that matched. sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_handlers_block_matched (gpointer instance, sl@0: GSignalMatchType mask, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gpointer func, sl@0: gpointer data) sl@0: { sl@0: guint n_handlers = 0; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); sl@0: sl@0: if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA)) sl@0: { sl@0: SIGNAL_LOCK (); sl@0: n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail, sl@0: closure, func, data, sl@0: g_signal_handler_block); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: return n_handlers; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handlers_unblock_matched: sl@0: * @instance: The instance to unblock handlers from. sl@0: * @mask: Mask indicating which of @signal_id, @detail, @closure, @func sl@0: * and/or @data the handlers have to match. sl@0: * @signal_id: Signal the handlers have to be connected to. sl@0: * @detail: Signal detail the handlers have to be connected to. sl@0: * @closure: The closure the handlers will invoke. sl@0: * @func: The C closure callback of the handlers (useless for non-C closures). sl@0: * @data: The closure data of the handlers' closures. sl@0: * sl@0: * Unblocks all handlers on an instance that match a certain selection sl@0: * criteria. The criteria mask is passed as an OR-ed combination of sl@0: * #GSignalMatchType flags, and the criteria values are passed as arguments. sl@0: * Passing at least one of the %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC sl@0: * or %G_SIGNAL_MATCH_DATA match flags is required for successful matches. sl@0: * If no handlers were found, 0 is returned, the number of unblocked handlers sl@0: * otherwise. The match criteria should not apply to any handlers that are sl@0: * not currently blocked. sl@0: * sl@0: * Returns: The number of handlers that matched. sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_handlers_unblock_matched (gpointer instance, sl@0: GSignalMatchType mask, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gpointer func, sl@0: gpointer data) sl@0: { sl@0: guint n_handlers = 0; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); sl@0: sl@0: if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA)) sl@0: { sl@0: SIGNAL_LOCK (); sl@0: n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail, sl@0: closure, func, data, sl@0: g_signal_handler_unblock); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: return n_handlers; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_handlers_disconnect_matched: sl@0: * @instance: The instance to remove handlers from. sl@0: * @mask: Mask indicating which of @signal_id, @detail, @closure, @func sl@0: * and/or @data the handlers have to match. sl@0: * @signal_id: Signal the handlers have to be connected to. sl@0: * @detail: Signal detail the handlers have to be connected to. sl@0: * @closure: The closure the handlers will invoke. sl@0: * @func: The C closure callback of the handlers (useless for non-C closures). sl@0: * @data: The closure data of the handlers' closures. sl@0: * sl@0: * Disconnects all handlers on an instance that match a certain sl@0: * selection criteria. The criteria mask is passed as an OR-ed sl@0: * combination of #GSignalMatchType flags, and the criteria values are sl@0: * passed as arguments. Passing at least one of the sl@0: * %G_SIGNAL_MATCH_CLOSURE, %G_SIGNAL_MATCH_FUNC or sl@0: * %G_SIGNAL_MATCH_DATA match flags is required for successful sl@0: * matches. If no handlers were found, 0 is returned, the number of sl@0: * disconnected handlers otherwise. sl@0: * sl@0: * Returns: The number of handlers that matched. sl@0: */ sl@0: EXPORT_C guint sl@0: g_signal_handlers_disconnect_matched (gpointer instance, sl@0: GSignalMatchType mask, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GClosure *closure, sl@0: gpointer func, sl@0: gpointer data) sl@0: { sl@0: guint n_handlers = 0; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), 0); sl@0: g_return_val_if_fail ((mask & ~G_SIGNAL_MATCH_MASK) == 0, 0); sl@0: sl@0: if (mask & (G_SIGNAL_MATCH_CLOSURE | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA)) sl@0: { sl@0: SIGNAL_LOCK (); sl@0: n_handlers = signal_handlers_foreach_matched_R (instance, mask, signal_id, detail, sl@0: closure, func, data, sl@0: g_signal_handler_disconnect); sl@0: SIGNAL_UNLOCK (); sl@0: } sl@0: sl@0: return n_handlers; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_has_handler_pending: sl@0: * @instance: the object whose signal handlers are sought. sl@0: * @signal_id: the signal id. sl@0: * @detail: the detail. sl@0: * @may_be_blocked: whether blocked handlers should count as match. sl@0: * sl@0: * Returns whether there are any handlers connected to @instance for the sl@0: * given signal id and detail. sl@0: * sl@0: * One example of when you might use this is when the arguments to the sl@0: * signal are difficult to compute. A class implementor may opt to not sl@0: * emit the signal if no one is attached anyway, thus saving the cost sl@0: * of building the arguments. sl@0: * sl@0: * Returns: %TRUE if a handler is connected to the signal, %FALSE sl@0: * otherwise. sl@0: */ sl@0: EXPORT_C gboolean sl@0: g_signal_has_handler_pending (gpointer instance, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: gboolean may_be_blocked) sl@0: { sl@0: HandlerMatch *mlist; sl@0: gboolean has_pending; sl@0: sl@0: g_return_val_if_fail (G_TYPE_CHECK_INSTANCE (instance), FALSE); sl@0: g_return_val_if_fail (signal_id > 0, FALSE); sl@0: sl@0: SIGNAL_LOCK (); sl@0: if (detail) sl@0: { sl@0: SignalNode *node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: sl@0: if (!(node->flags & G_SIGNAL_DETAILED)) sl@0: { sl@0: g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail); sl@0: SIGNAL_UNLOCK (); sl@0: return FALSE; sl@0: } sl@0: } sl@0: mlist = handlers_find (instance, sl@0: (G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_DETAIL | (may_be_blocked ? 0 : G_SIGNAL_MATCH_UNBLOCKED)), sl@0: signal_id, detail, NULL, NULL, NULL, TRUE); sl@0: if (mlist) sl@0: { sl@0: has_pending = TRUE; sl@0: handler_match_free1_R (mlist, instance); sl@0: } sl@0: else sl@0: has_pending = FALSE; sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: return has_pending; sl@0: } sl@0: sl@0: static inline gboolean sl@0: signal_check_skip_emission (SignalNode *node, sl@0: gpointer instance, sl@0: GQuark detail) sl@0: { sl@0: HandlerList *hlist; sl@0: sl@0: /* are we able to check for NULL class handlers? */ sl@0: if (!node->test_class_offset) sl@0: return FALSE; sl@0: sl@0: /* are there emission hooks pending? */ sl@0: if (node->emission_hooks && node->emission_hooks->hooks) sl@0: return FALSE; sl@0: sl@0: /* is there a non-NULL class handler? */ sl@0: if (node->test_class_offset != TEST_CLASS_MAGIC) sl@0: { sl@0: GTypeClass *class = G_TYPE_INSTANCE_GET_CLASS (instance, G_TYPE_FROM_INSTANCE (instance), GTypeClass); sl@0: sl@0: if (G_STRUCT_MEMBER (gpointer, class, node->test_class_offset)) sl@0: return FALSE; sl@0: } sl@0: sl@0: /* are signals being debugged? */ sl@0: #ifdef G_ENABLE_DEBUG sl@0: IF_DEBUG (SIGNALS, g_trace_instance_signals || g_trap_instance_signals) sl@0: return FALSE; sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: sl@0: /* is this a no-recurse signal already in emission? */ sl@0: if (node->flags & G_SIGNAL_NO_RECURSE && sl@0: emission_find (g_restart_emissions, node->signal_id, detail, instance)) sl@0: return FALSE; sl@0: sl@0: /* do we have pending handlers? */ sl@0: hlist = handler_list_lookup (node->signal_id, instance); sl@0: if (hlist && hlist->handlers) sl@0: return FALSE; sl@0: sl@0: /* none of the above, no emission required */ sl@0: return TRUE; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_emitv: sl@0: * @instance_and_params: argument list for the signal emission. The first sl@0: * element in the array is a #GValue for the instance the signal is sl@0: * being emitted on. The rest are any arguments to be passed to the sl@0: * signal. sl@0: * @signal_id: the signal id sl@0: * @detail: the detail sl@0: * @return_value: Location to store the return value of the signal emission. sl@0: * sl@0: * Emits a signal. sl@0: * sl@0: * Note that g_signal_emitv() doesn't change @return_value if no handlers are sl@0: * connected, in contrast to g_signal_emit() and g_signal_emit_valist(). sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_emitv (const GValue *instance_and_params, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: GValue *return_value) sl@0: { sl@0: gpointer instance; sl@0: SignalNode *node; sl@0: #ifdef G_ENABLE_DEBUG sl@0: const GValue *param_values; sl@0: guint i; sl@0: #endif sl@0: sl@0: g_return_if_fail (instance_and_params != NULL); sl@0: instance = g_value_peek_pointer (instance_and_params); sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (signal_id > 0); sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: param_values = instance_and_params + 1; sl@0: #endif sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) sl@0: { sl@0: g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: #ifdef G_ENABLE_DEBUG sl@0: if (detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: { sl@0: g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: for (i = 0; i < node->n_params; i++) sl@0: if (!G_TYPE_CHECK_VALUE_TYPE (param_values + i, node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE)) sl@0: { sl@0: g_critical ("%s: value for `%s' parameter %u for signal \"%s\" is of type `%s'", sl@0: G_STRLOC, sl@0: type_debug_name (node->param_types[i]), sl@0: i, sl@0: node->name, sl@0: G_VALUE_TYPE_NAME (param_values + i)); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: if (node->return_type != G_TYPE_NONE) sl@0: { sl@0: if (!return_value) sl@0: { sl@0: g_critical ("%s: return value `%s' for signal \"%s\" is (NULL)", sl@0: G_STRLOC, sl@0: type_debug_name (node->return_type), sl@0: node->name); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: else if (!node->accumulator && !G_TYPE_CHECK_VALUE_TYPE (return_value, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE)) sl@0: { sl@0: g_critical ("%s: return value `%s' for signal \"%s\" is of type `%s'", sl@0: G_STRLOC, sl@0: type_debug_name (node->return_type), sl@0: node->name, sl@0: G_VALUE_TYPE_NAME (return_value)); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: } sl@0: else sl@0: return_value = NULL; sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: sl@0: /* optimize NOP emissions */ sl@0: if (signal_check_skip_emission (node, instance, detail)) sl@0: { sl@0: /* nothing to do to emit this signal */ sl@0: SIGNAL_UNLOCK (); sl@0: /* g_printerr ("omitting emission of \"%s\"\n", node->name); */ sl@0: return; sl@0: } sl@0: sl@0: SIGNAL_UNLOCK (); sl@0: signal_emit_unlocked_R (node, detail, instance, return_value, instance_and_params); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_emit_valist: sl@0: * @instance: the instance the signal is being emitted on. sl@0: * @signal_id: the signal id sl@0: * @detail: the detail sl@0: * @var_args: a list of parameters to be passed to the signal, followed by a sl@0: * location for the return value. If the return type of the signal sl@0: * is #G_TYPE_NONE, the return value location can be omitted. sl@0: * sl@0: * Emits a signal. sl@0: * sl@0: * Note that g_signal_emit_valist() resets the return value to the default sl@0: * if no handlers are connected, in contrast to g_signal_emitv(). sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_emit_valist (gpointer instance, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: va_list var_args) sl@0: { sl@0: GValue *instance_and_params; sl@0: GType signal_return_type; sl@0: GValue *param_values; sl@0: SignalNode *node; sl@0: guint i, n_params; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (signal_id > 0); sl@0: sl@0: SIGNAL_LOCK (); sl@0: node = LOOKUP_SIGNAL_NODE (signal_id); sl@0: if (!node || !g_type_is_a (G_TYPE_FROM_INSTANCE (instance), node->itype)) sl@0: { sl@0: g_warning ("%s: signal id `%u' is invalid for instance `%p'", G_STRLOC, signal_id, instance); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: #ifndef G_DISABLE_CHECKS sl@0: if (detail && !(node->flags & G_SIGNAL_DETAILED)) sl@0: { sl@0: g_warning ("%s: signal id `%u' does not support detail (%u)", G_STRLOC, signal_id, detail); sl@0: SIGNAL_UNLOCK (); sl@0: return; sl@0: } sl@0: #endif /* !G_DISABLE_CHECKS */ sl@0: sl@0: /* optimize NOP emissions */ sl@0: if (signal_check_skip_emission (node, instance, detail)) sl@0: { sl@0: /* nothing to do to emit this signal */ sl@0: SIGNAL_UNLOCK (); sl@0: /* g_printerr ("omitting emission of \"%s\"\n", node->name); */ sl@0: return; sl@0: } sl@0: sl@0: n_params = node->n_params; sl@0: signal_return_type = node->return_type; sl@0: instance_and_params = g_slice_alloc (sizeof (GValue) * (n_params + 1)); sl@0: param_values = instance_and_params + 1; sl@0: sl@0: for (i = 0; i < node->n_params; i++) sl@0: { sl@0: gchar *error; sl@0: GType ptype = node->param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: gboolean static_scope = node->param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: sl@0: param_values[i].g_type = 0; sl@0: SIGNAL_UNLOCK (); sl@0: g_value_init (param_values + i, ptype); sl@0: G_VALUE_COLLECT (param_values + i, sl@0: var_args, sl@0: static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, sl@0: &error); sl@0: if (error) sl@0: { sl@0: g_warning ("%s: %s", G_STRLOC, error); sl@0: g_free (error); sl@0: sl@0: /* we purposely leak the value here, it might not be sl@0: * in a sane state if an error condition occoured sl@0: */ sl@0: while (i--) sl@0: g_value_unset (param_values + i); sl@0: sl@0: g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params); sl@0: return; sl@0: } sl@0: SIGNAL_LOCK (); sl@0: } sl@0: SIGNAL_UNLOCK (); sl@0: instance_and_params->g_type = 0; sl@0: g_value_init (instance_and_params, G_TYPE_FROM_INSTANCE (instance)); sl@0: g_value_set_instance (instance_and_params, instance); sl@0: if (signal_return_type == G_TYPE_NONE) sl@0: signal_emit_unlocked_R (node, detail, instance, NULL, instance_and_params); sl@0: else sl@0: { sl@0: GValue return_value = { 0, }; sl@0: gchar *error = NULL; sl@0: GType rtype = signal_return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: gboolean static_scope = signal_return_type & G_SIGNAL_TYPE_STATIC_SCOPE; sl@0: sl@0: g_value_init (&return_value, rtype); sl@0: sl@0: signal_emit_unlocked_R (node, detail, instance, &return_value, instance_and_params); sl@0: sl@0: G_VALUE_LCOPY (&return_value, sl@0: var_args, sl@0: static_scope ? G_VALUE_NOCOPY_CONTENTS : 0, sl@0: &error); sl@0: if (!error) sl@0: g_value_unset (&return_value); sl@0: else sl@0: { sl@0: g_warning ("%s: %s", G_STRLOC, error); sl@0: g_free (error); sl@0: sl@0: /* we purposely leak the value here, it might not be sl@0: * in a sane state if an error condition occured sl@0: */ sl@0: } sl@0: } sl@0: for (i = 0; i < n_params; i++) sl@0: g_value_unset (param_values + i); sl@0: g_value_unset (instance_and_params); sl@0: g_slice_free1 (sizeof (GValue) * (n_params + 1), instance_and_params); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_emit: sl@0: * @instance: the instance the signal is being emitted on. sl@0: * @signal_id: the signal id sl@0: * @detail: the detail sl@0: * @...: parameters to be passed to the signal, followed by a sl@0: * location for the return value. If the return type of the signal sl@0: * is #G_TYPE_NONE, the return value location can be omitted. sl@0: * sl@0: * Emits a signal. sl@0: * sl@0: * Note that g_signal_emit() resets the return value to the default sl@0: * if no handlers are connected, in contrast to g_signal_emitv(). sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_emit (gpointer instance, sl@0: guint signal_id, sl@0: GQuark detail, sl@0: ...) sl@0: { sl@0: va_list var_args; sl@0: sl@0: va_start (var_args, detail); sl@0: g_signal_emit_valist (instance, signal_id, detail, var_args); sl@0: va_end (var_args); sl@0: } sl@0: sl@0: /** sl@0: * g_signal_emit_by_name: sl@0: * @instance: the instance the signal is being emitted on. sl@0: * @detailed_signal: a string of the form "signal-name::detail". sl@0: * @...: parameters to be passed to the signal, followed by a sl@0: * location for the return value. If the return type of the signal sl@0: * is #G_TYPE_NONE, the return value location can be omitted. sl@0: * sl@0: * Emits a signal. sl@0: * sl@0: * Note that g_signal_emit_by_name() resets the return value to the default sl@0: * if no handlers are connected, in contrast to g_signal_emitv(). sl@0: */ sl@0: EXPORT_C void sl@0: g_signal_emit_by_name (gpointer instance, sl@0: const gchar *detailed_signal, sl@0: ...) sl@0: { sl@0: GQuark detail = 0; sl@0: guint signal_id; sl@0: sl@0: g_return_if_fail (G_TYPE_CHECK_INSTANCE (instance)); sl@0: g_return_if_fail (detailed_signal != NULL); sl@0: sl@0: SIGNAL_LOCK (); sl@0: signal_id = signal_parse_name (detailed_signal, G_TYPE_FROM_INSTANCE (instance), &detail, TRUE); sl@0: SIGNAL_UNLOCK (); sl@0: sl@0: if (signal_id) sl@0: { sl@0: va_list var_args; sl@0: sl@0: va_start (var_args, detailed_signal); sl@0: g_signal_emit_valist (instance, signal_id, detail, var_args); sl@0: va_end (var_args); sl@0: } sl@0: else sl@0: g_warning ("%s: signal name `%s' is invalid for instance `%p'", G_STRLOC, detailed_signal, instance); sl@0: } sl@0: sl@0: static inline gboolean sl@0: accumulate (GSignalInvocationHint *ihint, sl@0: GValue *return_accu, sl@0: GValue *handler_return, sl@0: SignalAccumulator *accumulator) sl@0: { sl@0: gboolean continue_emission; sl@0: sl@0: if (!accumulator) sl@0: return TRUE; sl@0: sl@0: continue_emission = accumulator->func (ihint, return_accu, handler_return, accumulator->data); sl@0: g_value_reset (handler_return); sl@0: sl@0: return continue_emission; sl@0: } sl@0: sl@0: static gboolean sl@0: signal_emit_unlocked_R (SignalNode *node, sl@0: GQuark detail, sl@0: gpointer instance, sl@0: GValue *emission_return, sl@0: const GValue *instance_and_params) sl@0: { sl@0: SignalAccumulator *accumulator; sl@0: Emission emission; sl@0: GClosure *class_closure; sl@0: HandlerList *hlist; sl@0: Handler *handler_list = NULL; sl@0: GValue *return_accu, accu = { 0, }; sl@0: guint signal_id; sl@0: gulong max_sequential_handler_number; sl@0: gboolean return_value_altered = FALSE; sl@0: sl@0: #ifdef G_ENABLE_DEBUG sl@0: IF_DEBUG (SIGNALS, g_trace_instance_signals == instance || g_trap_instance_signals == instance) sl@0: { sl@0: g_message ("%s::%s(%u) emitted (instance=%p, signal-node=%p)", sl@0: g_type_name (G_TYPE_FROM_INSTANCE (instance)), sl@0: node->name, detail, sl@0: instance, node); sl@0: if (g_trap_instance_signals == instance) sl@0: G_BREAKPOINT (); sl@0: } sl@0: #endif /* G_ENABLE_DEBUG */ sl@0: sl@0: SIGNAL_LOCK (); sl@0: signal_id = node->signal_id; sl@0: if (node->flags & G_SIGNAL_NO_RECURSE) sl@0: { sl@0: Emission *node = emission_find (g_restart_emissions, signal_id, detail, instance); sl@0: sl@0: if (node) sl@0: { sl@0: node->state = EMISSION_RESTART; sl@0: SIGNAL_UNLOCK (); sl@0: return return_value_altered; sl@0: } sl@0: } sl@0: accumulator = node->accumulator; sl@0: if (accumulator) sl@0: { sl@0: SIGNAL_UNLOCK (); sl@0: g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE); sl@0: return_accu = &accu; sl@0: SIGNAL_LOCK (); sl@0: } sl@0: else sl@0: return_accu = emission_return; sl@0: emission.instance = instance; sl@0: emission.ihint.signal_id = node->signal_id; sl@0: emission.ihint.detail = detail; sl@0: emission.ihint.run_type = 0; sl@0: emission.state = 0; sl@0: emission.chain_type = G_TYPE_NONE; sl@0: emission_push ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission); sl@0: class_closure = signal_lookup_closure (node, instance); sl@0: sl@0: EMIT_RESTART: sl@0: sl@0: if (handler_list) sl@0: handler_unref_R (signal_id, instance, handler_list); sl@0: max_sequential_handler_number = g_handler_sequential_number; sl@0: hlist = handler_list_lookup (signal_id, instance); sl@0: handler_list = hlist ? hlist->handlers : NULL; sl@0: if (handler_list) sl@0: handler_ref (handler_list); sl@0: sl@0: emission.ihint.run_type = G_SIGNAL_RUN_FIRST; sl@0: sl@0: if ((node->flags & G_SIGNAL_RUN_FIRST) && class_closure) sl@0: { sl@0: emission.state = EMISSION_RUN; sl@0: sl@0: emission.chain_type = G_TYPE_FROM_INSTANCE (instance); sl@0: SIGNAL_UNLOCK (); sl@0: g_closure_invoke (class_closure, sl@0: return_accu, sl@0: node->n_params + 1, sl@0: instance_and_params, sl@0: &emission.ihint); sl@0: if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && sl@0: emission.state == EMISSION_RUN) sl@0: emission.state = EMISSION_STOP; sl@0: SIGNAL_LOCK (); sl@0: emission.chain_type = G_TYPE_NONE; sl@0: return_value_altered = TRUE; sl@0: sl@0: if (emission.state == EMISSION_STOP) sl@0: goto EMIT_CLEANUP; sl@0: else if (emission.state == EMISSION_RESTART) sl@0: goto EMIT_RESTART; sl@0: } sl@0: sl@0: if (node->emission_hooks) sl@0: { sl@0: gboolean need_destroy, was_in_call, may_recurse = TRUE; sl@0: GHook *hook; sl@0: sl@0: emission.state = EMISSION_HOOK; sl@0: hook = g_hook_first_valid (node->emission_hooks, may_recurse); sl@0: while (hook) sl@0: { sl@0: SignalHook *signal_hook = SIGNAL_HOOK (hook); sl@0: sl@0: if (!signal_hook->detail || signal_hook->detail == detail) sl@0: { sl@0: GSignalEmissionHook hook_func = (GSignalEmissionHook) hook->func; sl@0: sl@0: was_in_call = G_HOOK_IN_CALL (hook); sl@0: hook->flags |= G_HOOK_FLAG_IN_CALL; sl@0: SIGNAL_UNLOCK (); sl@0: need_destroy = !hook_func (&emission.ihint, node->n_params + 1, instance_and_params, hook->data); sl@0: SIGNAL_LOCK (); sl@0: if (!was_in_call) sl@0: hook->flags &= ~G_HOOK_FLAG_IN_CALL; sl@0: if (need_destroy) sl@0: g_hook_destroy_link (node->emission_hooks, hook); sl@0: } sl@0: hook = g_hook_next_valid (node->emission_hooks, hook, may_recurse); sl@0: } sl@0: sl@0: if (emission.state == EMISSION_RESTART) sl@0: goto EMIT_RESTART; sl@0: } sl@0: sl@0: if (handler_list) sl@0: { sl@0: Handler *handler = handler_list; sl@0: sl@0: emission.state = EMISSION_RUN; sl@0: handler_ref (handler); sl@0: do sl@0: { sl@0: Handler *tmp; sl@0: sl@0: if (handler->after) sl@0: { sl@0: handler_unref_R (signal_id, instance, handler_list); sl@0: handler_list = handler; sl@0: break; sl@0: } sl@0: else if (!handler->block_count && (!handler->detail || handler->detail == detail) && sl@0: handler->sequential_number < max_sequential_handler_number) sl@0: { sl@0: SIGNAL_UNLOCK (); sl@0: g_closure_invoke (handler->closure, sl@0: return_accu, sl@0: node->n_params + 1, sl@0: instance_and_params, sl@0: &emission.ihint); sl@0: if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && sl@0: emission.state == EMISSION_RUN) sl@0: emission.state = EMISSION_STOP; sl@0: SIGNAL_LOCK (); sl@0: return_value_altered = TRUE; sl@0: sl@0: tmp = emission.state == EMISSION_RUN ? handler->next : NULL; sl@0: } sl@0: else sl@0: tmp = handler->next; sl@0: sl@0: if (tmp) sl@0: handler_ref (tmp); sl@0: handler_unref_R (signal_id, instance, handler_list); sl@0: handler_list = handler; sl@0: handler = tmp; sl@0: } sl@0: while (handler); sl@0: sl@0: if (emission.state == EMISSION_STOP) sl@0: goto EMIT_CLEANUP; sl@0: else if (emission.state == EMISSION_RESTART) sl@0: goto EMIT_RESTART; sl@0: } sl@0: sl@0: emission.ihint.run_type = G_SIGNAL_RUN_LAST; sl@0: sl@0: if ((node->flags & G_SIGNAL_RUN_LAST) && class_closure) sl@0: { sl@0: emission.state = EMISSION_RUN; sl@0: sl@0: emission.chain_type = G_TYPE_FROM_INSTANCE (instance); sl@0: SIGNAL_UNLOCK (); sl@0: g_closure_invoke (class_closure, sl@0: return_accu, sl@0: node->n_params + 1, sl@0: instance_and_params, sl@0: &emission.ihint); sl@0: if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && sl@0: emission.state == EMISSION_RUN) sl@0: emission.state = EMISSION_STOP; sl@0: SIGNAL_LOCK (); sl@0: emission.chain_type = G_TYPE_NONE; sl@0: return_value_altered = TRUE; sl@0: sl@0: if (emission.state == EMISSION_STOP) sl@0: goto EMIT_CLEANUP; sl@0: else if (emission.state == EMISSION_RESTART) sl@0: goto EMIT_RESTART; sl@0: } sl@0: sl@0: if (handler_list) sl@0: { sl@0: Handler *handler = handler_list; sl@0: sl@0: emission.state = EMISSION_RUN; sl@0: handler_ref (handler); sl@0: do sl@0: { sl@0: Handler *tmp; sl@0: sl@0: if (handler->after && !handler->block_count && (!handler->detail || handler->detail == detail) && sl@0: handler->sequential_number < max_sequential_handler_number) sl@0: { sl@0: SIGNAL_UNLOCK (); sl@0: g_closure_invoke (handler->closure, sl@0: return_accu, sl@0: node->n_params + 1, sl@0: instance_and_params, sl@0: &emission.ihint); sl@0: if (!accumulate (&emission.ihint, emission_return, &accu, accumulator) && sl@0: emission.state == EMISSION_RUN) sl@0: emission.state = EMISSION_STOP; sl@0: SIGNAL_LOCK (); sl@0: return_value_altered = TRUE; sl@0: sl@0: tmp = emission.state == EMISSION_RUN ? handler->next : NULL; sl@0: } sl@0: else sl@0: tmp = handler->next; sl@0: sl@0: if (tmp) sl@0: handler_ref (tmp); sl@0: handler_unref_R (signal_id, instance, handler); sl@0: handler = tmp; sl@0: } sl@0: while (handler); sl@0: sl@0: if (emission.state == EMISSION_STOP) sl@0: goto EMIT_CLEANUP; sl@0: else if (emission.state == EMISSION_RESTART) sl@0: goto EMIT_RESTART; sl@0: } sl@0: sl@0: EMIT_CLEANUP: sl@0: sl@0: emission.ihint.run_type = G_SIGNAL_RUN_CLEANUP; sl@0: sl@0: if ((node->flags & G_SIGNAL_RUN_CLEANUP) && class_closure) sl@0: { sl@0: gboolean need_unset = FALSE; sl@0: sl@0: emission.state = EMISSION_STOP; sl@0: sl@0: emission.chain_type = G_TYPE_FROM_INSTANCE (instance); sl@0: SIGNAL_UNLOCK (); sl@0: if (node->return_type != G_TYPE_NONE && !accumulator) sl@0: { sl@0: g_value_init (&accu, node->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE); sl@0: need_unset = TRUE; sl@0: } sl@0: g_closure_invoke (class_closure, sl@0: node->return_type != G_TYPE_NONE ? &accu : NULL, sl@0: node->n_params + 1, sl@0: instance_and_params, sl@0: &emission.ihint); sl@0: if (need_unset) sl@0: g_value_unset (&accu); sl@0: SIGNAL_LOCK (); sl@0: emission.chain_type = G_TYPE_NONE; sl@0: sl@0: if (emission.state == EMISSION_RESTART) sl@0: goto EMIT_RESTART; sl@0: } sl@0: sl@0: if (handler_list) sl@0: handler_unref_R (signal_id, instance, handler_list); sl@0: sl@0: emission_pop ((node->flags & G_SIGNAL_NO_RECURSE) ? &g_restart_emissions : &g_recursive_emissions, &emission); sl@0: SIGNAL_UNLOCK (); sl@0: if (accumulator) sl@0: g_value_unset (&accu); sl@0: sl@0: return return_value_altered; sl@0: } sl@0: sl@0: static const gchar* sl@0: type_debug_name (GType type) sl@0: { sl@0: if (type) sl@0: { sl@0: const char *name = g_type_name (type & ~G_SIGNAL_TYPE_STATIC_SCOPE); sl@0: return name ? name : ""; sl@0: } sl@0: else sl@0: return ""; sl@0: } sl@0: sl@0: /** sl@0: * g_signal_accumulator_true_handled: sl@0: * @ihint: standard #GSignalAccumulator parameter sl@0: * @return_accu: standard #GSignalAccumulator parameter sl@0: * @handler_return: standard #GSignalAccumulator parameter sl@0: * @dummy: standard #GSignalAccumulator parameter sl@0: * sl@0: * A predefined #GSignalAccumulator for signals that return a sl@0: * boolean values. The behavior that this accumulator gives is sl@0: * that a return of %TRUE stops the signal emission: no further sl@0: * callbacks will be invoked, while a return of %FALSE allows sl@0: * the emission to coninue. The idea here is that a %TRUE return sl@0: * indicates that the callback handled the signal, sl@0: * and no further handling is needed. sl@0: * sl@0: * Since: 2.4 sl@0: * sl@0: * Returns: standard #GSignalAccumulator result sl@0: */ sl@0: EXPORT_C gboolean sl@0: g_signal_accumulator_true_handled (GSignalInvocationHint *ihint, sl@0: GValue *return_accu, sl@0: const GValue *handler_return, sl@0: gpointer dummy) sl@0: { sl@0: gboolean continue_emission; sl@0: gboolean signal_handled; sl@0: sl@0: signal_handled = g_value_get_boolean (handler_return); sl@0: g_value_set_boolean (return_accu, signal_handled); sl@0: continue_emission = !signal_handled; sl@0: sl@0: return continue_emission; sl@0: } sl@0: sl@0: /* --- compile standard marshallers --- */ sl@0: #include "gmarshal.c" sl@0: sl@0: #define __G_SIGNAL_C__ sl@0: #include "gobjectaliasdef.c"