From beb9cd2eb219e04f9872c6a4dd743d5d1c36b4b1 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 13 Jun 2005 03:01:30 +0000 Subject: 2005-06-12 Colin Walters Async signals and various bugfixes and testing by Ross Burton . * glib/dbus-gvalue.h: (struct DBusBasicGValue): Delete. (dbus_gvalue_genmarshal_name_from_type) (dbus_gvalue_ctype_from_type): Moved to dbus-binding-tool-glib.c. (dbus_gtype_to_dbus_type): Renamed to dbus_gtype_from_signature. (dbus_g_value_types_init, dbus_gtype_from_signature) (dbus_gtype_from_signature_iter, dbus_gtype_to_signature) (dbus_gtypes_from_arg_signature): New function prototypes. (dbus_gvalue_demarshal): Take context and error arguments. (dbus_gvalue_demarshal_variant): New function. (dbus_gvalue_demarshal_message): New function. (dbus_gvalue_store): Delete. * glib/dbus-gvalue.c: File has been almost entirely rewritten; now we special-case more types such as DBUS_TYPE_SIGNATURE, handle arrays and hash tables correctly, etc. Full support for recursive values is not yet complete. * glib/dbus-gproxy.c (dbus_g_proxy_class_init): Change last argument of signal to G_TYPE_POINTER since we now pass a structure. (lookup_g_marshaller): Delete in favor of _dbus_gobject_lookup_marshaller. (marshal_dbus_message_to_g_marshaller): Use _dbus_gobject_lookup_marshaller and dbus_gvalue_demarshal_message to handle remote signal callbacks. (dbus_g_proxy_new_from_proxy): New function; creates a new DBusGProxy by copying an existing one. (dbus_g_proxy_get_interface, dbus_g_proxy_set_interface) (dbus_g_proxy_get_path): New functions. (dbus_g_proxy_marshal_args_to_message): New function; factored out of existing code. (DBUS_G_VALUE_ARRAY_COLLECT_ALL): Collect all arguments from a varargs array. (dbus_g_proxy_begin_call_internal): New function. (dbus_g_proxy_end_call_internal): New function. (dbus_g_proxy_begin_call): Take GTypes instead of DBus types as arguments; simply invoke dbus_g_proxy_begin_call_internal after collecting args into value array. (dbus_g_proxy_end_call): Take GTypes instead of DBus types; invoke dbus_g_proxy_end_call_internal. (dbus_g_proxy_invoke): Simply invoke begin_call_interanl and end_call_internal. (dbus_g_proxy_call_no_reply): Take GTypes instead of DBus types. (array_free_all): New function. (dbus_g_proxy_add_signal): Take GTypes. * glib/dbus-gobject.h: (_dbus_glib_marshal_dbus_message_to_gvalue_array): Delete. (_dbus_gobject_get_path, _dbus_gobject_lookup_marshaller): Prototype. * glib/dbus-gobject.c: Add a global marshal_table hash which stores mappings from type signatures to marshallers. Change lots of invocations of dbus_gtype_to_dbus_type to dbus_gtype_to_signature. (_dbus_glib_marshal_dbus_message_to_gvalue_array): Delete. (introspect_signals): Fix test for query.return_type. (set_object_property): Update invocation of dbus_gvalue_demarshal. (invoke_object_method): Many changes. Handle asynchronous invocations. Convert arguments with dbus_gvalue_demarshal_message. Handle errors. Use DBusSignatureIter instead of strlen on args. Handle all arguments generically. Special-case variants. (dbus_g_method_return, dbus_g_method_return_error): New function. (DBusGSignalClosure): New structure, closes over signal information. (dbus_g_signal_closure_new): New function. (dbus_g_signal_closure_finalize): New function. (signal_emitter_marshaller): New function; is special marshaller which emits signals on bus. (export_signals): New function; introspects object signals and connects to them. (dbus_g_object_type_install_info): Take GType instead of GObjectClass. (dbus_g_connection_register_g_object): Invoke export_signals. (dbus_g_connection_lookup_g_object): New function. (DBusGFuncSignature) New structure; used for mapping type signatures to marshallers. (funcsig_hash): New function; hashes DBusGFuncSignature. (funcsig_equal): New function; compares DBusGFuncSignature. (_dbus_gobject_lookup_marshaller): New function. (dbus_g_object_register_marshaller): New function; used to register a marshaller at runtime for a particular signature. * glib/dbus-gmain.c (_dbus_gmain_test): Add various tests. * glib/dbus-binding-tool-glib.h: Add DBUS_GLIB_ANNOTATION_ASYNC which notes a server method implementation should be asynchronous. * glib/dbus-binding-tool-glib.c (dbus_binding_tool_output_glib_server): Call dbus_g_value_types_init. (write_formal_parameters): Use dbus_gtype_from_signature. Handle variants specially. (dbus_g_type_get_lookup_function): Turn GType into an invocation of a lookup function. (write_args_for_direction): Use dbus_g_type_get_lookup_function. (write_untyped_out_args): New method; write output arguments. (write_formal_declarations_for_direction): Function for writing prototypes. (write_formal_parameters_for_direction): Function for writing implementations. (write_typed_args_for_direction): Function for writing arguments prefixed with GTypes. (write_async_method_client): Write out async version of method. * glib/dbus-binding-tool-glib.c: Include dbus-gvalue-utils.h. (dbus_g_type_get_marshal_name): Move mapping from GType to marshal name into here. (dbus_g_type_get_c_name): Move into here. (compute_marshaller): Convert signature to type with dbus_gtype_from_signature, use dbus_g_type_get_marshal_name. (compute_marshaller_name): Ditto. (compute_marshaller): Handle async signal annotations. (gather_marshallers): Return if we don't have a known prefix. (generate_glue): Collect introspection blob here, and write all of the blob at the end. This allows an object with multiple interfaces to work. Mark async methods in introspection blob. * glib/Makefile.am (libdbus_glib_1_la_SOURCES): Add dbus-gtype-specialized.c, dbus-gtype-specialized.h, dbus-gvalue-utils.h, dbus-gvalue-utils.c. * dbus/dbus-glib.h: Don't include dbus-protocol.h; this avoids people accidentally using DBUS_TYPE_* which should not be necessary anymore. Do include dbus-gtype-specialized.h, which are utilities for GLib container types. Add various #defines for types such as DBUS_TYPE_G_BOOLEAN_ARRAY. (DBusGValueIterator, DBusGValue): Define, not fully used yet. (dbus_g_value_get_g_type): Type for recursive value. (dbus_g_value_open, dbus_g_value_iterator_get_value) (dbus_g_value_iterator_get_values, dbus_g_value_iterator_recurse) (dbus_g_value_free): Prototypes. (dbus_g_object_register_marshaller, dbus_g_proxy_new_from_proxy): Prototype. (dbus_g_proxy_set_interface): Prototype. (dbus_g_proxy_begin_call, dbus_g_proxy_end_call) (dbus_g_proxy_call_no_reply): Take GLib types instead of DBus types. (dbus_g_proxy_get_path, dbus_g_proxy_get_interface): Accessors. (DBusGAsyncData, DBusGMethodInvocation): Structures for doing async invocations. (dbus_g_method_return, dbus_g_method_return_error): Prototypes. * doc/dbus-tutorial.xml: Update GLib section. * tools/dbus-viewer.c (load_child_nodes): Update for new invocation type of dbus_g_proxy_end_call. (load_from_service_thread_func): Ditto. * tools/print-introspect.c (main): Ditto. * tools/dbus-names-model.c (have_names_notify) (names_model_reload, names_model_set_connection) Use GTypes. * python/Makefile.am (INCLUDES): Define DBUS_COMPILATION, needed since Python bindings use GLib bindings. * test/glib/Makefile.am (INCLUDES): Define DBUS_COMPILATION. Add --prefix argument. * tools/Makefile.am: Define DBUS_COMPILATION. Remove unneeded --ignore-unsupported arg. * test/glib/test-service-glib.c: * test/glib/test-service-glib.xml: * test/glib/test-dbus-glib.c: Add many more tests. --- glib/dbus-gtype-specialized.c | 441 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 441 insertions(+) create mode 100644 glib/dbus-gtype-specialized.c (limited to 'glib/dbus-gtype-specialized.c') diff --git a/glib/dbus-gtype-specialized.c b/glib/dbus-gtype-specialized.c new file mode 100644 index 00000000..b331fcc5 --- /dev/null +++ b/glib/dbus-gtype-specialized.c @@ -0,0 +1,441 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* dbus-gtype-specialized.c: Non-DBus-specific functions for specialized GTypes + * + * Copyright (C) 2005 Red Hat, Inc. + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "dbus-gtype-specialized.h" +#include +#include +#include + +typedef enum { + DBUS_G_SPECTYPE_COLLECTION, + DBUS_G_SPECTYPE_MAP +} DBusGTypeSpecializedType; + +typedef struct { + DBusGTypeSpecializedType type; + const DBusGTypeSpecializedVtable *vtable; +} DBusGTypeSpecializedContainer; + +typedef struct { + GType types[6]; + const DBusGTypeSpecializedContainer *klass; +} DBusGTypeSpecializedData; + +static GHashTable /* char * -> data* */ *specialized_containers; + +static GQuark +specialized_type_data_quark () +{ + static GQuark quark; + if (!quark) + quark = g_quark_from_static_string ("DBusGTypeSpecializedData"); + + return quark; +} + +void +dbus_g_type_specialized_init (void) +{ + specialized_containers = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); +} + +static gboolean +specialized_types_is_initialized (void) +{ + return specialized_containers != NULL; +} + +static DBusGTypeSpecializedData * +lookup_specialization_data (GType type) +{ + return g_type_get_qdata (type, specialized_type_data_quark ()); +} + +/* Copied from gboxed.c */ +static void +proxy_value_init (GValue *value) +{ + value->data[0].v_pointer = NULL; +} + +/* Adapted from gboxed.c */ +static void +proxy_value_free (GValue *value) +{ + if (value->data[0].v_pointer && !(value->data[1].v_uint & G_VALUE_NOCOPY_CONTENTS)) + { + DBusGTypeSpecializedData *data; + GType type; + + type = G_VALUE_TYPE (value); + data = lookup_specialization_data (type); + g_assert (data != NULL); + + data->klass->vtable->free_func (type, value->data[0].v_pointer); + } +} + +/* Adapted from gboxed.c */ +static void +proxy_value_copy (const GValue *src_value, + GValue *dest_value) +{ + if (src_value->data[0].v_pointer) + { + DBusGTypeSpecializedData *data; + GType type; + type = G_VALUE_TYPE (src_value); + data = lookup_specialization_data (type); + g_assert (data != NULL); + dest_value->data[0].v_pointer = data->klass->vtable->copy_func (type, src_value->data[0].v_pointer); + } + else + dest_value->data[0].v_pointer = src_value->data[0].v_pointer; +} + +/* Copied from gboxed.c */ +static gpointer +proxy_value_peek_pointer (const GValue *value) +{ + return value->data[0].v_pointer; +} + +/* Adapted from gboxed.c */ +static gchar* +proxy_collect_value (GValue *value, + guint n_collect_values, + GTypeCValue *collect_values, + guint collect_flags) +{ + DBusGTypeSpecializedData *data; + GType type; + + type = G_VALUE_TYPE (value); + data = lookup_specialization_data (type); + + if (!collect_values[0].v_pointer) + value->data[0].v_pointer = NULL; + else + { + if (collect_flags & G_VALUE_NOCOPY_CONTENTS) + { + value->data[0].v_pointer = collect_values[0].v_pointer; + value->data[1].v_uint = G_VALUE_NOCOPY_CONTENTS; + } + else + value->data[0].v_pointer = data->klass->vtable->copy_func (type, collect_values[0].v_pointer); + } + + return NULL; +} + +/* Adapted from gboxed.c */ +static gchar* +proxy_lcopy_value (const GValue *value, + guint n_collect_values, + GTypeCValue *collect_values, + guint collect_flags) +{ + gpointer *boxed_p = collect_values[0].v_pointer; + + if (!boxed_p) + return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value)); + + if (!value->data[0].v_pointer) + *boxed_p = NULL; + else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) + *boxed_p = value->data[0].v_pointer; + else + { + DBusGTypeSpecializedData *data; + GType type; + + type = G_VALUE_TYPE (value); + data = lookup_specialization_data (type); + + *boxed_p = data->klass->vtable->copy_func (type, value->data[0].v_pointer); + } + + return NULL; +} + +static char * +build_specialization_name (const char *prefix, GType first_type, GType second_type) +{ + GString *fullname; + + fullname = g_string_new (prefix); + g_string_append_c (fullname, '+'); + g_string_append (fullname, g_type_name (first_type)); + if (second_type != G_TYPE_INVALID) + { + g_string_append_c (fullname, '+'); + g_string_append (fullname, g_type_name (second_type)); + } + return g_string_free (fullname, FALSE); +} + +static void +register_container (const char *name, + DBusGTypeSpecializedType type, + const DBusGTypeSpecializedVtable *vtable) +{ + DBusGTypeSpecializedContainer *klass; + + klass = g_new0 (DBusGTypeSpecializedContainer, 1); + klass->type = type; + klass->vtable = vtable; + + g_hash_table_insert (specialized_containers, g_strdup (name), klass); +} + +void +dbus_g_type_register_collection (const char *name, + const DBusGTypeSpecializedCollectionVtable *vtable, + guint flags) +{ + g_return_if_fail (specialized_types_is_initialized ()); + register_container (name, DBUS_G_SPECTYPE_COLLECTION, (const DBusGTypeSpecializedVtable*) vtable); +} + +void +dbus_g_type_register_map (const char *name, + const DBusGTypeSpecializedMapVtable *vtable, + guint flags) +{ + g_return_if_fail (specialized_types_is_initialized ()); + register_container (name, DBUS_G_SPECTYPE_MAP, (const DBusGTypeSpecializedVtable*) vtable); +} + +static GType +register_specialized_instance (const DBusGTypeSpecializedContainer *klass, + char *name, + GType first_type, + GType second_type) +{ + GType ret; + + static const GTypeValueTable vtable = + { + proxy_value_init, + proxy_value_free, + proxy_value_copy, + proxy_value_peek_pointer, + "p", + proxy_collect_value, + "p", + proxy_lcopy_value, + }; + static const GTypeInfo derived_info = + { + 0, /* class_size */ + NULL, /* base_init */ + NULL, /* base_finalize */ + NULL, /* class_init */ + NULL, /* class_finalize */ + NULL, /* class_data */ + 0, /* instance_size */ + 0, /* n_preallocs */ + NULL, /* instance_init */ + &vtable, /* value_table */ + }; + + ret = g_type_register_static (G_TYPE_BOXED, name, &derived_info, 0); + /* install proxy functions upon successfull registration */ + if (ret != G_TYPE_INVALID) + { + DBusGTypeSpecializedData *data; + data = g_new0 (DBusGTypeSpecializedData, 1); + data->types[0] = first_type; + data->types[1] = second_type; + data->klass = klass; + g_type_set_qdata (ret, specialized_type_data_quark (), data); + } + + return ret; +} + +static GType +lookup_or_register_specialized (const char *container, + GType first_type, + GType second_type) +{ + GType ret; + char *name; + const DBusGTypeSpecializedContainer *klass; + + g_return_val_if_fail (specialized_types_is_initialized (), G_TYPE_INVALID); + + klass = g_hash_table_lookup (specialized_containers, container); + g_return_val_if_fail (klass != NULL, G_TYPE_INVALID); + + name = build_specialization_name (container, first_type, second_type); + ret = g_type_from_name (name); + if (ret == G_TYPE_INVALID) + { + /* Take ownership of name */ + ret = register_specialized_instance (klass, name, + first_type, + second_type); + } + else + g_free (name); + return ret; +} + +GType +dbus_g_type_get_collection (const char *container, + GType specialization) +{ + return lookup_or_register_specialized (container, specialization, G_TYPE_INVALID); +} + +GType +dbus_g_type_get_map (const char *container, + GType key_specialization, + GType value_specialization) +{ + return lookup_or_register_specialized (container, key_specialization, value_specialization); +} + +gboolean +dbus_g_type_is_collection (GType gtype) +{ + DBusGTypeSpecializedData *data; + data = lookup_specialization_data (gtype); + if (data == NULL) + return FALSE; + return data->klass->type == DBUS_G_SPECTYPE_COLLECTION; +} + +gboolean +dbus_g_type_is_map (GType gtype) +{ + DBusGTypeSpecializedData *data; + data = lookup_specialization_data (gtype); + if (data == NULL) + return FALSE; + return data->klass->type == DBUS_G_SPECTYPE_MAP; +} + +static GType +get_specialization_index (GType gtype, guint i) +{ + DBusGTypeSpecializedData *data; + + data = lookup_specialization_data (gtype); + return data->types[i]; +} + +GType +dbus_g_type_get_collection_specialization (GType gtype) +{ + g_return_val_if_fail (dbus_g_type_is_collection (gtype), G_TYPE_INVALID); + return get_specialization_index (gtype, 0); +} + +GType +dbus_g_type_get_map_key_specialization (GType gtype) +{ + g_return_val_if_fail (dbus_g_type_is_map (gtype), G_TYPE_INVALID); + return get_specialization_index (gtype, 0); +} + +GType +dbus_g_type_get_map_value_specialization (GType gtype) +{ + g_return_val_if_fail (dbus_g_type_is_map (gtype), G_TYPE_INVALID); + return get_specialization_index (gtype, 1); +} + +gpointer +dbus_g_type_specialized_construct (GType type) +{ + DBusGTypeSpecializedData *data; + g_return_val_if_fail (specialized_types_is_initialized (), FALSE); + + data = lookup_specialization_data (type); + g_return_val_if_fail (data != NULL, FALSE); + + return data->klass->vtable->constructor (type); +} + +gboolean +dbus_g_type_collection_get_fixed (GValue *value, + gpointer *data_ret, + guint *len_ret) +{ + DBusGTypeSpecializedData *data; + GType gtype; + + g_return_val_if_fail (specialized_types_is_initialized (), FALSE); + g_return_val_if_fail (G_VALUE_HOLDS_BOXED (value), FALSE); + + gtype = G_VALUE_TYPE (value); + data = lookup_specialization_data (gtype); + g_return_val_if_fail (data != NULL, FALSE); + + return ((DBusGTypeSpecializedCollectionVtable *) (data->klass->vtable))->fixed_accessor (gtype, + g_value_get_boxed (value), + data_ret, len_ret); +} + +void +dbus_g_type_collection_value_iterate (GValue *value, + DBusGTypeSpecializedCollectionIterator iterator, + gpointer user_data) +{ + DBusGTypeSpecializedData *data; + GType gtype; + + g_return_if_fail (specialized_types_is_initialized ()); + g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); + + gtype = G_VALUE_TYPE (value); + data = lookup_specialization_data (gtype); + g_return_if_fail (data != NULL); + + ((DBusGTypeSpecializedCollectionVtable *) data->klass->vtable)->iterator (gtype, + g_value_get_boxed (value), + iterator, user_data); +} + +void +dbus_g_type_map_value_iterate (GValue *value, + DBusGTypeSpecializedMapIterator iterator, + gpointer user_data) +{ + DBusGTypeSpecializedData *data; + GType gtype; + + g_return_if_fail (specialized_types_is_initialized ()); + g_return_if_fail (G_VALUE_HOLDS_BOXED (value)); + + gtype = G_VALUE_TYPE (value); + data = lookup_specialization_data (gtype); + g_return_if_fail (data != NULL); + + ((DBusGTypeSpecializedMapVtable *) data->klass->vtable)->iterator (gtype, + g_value_get_boxed (value), + iterator, user_data); +} -- cgit