1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gobject.c Exporting a GObject remotely
4 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
5 * Copyright (C) 2005 Nokia
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <gobject/gvaluecollector.h>
27 #include <dbus/dbus-glib.h>
28 #include <dbus/dbus-glib-lowlevel.h>
29 #include "dbus-gtest.h"
30 #include "dbus-gutils.h"
31 #include "dbus-gobject.h"
32 #include "dbus-gvalue.h"
33 #include "dbus-gmarshal.h"
34 #include "dbus-gvalue-utils.h"
38 * @addtogroup DBusGLibInternals
42 static GStaticRWLock globals_lock = G_STATIC_RW_LOCK_INIT;
43 static GHashTable *info_hash = NULL;
44 static GHashTable *marshal_table = NULL;
47 uscore_to_wincaps (const char *uscore)
51 gboolean last_was_uscore;
53 last_was_uscore = TRUE;
55 str = g_string_new (NULL);
59 if (*p == '-' || *p == '_')
61 last_was_uscore = TRUE;
67 g_string_append_c (str, g_ascii_toupper (*p));
68 last_was_uscore = FALSE;
71 g_string_append_c (str, *p);
76 return g_string_free (str, FALSE);
80 string_table_next (const char *table)
82 return (table + (strlen (table) + 1));
86 string_table_lookup (const char *table, int index)
93 ret = string_table_next (ret);
99 get_method_data (const DBusGObjectInfo *object,
100 const DBusGMethodInfo *method)
102 return object->data + method->data_offset;
106 object_error_domain_prefix_from_object_info (const DBusGObjectInfo *info)
113 object_error_code_from_object_info (const DBusGObjectInfo *info, GQuark domain, gint code)
120 method_interface_from_object_info (const DBusGObjectInfo *object,
121 const DBusGMethodInfo *method)
123 return string_table_lookup (get_method_data (object, method), 0);
127 method_name_from_object_info (const DBusGObjectInfo *object,
128 const DBusGMethodInfo *method)
130 return string_table_lookup (get_method_data (object, method), 1);
134 method_arg_info_from_object_info (const DBusGObjectInfo *object,
135 const DBusGMethodInfo *method)
137 return string_table_lookup (get_method_data (object, method), 3);/*RB was 2*/
141 arg_iterate (const char *data, const char **name, gboolean *in,
146 data = string_table_next (data);
156 g_warning ("invalid arg direction");
160 data = string_table_next (data);
163 return string_table_next (data);
167 method_dir_signature_from_object_info (const DBusGObjectInfo *object,
168 const DBusGMethodInfo *method,
174 arg = method_arg_info_from_object_info (object, method);
176 ret = g_string_new (NULL);
184 arg = arg_iterate (arg, &name, &arg_in, &type);
187 g_string_append (ret, type);
190 return g_string_free (ret, FALSE);
194 method_input_signature_from_object_info (const DBusGObjectInfo *object,
195 const DBusGMethodInfo *method)
197 return method_dir_signature_from_object_info (object, method, TRUE);
201 method_output_signature_from_object_info (const DBusGObjectInfo *object,
202 const DBusGMethodInfo *method)
204 return method_dir_signature_from_object_info (object, method, FALSE);
208 propsig_iterate (const char *data, const char **iface, const char **name)
212 data = string_table_next (data);
215 return string_table_next (data);
218 static const DBusGObjectInfo *
219 lookup_object_info (GObject *object)
221 const DBusGObjectInfo *ret;
226 g_static_rw_lock_reader_lock (&globals_lock);
228 if (info_hash == NULL)
231 for (classtype = G_TYPE_FROM_INSTANCE (object); classtype != 0; classtype = g_type_parent (classtype))
233 const DBusGObjectInfo *info;
235 info = g_hash_table_lookup (info_hash, g_type_class_peek (classtype));
237 if (info != NULL && info->format_version == 0)
245 g_static_rw_lock_reader_unlock (&globals_lock);
251 gobject_unregister_function (DBusConnection *connection,
256 object = G_OBJECT (user_data);
266 const DBusGObjectInfo *object_info;
267 } DBusGLibWriteIterfaceData;
274 } DBusGLibWriteInterfaceValues;
277 write_interface (gpointer key, gpointer val, gpointer user_data)
284 const DBusGObjectInfo *object_info;
285 DBusGLibWriteIterfaceData *data;
286 DBusGLibWriteInterfaceValues *values;
291 methods = values->methods;
292 signals = values->signals;
293 properties = values->properties;
297 object_info = data->object_info;
299 g_string_append_printf (xml, " <interface name=\"%s\">\n", name);
301 /* FIXME: recurse to parent types ? */
302 for (; methods; methods = methods->next)
304 DBusGMethodInfo *method;
305 method = methods->data;
308 g_string_append_printf (xml, " <method name=\"%s\">\n",
309 method_name_from_object_info (object_info, method));
311 args = method_arg_info_from_object_info (object_info, method);
319 args = arg_iterate (args, &name, &arg_in, &type);
321 /* FIXME - handle container types */
322 g_string_append_printf (xml, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n",
323 name, type, arg_in ? "in" : "out");
326 g_string_append (xml, " </method>\n");
329 g_slist_free (values->methods);
331 for (; signals; signals = signals->next)
339 signame = signals->data;
341 s = _dbus_gutils_wincaps_to_uscore (signame);
343 id = g_signal_lookup (s, data->gtype);
346 g_signal_query (id, &query);
347 g_assert (query.return_type == G_TYPE_NONE);
349 g_string_append_printf (xml, " <signal name=\"%s\">\n", signame);
351 for (arg = 0; arg < query.n_params; arg++)
353 const char *dbus_type = dbus_gtype_to_signature (query.param_types[arg]);
355 g_assert (dbus_type != NULL);
357 g_string_append (xml, " <arg type=\"");
358 g_string_append (xml, dbus_type);
359 g_string_append (xml, "\"/>\n");
362 g_string_append (xml, " </signal>\n");
365 g_slist_free (values->signals);
367 for (; properties; properties = properties->next)
369 const char *propname;
371 const char *dbus_type;
376 propname = properties->data;
378 s = _dbus_gutils_wincaps_to_uscore (spec->name);
380 spec = g_object_class_find_property (g_type_class_peek (data->gtype), s);
381 g_assert (spec != NULL);
384 dbus_type = dbus_gtype_to_signature (G_PARAM_SPEC_VALUE_TYPE (spec));
385 g_assert (dbus_type != NULL);
387 can_set = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
388 (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
390 can_get = (spec->flags & G_PARAM_READABLE) != 0;
392 if (can_set || can_get)
394 g_string_append_printf (xml, " <property name=\"%s\" ", propname);
395 g_string_append (xml, "type=\"");
396 g_string_append (xml, dbus_type);
397 g_string_append (xml, "\" access=\"");
399 if (can_set && can_get)
400 g_string_append (xml, "readwrite");
402 g_string_append (xml, "read");
406 g_string_append (xml, "write");
409 g_string_append (xml, "\"/>\n");
414 g_string_append (xml, " </property>\n");
416 g_slist_free (values->properties);
419 g_string_append (xml, " </interface>\n");
422 static DBusGLibWriteInterfaceValues *
423 lookup_values (GHashTable *interfaces, const char *method_interface)
425 DBusGLibWriteInterfaceValues *values;
426 if ((values = g_hash_table_lookup (interfaces, (gpointer) method_interface)) == NULL)
428 values = g_new0 (DBusGLibWriteInterfaceValues, 1);
429 g_hash_table_insert (interfaces, (gpointer) method_interface, values);
435 introspect_interfaces (GObject *object, GString *xml)
437 const DBusGObjectInfo *info;
438 DBusGLibWriteIterfaceData data;
440 GHashTable *interfaces;
441 DBusGLibWriteInterfaceValues *values;
444 info = lookup_object_info (object);
446 g_assert (info != NULL);
448 /* Gather a list of all interfaces, indexed into their methods */
449 interfaces = g_hash_table_new (g_str_hash, g_str_equal);
450 for (i = 0; i < info->n_method_infos; i++)
452 const char *method_name;
453 const char *method_interface;
454 const char *method_args;
455 const DBusGMethodInfo *method;
457 method = &(info->method_infos[i]);
459 method_interface = method_interface_from_object_info (info, method);
460 method_name = method_name_from_object_info (info, method);
461 method_args = method_arg_info_from_object_info (info, method);
463 values = lookup_values (interfaces, method_interface);
464 values->methods = g_slist_prepend (values->methods, (gpointer) method);
467 propsig = info->exported_signals;
473 propsig = propsig_iterate (propsig, &iface, &signame);
475 values = lookup_values (interfaces, iface);
476 values->signals = g_slist_prepend (values->signals, (gpointer) signame);
479 propsig = info->exported_properties;
483 const char *propname;
485 propsig = propsig_iterate (propsig, &iface, &propname);
487 values = lookup_values (interfaces, iface);
488 values->properties = g_slist_prepend (values->properties, (gpointer) propname);
491 memset (&data, 0, sizeof (data));
493 data.gtype = G_TYPE_FROM_INSTANCE (object);
494 data.object_info = info;
495 g_hash_table_foreach (interfaces, write_interface, &data);
497 g_hash_table_destroy (interfaces);
500 static DBusHandlerResult
501 handle_introspect (DBusConnection *connection,
502 DBusMessage *message,
510 if (!dbus_connection_list_registered (connection,
511 dbus_message_get_path (message),
513 g_error ("Out of memory");
515 xml = g_string_new (NULL);
517 g_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
519 g_string_append (xml, "<node>\n");
521 /* We are introspectable, though I guess that was pretty obvious */
522 g_string_append_printf (xml, " <interface name=\"%s\">\n", DBUS_INTERFACE_INTROSPECTABLE);
523 g_string_append (xml, " <method name=\"Introspect\">\n");
524 g_string_append_printf (xml, " <arg name=\"data\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING);
525 g_string_append (xml, " </method>\n");
526 g_string_append (xml, " </interface>\n");
528 /* We support get/set properties */
529 g_string_append_printf (xml, " <interface name=\"%s\">\n", DBUS_INTERFACE_PROPERTIES);
530 g_string_append (xml, " <method name=\"Get\">\n");
531 g_string_append_printf (xml, " <arg name=\"interface\" direction=\"in\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING);
532 g_string_append_printf (xml, " <arg name=\"propname\" direction=\"in\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING);
533 g_string_append_printf (xml, " <arg name=\"value\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_VARIANT_AS_STRING);
534 g_string_append (xml, " </method>\n");
535 g_string_append (xml, " <method name=\"Set\">\n");
536 g_string_append_printf (xml, " <arg name=\"interface\" direction=\"in\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING);
537 g_string_append_printf (xml, " <arg name=\"propname\" direction=\"in\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING);
538 g_string_append_printf (xml, " <arg name=\"value\" direction=\"in\" type=\"%s\"/>\n", DBUS_TYPE_VARIANT_AS_STRING);
539 g_string_append (xml, " </method>\n");
540 g_string_append (xml, " </interface>\n");
542 introspect_interfaces (object, xml);
544 /* Append child nodes */
545 for (i = 0; children[i]; i++)
546 g_string_append_printf (xml, " <node name=\"%s\"/>\n",
549 /* Close the XML, and send it to the requesting app */
550 g_string_append (xml, "</node>\n");
552 ret = dbus_message_new_method_return (message);
554 g_error ("Out of memory");
556 dbus_message_append_args (ret,
557 DBUS_TYPE_STRING, &xml->str,
560 dbus_connection_send (connection, ret, NULL);
561 dbus_message_unref (ret);
563 g_string_free (xml, TRUE);
565 dbus_free_string_array (children);
567 return DBUS_HANDLER_RESULT_HANDLED;
571 set_object_property (DBusConnection *connection,
572 DBusMessage *message,
573 DBusMessageIter *iter,
577 GValue value = { 0, };
580 DBusGValueMarshalCtx context;
582 dbus_message_iter_recurse (iter, &sub);
584 context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (connection);
585 context.proxy = NULL;
587 g_value_init (&value, pspec->value_type);
588 if (dbus_gvalue_demarshal (&context, &sub, &value, NULL))
590 g_object_set_property (object,
594 g_value_unset (&value);
596 ret = dbus_message_new_method_return (message);
598 g_error ("out of memory");
602 ret = dbus_message_new_error (message,
603 DBUS_ERROR_INVALID_ARGS,
604 "Argument's D-BUS type can't be converted to a GType");
606 g_error ("out of memory");
613 get_object_property (DBusConnection *connection,
614 DBusMessage *message,
619 GValue value = {0, };
621 DBusMessageIter iter;
623 value_type = G_PARAM_SPEC_VALUE_TYPE (pspec);
625 ret = dbus_message_new_method_return (message);
627 g_error ("out of memory");
629 g_value_init (&value, value_type);
630 g_object_get_property (object, pspec->name, &value);
632 value_type = G_VALUE_TYPE (&value);
634 dbus_message_iter_init_append (message, &iter);
636 if (!dbus_gvalue_marshal (&iter, &value))
638 dbus_message_unref (ret);
639 ret = dbus_message_new_error (message,
640 DBUS_ERROR_UNKNOWN_METHOD,
641 "Can't convert GType of object property to a D-BUS type");
648 lookup_object_and_method (GObject *object,
649 DBusMessage *message,
650 const DBusGObjectInfo **object_ret,
651 const DBusGMethodInfo **method_ret)
653 const char *interface;
655 const char *signature;
657 const DBusGObjectInfo *info;
660 interface = dbus_message_get_interface (message);
661 member = dbus_message_get_member (message);
662 signature = dbus_message_get_signature (message);
665 info = lookup_object_info (object);
668 for (i = 0; i < info->n_method_infos; i++)
670 const char *expected_member;
671 const char *expected_interface;
672 char *expected_signature;
673 const DBusGMethodInfo *method;
675 method = &(info->method_infos[i]);
677 /* Check method interface/name and input signature */
678 expected_interface = method_interface_from_object_info (*object_ret, method);
679 expected_member = method_name_from_object_info (*object_ret, method);
680 expected_signature = method_input_signature_from_object_info (*object_ret, method);
682 if ((interface == NULL
683 || strcmp (expected_interface, interface) == 0)
684 && strcmp (expected_member, member) == 0
685 && strcmp (expected_signature, signature) == 0)
687 g_free (expected_signature);
688 *method_ret = method;
691 g_free (expected_signature);
698 gerror_domaincode_to_dbus_error_name (const DBusGObjectInfo *object_info,
699 GQuark domain, gint code)
701 const char *domain_str;
702 const char *code_str;
703 GString *dbus_error_name;
705 domain_str = object_error_domain_prefix_from_object_info (object_info);
706 code_str = object_error_code_from_object_info (object_info, domain, code);
708 if (!domain_str || !code_str)
710 /* If we can't map it sensibly, make up an error name */
711 char *domain_from_quark;
713 dbus_error_name = g_string_new ("org.freedesktop.DBus.GLib.UnmappedError.");
715 domain_from_quark = uscore_to_wincaps (g_quark_to_string (domain));
716 g_string_append (dbus_error_name, domain_from_quark);
717 g_free (domain_from_quark);
719 g_string_append_printf (dbus_error_name, ".Code%d", code);
723 dbus_error_name = g_string_new (domain_str);
724 g_string_append_c (dbus_error_name, '.');
725 g_string_append (dbus_error_name, code_str);
728 return g_string_free (dbus_error_name, FALSE);
732 gerror_to_dbus_error_message (const DBusGObjectInfo *object_info,
733 DBusMessage *message,
742 error_msg = g_strdup_printf ("Method invoked for %s returned FALSE but did not set error", dbus_message_get_member (message));
743 reply = dbus_message_new_error (message, "org.freedesktop.DBus.GLib.ErrorError", error_msg);
748 if (error->domain == DBUS_GERROR)
749 reply = dbus_message_new_error (message,
750 dbus_g_error_get_name (error),
755 error_name = gerror_domaincode_to_dbus_error_name (object_info, error->domain, error->code);
756 reply = dbus_message_new_error (message, error_name, error->message);
764 * The context of an asynchronous method call. See dbus_g_method_return() and
765 * dbus_g_method_return_error().
767 struct _DBusGMethodInvocation {
768 DBusGConnection *connection; /**< The connection */
769 DBusGMessage *message; /**< The message which generated the method call */
770 const DBusGObjectInfo *object; /**< The object the method was called on */
771 const DBusGMethodInfo *method; /**< The method called */
774 static DBusHandlerResult
775 invoke_object_method (GObject *object,
776 const DBusGObjectInfo *object_info,
777 const DBusGMethodInfo *method,
778 DBusConnection *connection,
779 DBusMessage *message)
781 gboolean had_error, call_only;
783 GValueArray *value_array;
784 GValue object_value = {0,};
785 GValue error_value = {0,};
786 GValue return_value = {0,};
789 char *out_signature = NULL;
791 DBusSignatureIter out_signature_iter;
792 GArray *out_param_values = NULL;
793 GValueArray *out_param_gvalues = NULL;
795 int out_param_pos, out_param_gvalue_pos;
796 DBusHandlerResult result;
801 if (strcmp (string_table_lookup (get_method_data (object_info, method), 2), "A") == 0)
806 /* This is evil. We do this to work around the fact that
807 * the generated glib marshallers check a flag in the closure object
808 * which we don't care about. We don't need/want to create
809 * a new closure for each invocation.
811 memset (&closure, 0, sizeof (closure));
813 in_signature = method_input_signature_from_object_info (object_info, method);
815 /* Convert method IN parameters to GValueArray */
820 DBusGValueMarshalCtx context;
821 GError *error = NULL;
823 context.gconnection = DBUS_G_CONNECTION_FROM_CONNECTION (connection);
824 context.proxy = NULL;
826 types_array = dbus_gtypes_from_arg_signature (in_signature, FALSE);
827 n_params = types_array->len;
828 types = (const GType*) types_array->data;
830 value_array = dbus_gvalue_demarshal_message (&context, message, n_params, types, &error);
831 if (value_array == NULL)
833 g_free (in_signature);
834 g_array_free (types_array, TRUE);
835 reply = dbus_message_new_error (message, "org.freedesktop.DBus.GLib.ErrorError", error->message);
836 dbus_connection_send (connection, reply, NULL);
837 dbus_message_unref (reply);
838 g_error_free (error);
839 return DBUS_HANDLER_RESULT_HANDLED;
841 g_array_free (types_array, TRUE);
844 /* Prepend object as first argument */
845 g_value_init (&object_value, G_TYPE_OBJECT);
846 g_value_set_object (&object_value, object);
847 g_value_array_prepend (value_array, &object_value);
851 GValue context_value = {0,};
852 DBusGMethodInvocation *context;
853 context = g_new (DBusGMethodInvocation, 1);
854 context->connection = dbus_g_connection_ref (DBUS_G_CONNECTION_FROM_CONNECTION (connection));
855 context->message = dbus_g_message_ref (DBUS_G_MESSAGE_FROM_MESSAGE (message));
856 context->object = object_info;
857 context->method = method;
858 g_value_init (&context_value, G_TYPE_POINTER);
859 g_value_set_pointer (&context_value, context);
860 g_value_array_append (value_array, &context_value);
864 out_signature = method_output_signature_from_object_info (object_info, method);
866 /* Count number of output parameters */
867 dbus_signature_iter_init (&out_signature_iter, out_signature);
869 while ((current_type = dbus_signature_iter_get_current_type (&out_signature_iter)) != DBUS_TYPE_INVALID)
872 dbus_signature_iter_next (&out_signature_iter);
875 /* Create an array to store the actual values of OUT
876 * parameters. Then, create a GValue boxed POINTER
877 * to each of those values, and append to the invocation,
878 * so the method can return the OUT parameters.
880 out_param_values = g_array_sized_new (FALSE, TRUE, sizeof (GTypeCValue), out_param_count);
882 /* We have a special array of GValues for toplevel GValue return
885 out_param_gvalues = g_value_array_new (out_param_count);
887 out_param_gvalue_pos = 0;
888 dbus_signature_iter_init (&out_signature_iter, out_signature);
889 while ((current_type = dbus_signature_iter_get_current_type (&out_signature_iter)) != DBUS_TYPE_INVALID)
891 GValue value = {0, };
894 g_value_init (&value, G_TYPE_POINTER);
896 /* We special case variants to make method invocation a bit nicer */
897 if (current_type != DBUS_TYPE_VARIANT)
899 memset (&storage, 0, sizeof (storage));
900 g_array_append_val (out_param_values, storage);
901 g_value_set_pointer (&value, &(g_array_index (out_param_values, GTypeCValue, out_param_pos)));
906 g_value_array_append (out_param_gvalues, NULL);
907 g_value_set_pointer (&value, out_param_gvalues->values + out_param_gvalue_pos);
908 out_param_gvalue_pos++;
910 g_value_array_append (value_array, &value);
911 dbus_signature_iter_next (&out_signature_iter);
914 /* Append GError as final argument */
915 g_value_init (&error_value, G_TYPE_POINTER);
916 g_value_set_pointer (&error_value, &gerror);
917 g_value_array_append (value_array, &error_value);
919 /* Actually invoke method */
920 g_value_init (&return_value, G_TYPE_BOOLEAN);
921 method->marshaller (&closure, &return_value,
922 value_array->n_values,
924 NULL, method->function);
927 result = DBUS_HANDLER_RESULT_HANDLED;
930 had_error = !g_value_get_boolean (&return_value);
934 DBusMessageIter iter;
936 reply = dbus_message_new_method_return (message);
940 /* Append OUT arguments to reply */
941 dbus_message_iter_init_append (reply, &iter);
942 dbus_signature_iter_init (&out_signature_iter, out_signature);
944 out_param_gvalue_pos = 0;
945 while ((current_type = dbus_signature_iter_get_current_type (&out_signature_iter)) != DBUS_TYPE_INVALID)
947 GValue gvalue = {0, };
949 g_value_init (&gvalue, dbus_gtype_from_signature_iter (&out_signature_iter, FALSE));
950 if (current_type != DBUS_TYPE_VARIANT)
952 if (!dbus_gvalue_take (&gvalue,
953 &(g_array_index (out_param_values, GTypeCValue, out_param_pos))))
954 g_assert_not_reached ();
959 g_value_set_static_boxed (&gvalue, out_param_gvalues->values + out_param_gvalue_pos);
960 out_param_gvalue_pos++;
963 if (!dbus_gvalue_marshal (&iter, &gvalue))
965 /* Here we actually free the allocated value; we
966 * took ownership of it with dbus_gvalue_take.
968 g_value_unset (&gvalue);
969 dbus_signature_iter_next (&out_signature_iter);
973 reply = gerror_to_dbus_error_message (object_info, message, gerror);
977 dbus_connection_send (connection, reply, NULL);
978 dbus_message_unref (reply);
981 result = DBUS_HANDLER_RESULT_HANDLED;
983 g_free (in_signature);
984 g_free (out_signature);
987 g_array_free (out_param_values, TRUE);
988 g_value_array_free (out_param_gvalues);
989 g_value_unset (&object_value);
990 g_value_unset (&error_value);
992 g_value_array_free (value_array);
993 g_value_unset (&return_value);
996 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
1000 static DBusHandlerResult
1001 gobject_message_function (DBusConnection *connection,
1002 DBusMessage *message,
1010 const char *wincaps_propname;
1011 /* const char *wincaps_propiface; */
1012 DBusMessageIter iter;
1013 const DBusGMethodInfo *method;
1014 const DBusGObjectInfo *object_info;
1016 object = G_OBJECT (user_data);
1018 if (dbus_message_is_method_call (message,
1019 DBUS_INTERFACE_INTROSPECTABLE,
1021 return handle_introspect (connection, message, object);
1023 /* Try the metainfo, which lets us invoke methods */
1024 if (lookup_object_and_method (object, message, &object_info, &method))
1025 return invoke_object_method (object, object_info, method, connection, message);
1027 /* If no metainfo, we can still do properties and signals
1028 * via standard GLib introspection
1032 if (dbus_message_is_method_call (message,
1033 DBUS_INTERFACE_PROPERTIES,
1036 else if (dbus_message_is_method_call (message,
1037 DBUS_INTERFACE_PROPERTIES,
1041 if (!(setter || getter))
1042 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1044 dbus_message_iter_init (message, &iter);
1046 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
1048 g_warning ("Property get or set does not have an interface string as first arg\n");
1049 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1051 /* We never use the interface name; if we did, we'd need to
1052 * remember that it can be empty string for "pick one for me"
1054 /* dbus_message_iter_get_basic (&iter, &wincaps_propiface); */
1055 dbus_message_iter_next (&iter);
1057 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_STRING)
1059 g_warning ("Property get or set does not have a property name string as second arg\n");
1060 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1062 dbus_message_iter_get_basic (&iter, &wincaps_propname);
1063 dbus_message_iter_next (&iter);
1065 s = _dbus_gutils_wincaps_to_uscore (wincaps_propname);
1067 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
1078 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_VARIANT)
1080 g_warning ("Property set does not have a variant value as third arg\n");
1081 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1084 ret = set_object_property (connection, message, &iter,
1086 dbus_message_iter_next (&iter);
1090 ret = get_object_property (connection, message,
1095 g_assert_not_reached ();
1099 g_assert (ret != NULL);
1101 if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID)
1102 g_warning ("Property get or set had too many arguments\n");
1104 dbus_connection_send (connection, ret, NULL);
1105 dbus_message_unref (ret);
1106 return DBUS_HANDLER_RESULT_HANDLED;
1109 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1112 static DBusObjectPathVTable gobject_dbus_vtable = {
1113 gobject_unregister_function,
1114 gobject_message_function,
1120 DBusGConnection *connection;
1122 const char *signame;
1123 const char *sigiface;
1124 } DBusGSignalClosure;
1127 dbus_g_signal_closure_new (DBusGConnection *connection,
1129 const char *signame,
1130 const char *sigiface)
1132 DBusGSignalClosure *closure;
1134 closure = (DBusGSignalClosure*) g_closure_new_simple (sizeof (DBusGSignalClosure), NULL);
1136 closure->connection = dbus_g_connection_ref (connection);
1137 closure->object = object;
1138 closure->signame = signame;
1139 closure->sigiface = sigiface;
1140 return (GClosure*) closure;
1144 dbus_g_signal_closure_finalize (gpointer data,
1147 DBusGSignalClosure *sigclosure = (DBusGSignalClosure *) closure;
1149 dbus_g_connection_unref (sigclosure->connection);
1153 signal_emitter_marshaller (GClosure *closure,
1155 guint n_param_values,
1156 const GValue *param_values,
1157 gpointer invocation_hint,
1158 gpointer marshal_data)
1160 DBusGSignalClosure *sigclosure;
1161 DBusMessage *signal;
1162 DBusMessageIter iter;
1166 sigclosure = (DBusGSignalClosure *) closure;
1168 g_assert (retval == NULL);
1170 path = _dbus_gobject_get_path (sigclosure->object);
1172 g_assert (path != NULL);
1174 signal = dbus_message_new_signal (path,
1175 sigclosure->sigiface,
1176 sigclosure->signame);
1179 g_error ("out of memory");
1183 dbus_message_iter_init_append (signal, &iter);
1185 /* First argument is the object itself, and we can't marshall that */
1186 for (i = 1; i < n_param_values; i++)
1188 if (!dbus_gvalue_marshal (&iter,
1189 (GValue *) (&(param_values[i]))))
1191 g_warning ("failed to marshal parameter %d for signal %s",
1192 i, sigclosure->signame);
1196 dbus_connection_send (DBUS_CONNECTION_FROM_G_CONNECTION (sigclosure->connection),
1199 dbus_message_unref (signal);
1203 export_signals (DBusGConnection *connection, const DBusGObjectInfo *info, GObject *object)
1206 const char *sigdata;
1208 const char *signame;
1210 gtype = G_TYPE_FROM_INSTANCE (object);
1212 sigdata = info->exported_signals;
1214 while (*sigdata != '\0')
1221 sigdata = propsig_iterate (sigdata, &iface, &signame);
1223 s = _dbus_gutils_wincaps_to_uscore (signame);
1225 id = g_signal_lookup (s, gtype);
1228 g_warning ("signal \"%s\" (from \"%s\") exported but not found in object class \"%s\"",
1229 s, signame, g_type_name (gtype));
1234 g_signal_query (id, &query);
1236 if (query.return_type != G_TYPE_NONE)
1238 g_warning ("Not exporting signal \"%s\" for object class \"%s\" as it has a return type \"%s\"",
1239 s, g_type_name (gtype), g_type_name (query.return_type));
1241 continue; /* FIXME: these could be listed as methods ? */
1244 closure = dbus_g_signal_closure_new (connection, object, signame, (char*) iface);
1245 g_closure_set_marshal (closure, signal_emitter_marshaller);
1247 g_signal_connect_closure_by_id (object,
1253 g_closure_add_finalize_notifier (closure, NULL,
1254 dbus_g_signal_closure_finalize);
1259 /** @} */ /* end of internals */
1262 * @addtogroup DBusGLib
1267 * Install introspection information about the given object GType
1268 * sufficient to allow methods on the object to be invoked by name.
1269 * The introspection information is normally generated by
1270 * dbus-glib-tool, then this function is called in the
1271 * class_init() for the object class.
1273 * Once introspection information has been installed, instances of the
1274 * object registered with dbus_g_connection_register_g_object() can have
1275 * their methods invoked remotely.
1277 * @param object_type GType for the object
1278 * @param info introspection data generated by dbus-glib-tool
1281 dbus_g_object_type_install_info (GType object_type,
1282 const DBusGObjectInfo *info)
1284 GObjectClass *object_class;
1286 g_return_if_fail (G_TYPE_IS_OBJECT (object_type));
1288 dbus_g_value_types_init ();
1290 object_class = g_type_class_peek (object_type);
1292 g_return_if_fail (G_IS_OBJECT_CLASS (object_class));
1294 g_static_rw_lock_writer_lock (&globals_lock);
1296 if (info_hash == NULL)
1298 info_hash = g_hash_table_new (NULL, NULL); /* direct hash */
1301 g_hash_table_replace (info_hash, object_class, (void*) info);
1303 g_static_rw_lock_writer_unlock (&globals_lock);
1307 unregister_gobject (DBusGConnection *connection, GObject *dead)
1310 path = g_object_steal_data (dead, "dbus_glib_object_path");
1311 dbus_connection_unregister_object_path (DBUS_CONNECTION_FROM_G_CONNECTION (connection), path);
1316 * Registers a GObject at the given path. Properties, methods, and signals
1317 * of the object can then be accessed remotely. Methods are only available
1318 * if method introspection data has been added to the object's class
1319 * with g_object_class_install_info().
1321 * The registration will be cancelled if either the DBusConnection or
1322 * the GObject gets finalized.
1324 * @param connection the D-BUS connection
1325 * @param at_path the path where the object will live (the object's name)
1326 * @param object the object
1329 dbus_g_connection_register_g_object (DBusGConnection *connection,
1330 const char *at_path,
1333 const DBusGObjectInfo *info;
1334 g_return_if_fail (connection != NULL);
1335 g_return_if_fail (at_path != NULL);
1336 g_return_if_fail (G_IS_OBJECT (object));
1338 info = lookup_object_info (object);
1341 g_warning ("No introspection data registered for object class \"%s\"",
1342 g_type_name (G_TYPE_FROM_INSTANCE (object)));
1346 if (!dbus_connection_register_object_path (DBUS_CONNECTION_FROM_G_CONNECTION (connection),
1348 &gobject_dbus_vtable,
1351 g_error ("Failed to register GObject with DBusConnection");
1355 export_signals (connection, info, object);
1357 g_object_set_data (object, "dbus_glib_object_path", g_strdup (at_path));
1358 g_object_weak_ref (object, (GWeakNotify)unregister_gobject, connection);
1362 dbus_g_connection_lookup_g_object (DBusGConnection *connection,
1363 const char *at_path)
1366 if (!dbus_connection_get_object_path_data (DBUS_CONNECTION_FROM_G_CONNECTION (connection), at_path, &ret))
1375 } DBusGFuncSignature;
1378 funcsig_hash (gconstpointer key)
1380 const DBusGFuncSignature *sig = key;
1386 types = sig->params;
1388 for (i = 0; i < sig->n_params; i++)
1390 ret += (int) (*types);
1398 funcsig_equal (gconstpointer aval,
1401 const DBusGFuncSignature *a = aval;
1402 const DBusGFuncSignature *b = bval;
1403 const GType *atypes;
1404 const GType *btypes;
1407 if (a->rettype != b->rettype
1408 || a->n_params != b->n_params)
1414 for (i = 0; i < a->n_params; i++)
1416 if (*btypes != *atypes)
1426 _dbus_gobject_lookup_marshaller (GType rettype,
1428 const GType *param_types)
1430 GClosureMarshal ret;
1431 DBusGFuncSignature sig;
1435 /* Convert to fundamental types */
1436 rettype = G_TYPE_FUNDAMENTAL (rettype);
1437 params = g_new (GType, n_params);
1438 for (i = 0; i < n_params; i++)
1439 params[i] = G_TYPE_FUNDAMENTAL (param_types[i]);
1441 sig.rettype = rettype;
1442 sig.n_params = n_params;
1443 sig.params = params;
1445 g_static_rw_lock_reader_lock (&globals_lock);
1448 ret = g_hash_table_lookup (marshal_table, &sig);
1452 g_static_rw_lock_reader_unlock (&globals_lock);
1456 if (rettype == G_TYPE_NONE)
1459 ret = g_cclosure_marshal_VOID__VOID;
1460 else if (n_params == 1)
1464 case G_TYPE_BOOLEAN:
1465 ret = g_cclosure_marshal_VOID__BOOLEAN;
1468 ret = g_cclosure_marshal_VOID__UCHAR;
1471 ret = g_cclosure_marshal_VOID__INT;
1474 ret = g_cclosure_marshal_VOID__UINT;
1477 ret = g_cclosure_marshal_VOID__DOUBLE;
1480 ret = g_cclosure_marshal_VOID__STRING;
1483 ret = g_cclosure_marshal_VOID__BOXED;
1488 else if (n_params == 3
1489 && params[0] == G_TYPE_STRING
1490 && params[1] == G_TYPE_STRING
1491 && params[2] == G_TYPE_STRING)
1493 ret = _dbus_g_marshal_NONE__STRING_STRING_STRING;
1502 * Register a GClosureMarshal to be used for signal invocations,
1503 * giving its return type and a list of parameter types,
1504 * followed by G_TYPE_INVALID.
1506 * This function will not be needed once GLib includes libffi.
1508 * @param marshaller a GClosureMarshal to be used for invocation
1509 * @param rettype a GType for the return type of the function
1510 * @param ... The parameter GTypes, followed by G_TYPE_INVALID
1513 dbus_g_object_register_marshaller (GClosureMarshal marshaller,
1521 va_start (args, rettype);
1523 types = g_array_new (TRUE, TRUE, sizeof (GType));
1525 while ((gtype = va_arg (args, GType)) != G_TYPE_INVALID)
1526 g_array_append_val (types, gtype);
1528 dbus_g_object_register_marshaller_array (marshaller, rettype,
1529 types->len, (GType*) types->data);
1531 g_array_free (types, TRUE);
1536 * Register a GClosureMarshal to be used for signal invocations.
1537 * See also #dbus_g_object_register_marshaller
1539 * @param marshaller a GClosureMarshal to be used for invocation
1540 * @param rettype a GType for the return type of the function
1541 * @param n_types number of function parameters
1542 * @param param_types a C array of GTypes values
1545 dbus_g_object_register_marshaller_array (GClosureMarshal marshaller,
1550 DBusGFuncSignature *sig;
1553 g_static_rw_lock_writer_lock (&globals_lock);
1555 if (marshal_table == NULL)
1556 marshal_table = g_hash_table_new_full (funcsig_hash,
1560 sig = g_new0 (DBusGFuncSignature, 1);
1561 sig->rettype = G_TYPE_FUNDAMENTAL (rettype);
1562 sig->n_params = n_types;
1563 sig->params = g_new (GType, n_types);
1564 for (i = 0; i < n_types; i++)
1565 sig->params[i] = G_TYPE_FUNDAMENTAL (types[i]);
1567 g_hash_table_insert (marshal_table, sig, marshaller);
1569 g_static_rw_lock_writer_unlock (&globals_lock);
1573 * Send a return message for a given method invocation, with arguments.
1574 * This function also frees the sending context.
1576 * @param context the method context
1579 dbus_g_method_return (DBusGMethodInvocation *context, ...)
1582 DBusMessageIter iter;
1588 reply = dbus_message_new_method_return (dbus_g_message_get_message (context->message));
1589 out_sig = method_output_signature_from_object_info (context->object, context->method);
1590 argsig = dbus_gtypes_from_arg_signature (out_sig, FALSE);
1592 dbus_message_iter_init_append (reply, &iter);
1594 va_start (args, context);
1595 for (i = 0; i < argsig->len; i++)
1597 GValue value = {0,};
1599 g_value_init (&value, g_array_index (argsig, GType, i));
1601 G_VALUE_COLLECT (&value, args, G_VALUE_NOCOPY_CONTENTS, &error);
1607 dbus_gvalue_marshal (&iter, &value);
1611 dbus_connection_send (dbus_g_connection_get_connection (context->connection), reply, NULL);
1612 dbus_message_unref (reply);
1614 dbus_g_connection_unref (context->connection);
1615 dbus_g_message_unref (context->message);
1621 * Send a error message for a given method invocation.
1622 * This function also frees the sending context.
1624 * @param context the method context
1625 * @param error the error to send.
1628 dbus_g_method_return_error (DBusGMethodInvocation *context, GError *error)
1631 reply = gerror_to_dbus_error_message (context->object, dbus_g_message_get_message (context->message), error);
1632 dbus_connection_send (dbus_g_connection_get_connection (context->connection), reply, NULL);
1633 dbus_message_unref (reply);
1637 /** @} */ /* end of public API */
1639 const char * _dbus_gobject_get_path (GObject *obj)
1641 return g_object_get_data (obj, "dbus_glib_object_path");
1644 #ifdef DBUS_BUILD_TESTS
1648 * @ingroup DBusGLibInternals
1649 * Unit test for GLib GObject integration ("skeletons")
1650 * @returns #TRUE on success.
1653 _dbus_gobject_test (const char *test_data_dir)
1656 static struct { const char *wincaps; const char *uscore; } name_pairs[] = {
1657 { "SetFoo", "set_foo" },
1659 { "GetFooBar", "get_foo_bar" },
1660 { "Hello", "hello" }
1662 /* Impossible-to-handle cases */
1663 /* { "FrobateUIHandler", "frobate_ui_handler" } */
1667 while (i < (int) G_N_ELEMENTS (name_pairs))
1672 uscore = _dbus_gutils_wincaps_to_uscore (name_pairs[i].wincaps);
1673 wincaps = uscore_to_wincaps (name_pairs[i].uscore);
1675 if (strcmp (uscore, name_pairs[i].uscore) != 0)
1677 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
1678 name_pairs[i].wincaps, name_pairs[i].uscore,
1683 if (strcmp (wincaps, name_pairs[i].wincaps) != 0)
1685 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
1686 name_pairs[i].uscore, name_pairs[i].wincaps,
1700 #endif /* DBUS_BUILD_TESTS */