dbus/glib/dbus-gvalue.c
Colin Walters 03f6615eac 2005-02-17 Colin Walters <walters@verbum.org>
This patch is based on initial work from
	Paul Kuliniewicz <kuliniew@purdue.edu>.

	* glib/dbus-gvalue.c (dbus_gvalue_init): New function; move
	initialization of GValue from dbus type to here.
	(dbus_gvalue_genmarshal_name_from_type): New function; generates a string
	for the "glib-genmarshal" program from a DBus type.
	(dbus_gvalue_binding_type_from_type): New function; turns a DBus type
	into the C name for it we use in the glib bindings.
	(dbus_gvalue_ctype_from_type): New function; maps a DBus type into a
	glib C type (not GValue).
	(dbus_gvalue_demarshal): invoke dbus_gvalue_init.

	* glib/dbus-gutils.c (_dbus_gutils_wincaps_to_uscore): Moved here
	from dbus-gobject.c.

	* glib/dbus-gutils.h: Prototype it.

	* glib/dbus-gproxy.c: Include new dbus-gobject.h.
	(marshal_dbus_message_to_g_marshaller): Use new shared function
	dbus_glib_marshal_dbus_message_to_gvalue_array.

	* glib/dbus-gparser.c (parse_interface, parse_method): Handle c_name attribute.
	Will be changed once we have annotations.

	* glib/dbus-gobject.c: Change info_hash_mutex from GStaticMutex to
	GStaticRWLock.  Callers updated.
	(wincaps_to_uscore): Move to dbus-gutils.c.  Callers updated.
	(string_table_next): New function for iterating over zero-terminated
	string value array.
	(string_table_lookup): New function; retrieves specific entry in
	array.
	(get_method_data): New function; look up method data in object data chunk.
	(object_error_domain_prefix_from_object_info)
	(object_error_code_from_object_info): New functions, but not implemented yet.
	(method_interface_from_object_info): New function; retrieve interface name.
	(method_name_from_object_info): New function; retrieve method name.
	(method_arg_info_from_object_info): New function; retrieve argument data.
	(arg_iterate): New function; iterates over serialized argument data.
	(method_dir_signature_from_object_info): New function; returns a
	GString holding type signature for arguments for just one
	direction (input or output).
	(method_input_signature_from_object_info)
	(method_output_signature_from_object_info): New functions.
	(dbus_glib_marshal_dbus_message_to_gvalue_array): New shared function;
	converts dbus message arguments into a GValue array.  Used for both
	signal handling and method invocation.
	(struct DBusGlibWriteIterfaceData): New utility structure.
	(write_interface): New function; generate introspection XML for
	an interface.
	(introspect_interfaces): New function; gathers all interface->methods,
	generates introspection XML for them.
	(handle_introspect): Invoke introspect_interfaces.
	(get_object_property): Be sure to zero-initalize stack-allocated GValue.
	(lookup_object_and_method): New function; examines an incoming message
	and attempts to match it up (via interface, method name, and argument
	signature) with a known object and method.
	(gerror_domaincode_to_dbus_error_name): New function; converts a
	GError domain and code into a DBus error name.  Needs GError data
	added to object introspection to work well.
	(gerror_to_dbus_error_message): Creates a DBusMessage error return from
	GError.
	(invoke_object_method): New function to invoke an object method
	looked up via lookup_object_and_method.  Parses the incoming
	message, turns it into a GValue array, then invokes the marshaller
	specified in the DBusGMethodInfo.  Creates a new message with
	either return values or error message as appropriate.
	(gobject_message_function): Invoke lookup_object_and_method and
	invoke_object_method.

	* glib/dbus-glib-tool.c: Include dbus-binding-tool-glib.h.
	(enum DBusBindingOutputMode): New enum for binding output modes.
	(pretty_print): Print binding names.
	(dbus_binding_tool_error_quark): GError bits.
	(version): Fix typo.
	(main): Create GIOChannel for output.  Parse new --mode argument,
	possible values are "pretty-print", "glib-server", "glib-client".
	Use mode to invoke appropriate function.

	* glib/dbus-gobject.h: Prototype dbus_glib_marshal_dbus_message_to_gvalue_array.

	* glib/dbus-glib-tool.h: New header, just includes GError bits
	for now.

	* glib/dbus-gidl.c (struct InterfaceInfo): Add bindings hashtable;
	maps binding style to name.
	(struct MethodInfo): Ditto.
	(get_hash_keys, get_hash_key): Utility function, returns keys for
	a GHashTable.
	(interface_info_new, method_info_new): Initialize bindings.
	(interface_info_unref, method_info_unref): Destroy bindings.
	(method_info_get_binding_names, method_info_get_binding_name)
	(interface_info_get_binding_names, interface_info_get_binding_name):
	Functions for retrieving binding names.
	(method_info_set_binding_name, interface_info_set_binding_name):
	Functions for setting binding names.

	* glib/dbus-binding-tool-glib.h: New file, has prototypes
	for glib binding generation.

	* glib/dbus-binding-tool-glib.c: New file, implements server-side
	and client-side glib glue generation.

	* glib/Makefile.am (dbus_binding_tool_SOURCES): Add
	dbus-binding-tool-glib.c, dbus-binding-tool-glib.h,
	dbus-glib-tool.h.

	* dbus/dbus-glib.h (struct DBusGMethodMarshaller): Remove in favor
	of using GClosureMarshal directly.
	(struct DBusGObjectInfo): Add n_infos member.

	* test/glib/test-service-glib.xml: New file; contains introspection data
	for MyTestObject used in test-service-glib.c.

	* test/glib/test-service-glib.c (enum MyObjectError): New GError enum.
	(my_object_do_nothing, my_object_increment, my_object_throw_error)
	(my_object_uppercase, my_object_many_args): New test methods.
	(main): Use dbus_g_object_class_install_info to include generated object
	info.

	* test/glib/Makefile.am: Generate server-side glue for test-service-glib.c,
	as well as client-side bindings.

	* test/glib/test-dbus-glib.c: Include test-service-glib-bindings.h.
	(main): Activate TestSuiteGLibService; test invoke a bunch of its methods
	using both the dbus_gproxy stuff directly as well as the generated bindings.
2005-02-17 17:41:30 +00:00

383 lines
10 KiB
C

/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-gvalue.c GValue to-from DBusMessageIter
*
* Copyright (C) 2004 Ximian, Inc.
* 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-gvalue.h>
/* This is slightly evil, we don't use g_value_set_foo() functions */
#define MAP_BASIC_INIT(d_t, g_t) \
case DBUS_TYPE_##d_t: \
g_value_init (value, G_TYPE_##g_t); \
break
gboolean
dbus_gvalue_init (int type,
GValue *value)
{
gboolean can_convert;
can_convert = TRUE;
switch (type)
{
MAP_BASIC_INIT (BOOLEAN, BOOLEAN);
MAP_BASIC_INIT (BYTE, UCHAR);
MAP_BASIC_INIT (INT32, INT);
MAP_BASIC_INIT (UINT32, UINT);
MAP_BASIC_INIT (INT64, INT64);
MAP_BASIC_INIT (UINT64, UINT64);
MAP_BASIC_INIT (DOUBLE, DOUBLE);
case DBUS_TYPE_INT16:
g_value_init (value, G_TYPE_INT);
break;
case DBUS_TYPE_UINT16:
g_value_init (value, G_TYPE_UINT);
break;
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
g_value_init (value, G_TYPE_STRING);
break;
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_ARRAY:
case DBUS_TYPE_VARIANT:
default:
can_convert = FALSE;
}
#undef MAP_BASIC_INIT
return can_convert;
}
const char *
dbus_gvalue_genmarshal_name_from_type (int type)
{
switch (type)
{
case DBUS_TYPE_BOOLEAN:
return "BOOLEAN";
case DBUS_TYPE_BYTE:
return "UCHAR";
case DBUS_TYPE_INT32:
return "INT";
case DBUS_TYPE_UINT32:
return "UINT";
case DBUS_TYPE_INT64:
return "INT64";
case DBUS_TYPE_UINT64:
return "UINT64";
case DBUS_TYPE_DOUBLE:
return "DOUBLE";
case DBUS_TYPE_INT16:
return "INT";
break;
case DBUS_TYPE_UINT16:
return "UINT";
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
return "STRING";
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_ARRAY:
case DBUS_TYPE_VARIANT:
return NULL;
}
return NULL;
}
const char *
dbus_gvalue_binding_type_from_type (int type)
{
#define STRINGIFY(x) \
case x: \
return (#x)
switch (type)
{
STRINGIFY(DBUS_TYPE_BOOLEAN);
STRINGIFY(DBUS_TYPE_BYTE);
STRINGIFY(DBUS_TYPE_INT32);
STRINGIFY(DBUS_TYPE_UINT32);
STRINGIFY(DBUS_TYPE_INT64);
STRINGIFY(DBUS_TYPE_UINT64);
STRINGIFY(DBUS_TYPE_DOUBLE);
case DBUS_TYPE_INT16:
return "DBUS_TYPE_INT32";
case DBUS_TYPE_UINT16:
return "DBUS_TYPE_UINT32";
STRINGIFY(DBUS_TYPE_STRING);
STRINGIFY(DBUS_TYPE_OBJECT_PATH);
STRINGIFY(DBUS_TYPE_SIGNATURE);
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_ARRAY:
case DBUS_TYPE_VARIANT:
return NULL;
}
#undef STRINGIFY
return NULL;
}
const char *
dbus_gvalue_ctype_from_type (int type, gboolean in)
{
switch (type)
{
case DBUS_TYPE_BOOLEAN:
return "gboolean";
case DBUS_TYPE_BYTE:
return "guchar";
case DBUS_TYPE_INT32:
return "gint32";
case DBUS_TYPE_UINT32:
return "guint32";
case DBUS_TYPE_INT64:
return "gint64";
case DBUS_TYPE_UINT64:
return "guint64";
case DBUS_TYPE_DOUBLE:
return "gdouble";
case DBUS_TYPE_INT16:
return "gint";
break;
case DBUS_TYPE_UINT16:
return "guint";
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
/* FIXME - kind of a hack */
if (in)
return "const char *";
else
return "char *";
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_ARRAY:
case DBUS_TYPE_VARIANT:
return NULL;
}
return NULL;
}
gboolean
dbus_gvalue_demarshal (DBusMessageIter *iter, GValue *value)
{
gboolean can_convert = TRUE;
g_assert (sizeof (dbus_bool_t) == sizeof (value->data[0].v_int));
dbus_gvalue_init (dbus_message_iter_get_arg_type (iter), value);
/* This is slightly evil, we don't use g_value_set_foo() functions */
#define MAP_BASIC(d_t, g_t) \
case DBUS_TYPE_##d_t: \
dbus_message_iter_get_basic (iter, &value->data[0]); \
break
switch (dbus_message_iter_get_arg_type (iter))
{
MAP_BASIC (BOOLEAN, BOOLEAN);
MAP_BASIC (BYTE, UCHAR);
MAP_BASIC (INT32, INT);
MAP_BASIC (UINT32, UINT);
MAP_BASIC (INT64, INT64);
MAP_BASIC (UINT64, UINT64);
MAP_BASIC (DOUBLE, DOUBLE);
case DBUS_TYPE_INT16:
{
dbus_int16_t v;
dbus_message_iter_get_basic (iter, &v);
g_value_set_int (value, v);
}
break;
case DBUS_TYPE_UINT16:
{
dbus_uint16_t v;
dbus_message_iter_get_basic (iter, &v);
g_value_set_uint (value, v);
}
break;
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
{
const char *s;
dbus_message_iter_get_basic (iter, &s);
g_value_set_string (value, s);
}
break;
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_ARRAY:
case DBUS_TYPE_VARIANT:
default:
can_convert = FALSE;
}
#undef MAP_BASIC
return can_convert;
}
gboolean
dbus_gvalue_marshal (DBusMessageIter *iter, GValue *value)
{
gboolean can_convert = TRUE;
GType value_type = G_VALUE_TYPE (value);
value_type = G_VALUE_TYPE (value);
switch (value_type)
{
case G_TYPE_CHAR:
{
char b = g_value_get_char (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_BYTE,
&b))
goto nomem;
}
break;
case G_TYPE_UCHAR:
{
unsigned char b = g_value_get_uchar (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_BYTE,
&b))
goto nomem;
}
break;
case G_TYPE_BOOLEAN:
{
dbus_bool_t b = g_value_get_boolean (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_BOOLEAN,
&b))
goto nomem;
}
break;
case G_TYPE_INT:
{
dbus_int32_t v = g_value_get_int (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_INT32,
&v))
goto nomem;
}
break;
case G_TYPE_UINT:
{
dbus_uint32_t v = g_value_get_uint (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_UINT32,
&v))
goto nomem;
}
break;
/* long gets cut to 32 bits so the remote API is consistent
* on all architectures
*/
case G_TYPE_LONG:
{
dbus_int32_t v = g_value_get_long (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_INT32,
&v))
goto nomem;
}
break;
case G_TYPE_ULONG:
{
dbus_uint32_t v = g_value_get_ulong (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_UINT32,
&v))
goto nomem;
}
break;
case G_TYPE_INT64:
{
gint64 v = g_value_get_int64 (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_INT64,
&v))
goto nomem;
}
break;
case G_TYPE_UINT64:
{
guint64 v = g_value_get_uint64 (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_UINT64,
&v))
goto nomem;
}
break;
case G_TYPE_FLOAT:
{
double v = g_value_get_float (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_DOUBLE,
&v))
goto nomem;
}
break;
case G_TYPE_DOUBLE:
{
double v = g_value_get_double (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_DOUBLE,
&v))
goto nomem;
}
break;
case G_TYPE_STRING:
/* FIXME, the GValue string may not be valid UTF-8 */
{
const char *v = g_value_get_string (value);
if (!dbus_message_iter_append_basic (iter,
DBUS_TYPE_STRING,
&v))
goto nomem;
}
break;
default:
/* FIXME: we need to define custom boxed types for arrays
etc. so we can map them transparently / pleasantly */
can_convert = FALSE;
break;
}
return can_convert;
nomem:
g_error ("no memory");
return FALSE;
}