1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gobject.c Exporting a GObject remotely
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.0
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/dbus-glib.h>
26 #include <dbus/dbus-glib-lowlevel.h>
27 #include "dbus-gtest.h"
28 #include "dbus-gutils.h"
29 #include "dbus-gvalue.h"
33 * @addtogroup DBusGLibInternals
37 static GStaticMutex info_hash_mutex = G_STATIC_MUTEX_INIT;
38 static GHashTable *info_hash = NULL;
41 wincaps_to_uscore (const char *caps)
46 str = g_string_new (NULL);
50 if (g_ascii_isupper (*p))
53 (str->len < 2 || str->str[str->len-2] != '_'))
54 g_string_append_c (str, '_');
55 g_string_append_c (str, g_ascii_tolower (*p));
59 g_string_append_c (str, *p);
64 return g_string_free (str, FALSE);
68 uscore_to_wincaps (const char *uscore)
72 gboolean last_was_uscore;
74 last_was_uscore = TRUE;
76 str = g_string_new (NULL);
80 if (*p == '-' || *p == '_')
82 last_was_uscore = TRUE;
88 g_string_append_c (str, g_ascii_toupper (*p));
89 last_was_uscore = FALSE;
92 g_string_append_c (str, *p);
97 return g_string_free (str, FALSE);
101 gobject_unregister_function (DBusConnection *connection,
106 object = G_OBJECT (user_data);
113 gtype_to_dbus_type (GType type)
119 return DBUS_TYPE_BYTE;
122 return DBUS_TYPE_BOOLEAN;
124 /* long gets cut to 32 bits so the remote API is consistent
125 * on all architectures
130 return DBUS_TYPE_INT32;
133 return DBUS_TYPE_UINT32;
136 return DBUS_TYPE_INT64;
139 return DBUS_TYPE_UINT64;
143 return DBUS_TYPE_DOUBLE;
146 return DBUS_TYPE_STRING;
149 return DBUS_TYPE_INVALID;
154 introspect_properties (GObject *object, GString *xml)
157 unsigned int n_specs;
161 last_type = G_TYPE_INVALID;
162 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
165 for (i = 0; i < n_specs; i++ )
171 GParamSpec *spec = specs[i];
173 dbus_type = gtype_to_dbus_type (G_PARAM_SPEC_VALUE_TYPE (spec));
174 if (dbus_type == DBUS_TYPE_INVALID)
177 if (spec->owner_type != last_type)
179 if (last_type != G_TYPE_INVALID)
180 g_string_append (xml, " </interface>\n");
183 /* FIXME what should the namespace on the interface be in
184 * general? should people be able to set it for their
187 g_string_append (xml, " <interface name=\"org.gtk.objects.");
188 g_string_append (xml, g_type_name (spec->owner_type));
189 g_string_append (xml, "\">\n");
191 last_type = spec->owner_type;
194 can_set = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
195 (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
197 can_get = (spec->flags & G_PARAM_READABLE) != 0;
199 s = uscore_to_wincaps (spec->name);
203 g_string_append (xml, " <method name=\"set_");
204 g_string_append (xml, s);
205 g_string_append (xml, "\">\n");
207 g_string_append (xml, " <arg type=\"");
208 g_string_append (xml, _dbus_gutils_type_to_string (dbus_type));
209 g_string_append (xml, "\"/>\n");
214 g_string_append (xml, " <method name=\"get_");
215 g_string_append (xml, s);
216 g_string_append (xml, "\">\n");
218 g_string_append (xml, " <arg type=\"");
219 g_string_append (xml, _dbus_gutils_type_to_string (dbus_type));
220 g_string_append (xml, "\" direction=\"out\"/>\n");
226 if (last_type != G_TYPE_INVALID)
227 g_string_append (xml, " </interface>\n");
233 introspect_signals (GType type, GString *xml)
238 ids = g_signal_list_ids (type, &n_ids);
242 g_string_append (xml, " <interface name=\"org.gtk.objects.");
243 g_string_append (xml, g_type_name (type));
244 g_string_append (xml, "\">\n");
246 /* FIXME: recurse to parent types ? */
247 for (i = 0; i < n_ids; i++)
252 g_signal_query (ids[i], &query);
254 if (query.return_type)
255 continue; /* FIXME: these could be listed as methods ? */
257 g_string_append (xml, " <signal name=\"");
258 g_string_append (xml, query.signal_name);
259 g_string_append (xml, "\">\n");
261 for (arg = 0; arg < query.n_params; arg++)
263 int dbus_type = gtype_to_dbus_type (query.param_types[arg]);
265 g_string_append (xml, " <arg type=\"");
266 g_string_append (xml, _dbus_gutils_type_to_string (dbus_type));
267 g_string_append (xml, "\"/>\n");
270 g_string_append (xml, " </signal>\n");
273 g_string_append (xml, " </interface>\n");
276 static DBusHandlerResult
277 handle_introspect (DBusConnection *connection,
278 DBusMessage *message,
286 if (!dbus_connection_list_registered (connection,
287 dbus_message_get_path (message),
289 g_error ("Out of memory");
291 xml = g_string_new (NULL);
293 introspect_signals (G_OBJECT_TYPE (object), xml);
294 introspect_properties (object, xml);
296 g_string_append (xml, "<node>\n");
298 /* Append child nodes */
299 for (i = 0; children[i]; i++)
300 g_string_append_printf (xml, " <node name=\"%s\"/>\n",
303 /* Close the XML, and send it to the requesting app */
304 g_string_append (xml, "</node>\n");
306 ret = dbus_message_new_method_return (message);
308 g_error ("Out of memory");
310 dbus_message_append_args (message,
311 DBUS_TYPE_STRING, xml->str,
314 dbus_connection_send (connection, message, NULL);
315 dbus_message_unref (message);
317 g_string_free (xml, TRUE);
319 dbus_free_string_array (children);
321 return DBUS_HANDLER_RESULT_HANDLED;
325 set_object_property (DBusConnection *connection,
326 DBusMessage *message,
330 GValue value = { 0, };
332 DBusMessageIter iter;
334 dbus_message_iter_init (message, &iter);
336 /* The g_object_set_property() will transform some types, e.g. it
337 * will let you use a uchar to set an int property etc. Note that
338 * any error in value range or value conversion will just
339 * g_warning(). These GObject skels are not for secure applications.
341 if (dbus_gvalue_demarshal (&iter, &value))
343 g_object_set_property (object,
347 g_value_unset (&value);
349 ret = dbus_message_new_method_return (message);
351 g_error ("out of memory");
355 ret = dbus_message_new_error (message,
356 DBUS_ERROR_INVALID_ARGS,
357 "Argument's D-BUS type can't be converted to a GType");
359 g_error ("out of memory");
366 get_object_property (DBusConnection *connection,
367 DBusMessage *message,
374 DBusMessageIter iter;
376 value_type = G_PARAM_SPEC_VALUE_TYPE (pspec);
378 ret = dbus_message_new_method_return (message);
380 g_error ("out of memory");
382 g_value_init (&value, value_type);
383 g_object_get_property (object, pspec->name, &value);
385 value_type = G_VALUE_TYPE (&value);
387 dbus_message_append_iter_init (message, &iter);
389 if (!dbus_gvalue_marshal (&iter, &value))
391 dbus_message_unref (ret);
392 ret = dbus_message_new_error (message,
393 DBUS_ERROR_UNKNOWN_METHOD,
394 "Can't convert GType of object property to a D-BUS type");
400 static DBusHandlerResult
401 gobject_message_function (DBusConnection *connection,
402 DBusMessage *message,
405 const DBusGObjectInfo *info;
413 object = G_OBJECT (user_data);
415 if (dbus_message_is_method_call (message,
416 DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
418 return handle_introspect (connection, message, object);
420 member = dbus_message_get_member (message);
422 /* Try the metainfo, which lets us invoke methods */
424 g_static_mutex_lock (&info_hash_mutex);
425 /* FIXME this needs to walk up the inheritance tree, not
426 * just look at the most-derived class
428 info = g_hash_table_lookup (info_hash,
429 G_OBJECT_GET_CLASS (object));
430 g_static_mutex_unlock (&info_hash_mutex);
439 /* If no metainfo, we can still do properties and signals
440 * via standard GLib introspection
442 setter = (member[0] == 's' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
443 getter = (member[0] == 'g' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
445 if (!(setter || getter))
446 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
448 s = wincaps_to_uscore (&member[4]);
450 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
461 ret = set_object_property (connection, message,
466 ret = get_object_property (connection, message,
471 g_assert_not_reached ();
475 g_assert (ret != NULL);
477 dbus_connection_send (connection, ret, NULL);
478 dbus_message_unref (ret);
479 return DBUS_HANDLER_RESULT_HANDLED;
482 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
485 static DBusObjectPathVTable gobject_dbus_vtable = {
486 gobject_unregister_function,
487 gobject_message_function,
491 /** @} */ /* end of internals */
494 * @addtogroup DBusGLib
499 * Install introspection information about the given object class
500 * sufficient to allow methods on the object to be invoked by name.
501 * The introspection information is normally generated by
502 * dbus-glib-tool, then this function is called in the
503 * class_init() for the object class.
505 * Once introspection information has been installed, instances of the
506 * object registered with dbus_g_connection_register_g_object() can have
507 * their methods invoked remotely.
509 * @param object_class class struct of the object
510 * @param info introspection data generated by dbus-glib-tool
513 dbus_g_object_class_install_info (GObjectClass *object_class,
514 const DBusGObjectInfo *info)
516 g_return_if_fail (G_IS_OBJECT_CLASS (object_class));
518 g_static_mutex_lock (&info_hash_mutex);
520 if (info_hash == NULL)
522 info_hash = g_hash_table_new (NULL, NULL); /* direct hash */
525 g_hash_table_replace (info_hash, object_class, (void*) info);
527 g_static_mutex_unlock (&info_hash_mutex);
531 * Registers a GObject at the given path. Properties, methods, and signals
532 * of the object can then be accessed remotely. Methods are only available
533 * if method introspection data has been added to the object's class
534 * with g_object_class_install_info().
536 * The registration will be cancelled if either the DBusConnection or
537 * the GObject gets finalized.
539 * @param connection the D-BUS connection
540 * @param at_path the path where the object will live (the object's name)
541 * @param object the object
544 dbus_g_connection_register_g_object (DBusGConnection *connection,
548 g_return_if_fail (connection != NULL);
549 g_return_if_fail (at_path != NULL);
550 g_return_if_fail (G_IS_OBJECT (object));
552 if (!dbus_connection_register_object_path (DBUS_CONNECTION_FROM_G_CONNECTION (connection),
554 &gobject_dbus_vtable,
556 g_error ("Failed to register GObject with DBusConnection");
558 /* FIXME set up memory management (so we break the
559 * registration if object or connection vanishes)
563 /** @} */ /* end of public API */
565 #ifdef DBUS_BUILD_TESTS
569 * @ingroup DBusGLibInternals
570 * Unit test for GLib GObject integration ("skeletons")
571 * @returns #TRUE on success.
574 _dbus_gobject_test (const char *test_data_dir)
577 static struct { const char *wincaps; const char *uscore; } name_pairs[] = {
578 { "SetFoo", "set_foo" },
580 { "GetFooBar", "get_foo_bar" },
583 /* Impossible-to-handle cases */
584 /* { "FrobateUIHandler", "frobate_ui_handler" } */
588 while (i < (int) G_N_ELEMENTS (name_pairs))
593 uscore = wincaps_to_uscore (name_pairs[i].wincaps);
594 wincaps = uscore_to_wincaps (name_pairs[i].uscore);
596 if (strcmp (uscore, name_pairs[i].uscore) != 0)
598 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
599 name_pairs[i].wincaps, name_pairs[i].uscore,
604 if (strcmp (wincaps, name_pairs[i].wincaps) != 0)
606 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
607 name_pairs[i].uscore, name_pairs[i].wincaps,
621 #endif /* DBUS_BUILD_TESTS */