1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gobject.c Exporting a GObject remotely
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-glib.h"
26 #include "dbus-gtest.h"
30 * @addtogroup DBusGLibInternals
34 static GStaticMutex info_hash_mutex = G_STATIC_MUTEX_INIT;
35 static GHashTable *info_hash = NULL;
38 wincaps_to_uscore (const char *caps)
43 str = g_string_new (NULL);
47 if (g_ascii_isupper (*p))
50 (str->len < 2 || str->str[str->len-2] != '_'))
51 g_string_append_c (str, '_');
52 g_string_append_c (str, g_ascii_tolower (*p));
56 g_string_append_c (str, *p);
61 return g_string_free (str, FALSE);
65 uscore_to_wincaps (const char *uscore)
69 gboolean last_was_uscore;
71 last_was_uscore = TRUE;
73 str = g_string_new (NULL);
77 if (*p == '-' || *p == '_')
79 last_was_uscore = TRUE;
85 g_string_append_c (str, g_ascii_toupper (*p));
86 last_was_uscore = FALSE;
89 g_string_append_c (str, *p);
94 return g_string_free (str, FALSE);
98 gobject_unregister_function (DBusConnection *connection,
103 object = G_OBJECT (user_data);
109 gtype_to_dbus_type (GType type)
115 return DBUS_TYPE_BYTE;
118 return DBUS_TYPE_BOOLEAN;
120 /* long gets cut to 32 bits so the remote API is consistent
121 * on all architectures
126 return DBUS_TYPE_INT32;
129 return DBUS_TYPE_UINT32;
132 return DBUS_TYPE_INT64;
135 return DBUS_TYPE_UINT64;
139 return DBUS_TYPE_DOUBLE;
142 return DBUS_TYPE_STRING;
145 return DBUS_TYPE_INVALID;
150 dbus_type_to_string (int type)
154 case DBUS_TYPE_INVALID:
158 case DBUS_TYPE_BOOLEAN:
160 case DBUS_TYPE_INT32:
162 case DBUS_TYPE_UINT32:
164 case DBUS_TYPE_DOUBLE:
166 case DBUS_TYPE_STRING:
168 case DBUS_TYPE_NAMED:
170 case DBUS_TYPE_ARRAY:
179 static DBusHandlerResult
180 handle_introspect (DBusConnection *connection,
181 DBusMessage *message,
186 unsigned int n_specs;
191 xml = g_string_new (NULL);
193 g_string_append (xml, "<node>\n");
195 last_type = G_TYPE_INVALID;
197 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
203 GParamSpec *spec = specs[i];
209 dbus_type = gtype_to_dbus_type (G_PARAM_SPEC_VALUE_TYPE (spec));
210 if (dbus_type == DBUS_TYPE_INVALID)
213 if (spec->owner_type != last_type)
215 if (last_type != G_TYPE_INVALID)
216 g_string_append (xml, " </interface>\n");
219 /* FIXME what should the namespace on the interface be in
220 * general? should people be able to set it for their
224 g_string_append (xml, " <interface name=\"org.gtk.objects.");
225 g_string_append (xml, g_type_name (spec->owner_type));
226 g_string_append (xml, "\">\n");
228 last_type = spec->owner_type;
231 can_set = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
232 (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
234 can_get = (spec->flags & G_PARAM_READABLE) != 0;
236 s = uscore_to_wincaps (spec->name);
240 g_string_append (xml, " <method name=\"set_");
241 g_string_append (xml, s);
242 g_string_append (xml, "\">\n");
244 g_string_append (xml, " <arg type=\"");
245 g_string_append (xml, dbus_type_to_string (dbus_type));
246 g_string_append (xml, "\"/>\n");
251 g_string_append (xml, " <method name=\"get_");
252 g_string_append (xml, s);
253 g_string_append (xml, "\">\n");
255 g_string_append (xml, " <arg type=\"");
256 g_string_append (xml, dbus_type_to_string (dbus_type));
257 g_string_append (xml, "\" direction=\"out\"/>\n");
266 if (last_type != G_TYPE_INVALID)
267 g_string_append (xml, " </interface>\n");
271 /* Close the XML, and send it to the requesting app */
273 g_string_append (xml, "</node>\n");
275 ret = dbus_message_new_method_return (message);
277 g_error ("out of memory");
279 dbus_message_append_args (message,
280 DBUS_TYPE_STRING, xml->str,
283 dbus_connection_send (connection, message, NULL);
284 dbus_message_unref (message);
286 g_string_free (xml, TRUE);
288 return DBUS_HANDLER_RESULT_HANDLED;
292 set_object_property (DBusConnection *connection,
293 DBusMessage *message,
298 DBusMessageIter iter;
303 dbus_message_iter_init (message, &iter);
304 type = dbus_message_get_type (message);
313 b = dbus_message_iter_get_byte (&iter);
315 g_value_init (&value, G_TYPE_UCHAR);
317 g_value_set_uchar (&value, b);
320 case DBUS_TYPE_BOOLEAN:
324 b = dbus_message_iter_get_boolean (&iter);
326 g_value_init (&value, G_TYPE_BOOLEAN);
328 g_value_set_boolean (&value, b);
331 case DBUS_TYPE_INT32:
335 i = dbus_message_iter_get_int32 (&iter);
337 g_value_init (&value, G_TYPE_INT);
339 g_value_set_int (&value, i);
342 case DBUS_TYPE_UINT32:
346 i = dbus_message_iter_get_uint32 (&iter);
348 g_value_init (&value, G_TYPE_UINT);
350 g_value_set_uint (&value, i);
353 case DBUS_TYPE_INT64:
357 i = dbus_message_iter_get_int64 (&iter);
359 g_value_init (&value, G_TYPE_INT64);
361 g_value_set_int64 (&value, i);
364 case DBUS_TYPE_UINT64:
368 i = dbus_message_iter_get_uint64 (&iter);
370 g_value_init (&value, G_TYPE_UINT64);
372 g_value_set_uint64 (&value, i);
375 case DBUS_TYPE_DOUBLE:
379 d = dbus_message_iter_get_double (&iter);
381 g_value_init (&value, G_TYPE_DOUBLE);
383 g_value_set_double (&value, d);
386 case DBUS_TYPE_STRING:
390 /* FIXME use a const string accessor */
392 s = dbus_message_iter_get_string (&iter);
394 g_value_init (&value, G_TYPE_STRING);
396 g_value_set_string (&value, s);
402 /* FIXME array and other types, especially byte array
403 * converted to G_TYPE_STRING
411 /* The g_object_set_property() will transform some types, e.g. it
412 * will let you use a uchar to set an int property etc. Note that
413 * any error in value range or value conversion will just
414 * g_warning(). These GObject skels are not for secure applications.
419 g_object_set_property (object,
423 ret = dbus_message_new_method_return (message);
425 g_error ("out of memory");
427 g_value_unset (&value);
431 ret = dbus_message_new_error (message,
432 DBUS_ERROR_INVALID_ARGS,
433 "Argument's D-BUS type can't be converted to a GType");
435 g_error ("out of memory");
442 get_object_property (DBusConnection *connection,
443 DBusMessage *message,
451 DBusMessageIter iter;
453 value_type = G_PARAM_SPEC_VALUE_TYPE (pspec);
455 ret = dbus_message_new_method_return (message);
457 g_error ("out of memory");
460 g_value_init (&value, value_type);
461 g_object_get_property (object, pspec->name, &value);
463 value_type = G_VALUE_TYPE (&value);
465 dbus_message_append_iter_init (message, &iter);
470 dbus_message_iter_append_byte (&iter,
471 g_value_get_char (&value));
474 dbus_message_iter_append_byte (&iter,
475 g_value_get_uchar (&value));
478 dbus_message_iter_append_boolean (&iter,
479 g_value_get_boolean (&value));
482 dbus_message_iter_append_int32 (&iter,
483 g_value_get_int (&value));
486 dbus_message_iter_append_uint32 (&iter,
487 g_value_get_uint (&value));
489 /* long gets cut to 32 bits so the remote API is consistent
490 * on all architectures
493 dbus_message_iter_append_int32 (&iter,
494 g_value_get_long (&value));
497 dbus_message_iter_append_uint32 (&iter,
498 g_value_get_ulong (&value));
501 dbus_message_iter_append_int64 (&iter,
502 g_value_get_int64 (&value));
505 dbus_message_iter_append_uint64 (&iter,
506 g_value_get_uint64 (&value));
509 dbus_message_iter_append_double (&iter,
510 g_value_get_float (&value));
513 dbus_message_iter_append_double (&iter,
514 g_value_get_double (&value));
517 /* FIXME, the GValue string may not be valid UTF-8 */
518 dbus_message_iter_append_string (&iter,
519 g_value_get_string (&value));
526 g_value_unset (&value);
530 dbus_message_unref (ret);
531 ret = dbus_message_new_error (message,
532 DBUS_ERROR_UNKNOWN_METHOD,
533 "Can't convert GType of object property to a D-BUS type");
539 static DBusHandlerResult
540 gobject_message_function (DBusConnection *connection,
541 DBusMessage *message,
544 const DBusGObjectInfo *info;
552 object = G_OBJECT (user_data);
554 if (dbus_message_is_method_call (message,
555 DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
557 return handle_introspect (connection, message, object);
559 member = dbus_message_get_member (message);
561 /* Try the metainfo, which lets us invoke methods */
563 g_static_mutex_lock (&info_hash_mutex);
564 /* FIXME this needs to walk up the inheritance tree, not
565 * just look at the most-derived class
567 info = g_hash_table_lookup (info_hash,
568 G_OBJECT_GET_CLASS (object));
569 g_static_mutex_unlock (&info_hash_mutex);
578 /* If no metainfo, we can still do properties and signals
579 * via standard GLib introspection
581 setter = (member[0] == 's' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
582 getter = (member[0] == 'g' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
584 if (!(setter || getter))
585 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
587 s = wincaps_to_uscore (&member[4]);
589 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
600 ret = set_object_property (connection, message,
605 ret = get_object_property (connection, message,
610 g_assert_not_reached ();
614 g_assert (ret != NULL);
616 dbus_connection_send (connection, ret, NULL);
617 dbus_message_unref (ret);
618 return DBUS_HANDLER_RESULT_HANDLED;
621 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
624 static DBusObjectPathVTable gobject_dbus_vtable = {
625 gobject_unregister_function,
626 gobject_message_function,
630 /** @} */ /* end of internals */
633 * @addtogroup DBusGLib
638 * Install introspection information about the given object class
639 * sufficient to allow methods on the object to be invoked by name.
640 * The introspection information is normally generated by
641 * dbus-glib-tool, then this function is called in the
642 * class_init() for the object class.
644 * Once introspection information has been installed, instances of the
645 * object registered with dbus_connection_register_gobject() can have
646 * their methods invoked remotely.
648 * @param object_class class struct of the object
649 * @param info introspection data generated by dbus-glib-tool
652 dbus_gobject_class_install_info (GObjectClass *object_class,
653 const DBusGObjectInfo *info)
655 g_return_if_fail (G_IS_OBJECT_CLASS (object_class));
657 g_static_mutex_lock (&info_hash_mutex);
659 if (info_hash == NULL)
661 info_hash = g_hash_table_new (NULL, NULL); /* direct hash */
664 g_hash_table_replace (info_hash, object_class, (void*) info);
666 g_static_mutex_unlock (&info_hash_mutex);
670 split_path (const char *path)
688 split = g_new0 (char*, n_components + 1);
698 while (j < len && path[j] != '/')
701 /* Now [i, j) is the path component */
703 g_assert (path[i] != '/');
704 g_assert (j == len || path[j] == '/');
706 split[comp] = g_strndup (&path[i], j - i + 1);
708 split[comp][j-i] = '\0';
719 * Registers a GObject at the given path. Properties, methods, and signals
720 * of the object can then be accessed remotely. Methods are only available
721 * if method introspection data has been added to the object's class
722 * with g_object_class_install_info().
724 * The registration will be cancelled if either the DBusConnection or
725 * the GObject gets finalized.
727 * @param connection the D-BUS connection
728 * @param at_path the path where the object will live (the object's name)
729 * @param object the object
732 dbus_connection_register_gobject (DBusConnection *connection,
738 g_return_if_fail (connection != NULL);
739 g_return_if_fail (at_path != NULL);
740 g_return_if_fail (G_IS_OBJECT (object));
742 split = split_path (at_path);
744 if (!dbus_connection_register_object_path (connection,
745 (const char**) split,
746 &gobject_dbus_vtable,
748 g_error ("Failed to register GObject with DBusConnection");
752 /* FIXME set up memory management (so we break the
753 * registration if object or connection vanishes)
757 /** @} */ /* end of public API */
759 #ifdef DBUS_BUILD_TESTS
763 * @ingroup DBusGLibInternals
764 * Unit test for GLib GObject integration ("skeletons")
765 * @returns #TRUE on success.
768 _dbus_gobject_test (const char *test_data_dir)
771 static struct { const char *wincaps; const char *uscore; } name_pairs[] = {
772 { "SetFoo", "set_foo" },
774 { "GetFooBar", "get_foo_bar" },
777 /* Impossible-to-handle cases */
778 /* { "FrobateUIHandler", "frobate_ui_handler" } */
782 while (i < (int) G_N_ELEMENTS (name_pairs))
787 uscore = wincaps_to_uscore (name_pairs[i].wincaps);
788 wincaps = uscore_to_wincaps (name_pairs[i].uscore);
790 if (strcmp (uscore, name_pairs[i].uscore) != 0)
792 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
793 name_pairs[i].wincaps, name_pairs[i].uscore,
798 if (strcmp (wincaps, name_pairs[i].wincaps) != 0)
800 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
801 name_pairs[i].uscore, name_pairs[i].wincaps,
815 #endif /* DBUS_BUILD_TESTS */