2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008 Novell, Inc.
6 * Copyright 2008 Codethink Ltd.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library 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 GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
29 #include "droute-pairhash.h"
31 #define CHUNKS_DEFAULT (512)
33 #define oom() g_error ("D-Bus out of memory, this message will fail anyway")
35 #if defined DROUTE_DEBUG
36 #define _DROUTE_DEBUG(format, args...) g_print (format , ## args)
38 #define _DROUTE_DEBUG(format, args...)
43 GPtrArray *registered_paths;
45 gchar *introspect_string;
54 GPtrArray *interfaces;
55 GPtrArray *introspection;
57 GHashTable *properties;
59 DRouteIntrospectChildrenFunction introspect_children_cb;
60 void *introspect_children_data;
62 DRouteGetDatumFunction get_datum;
65 /*---------------------------------------------------------------------------*/
67 typedef struct PropertyPair
69 DRoutePropertyFunction get;
70 DRoutePropertyFunction set;
73 /*---------------------------------------------------------------------------*/
75 static DBusHandlerResult
76 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data);
79 droute_object_does_not_exist_error (DBusMessage *message);
81 /*---------------------------------------------------------------------------*/
84 path_new (DRouteContext *cnx,
88 DRouteIntrospectChildrenFunction introspect_children_cb,
89 void *introspect_children_data,
90 DRouteGetDatumFunction get_datum)
94 new_path = g_new0 (DRoutePath, 1);
96 new_path->path = g_strdup (path);
97 new_path->prefix = prefix;
98 new_path->chunks = g_string_chunk_new (CHUNKS_DEFAULT);
99 new_path->interfaces = g_ptr_array_new ();
100 new_path->introspection = g_ptr_array_new ();
102 new_path->methods = g_hash_table_new_full ((GHashFunc)str_pair_hash,
107 new_path->properties = g_hash_table_new_full ((GHashFunc)str_pair_hash,
112 new_path->introspect_children_cb = introspect_children_cb;
113 new_path->introspect_children_data = introspect_children_data;
114 new_path->user_data = user_data;
115 new_path->get_datum = get_datum;
121 path_free (DRoutePath *path, gpointer user_data)
124 g_string_chunk_free (path->chunks);
125 g_ptr_array_free (path->interfaces, TRUE);
126 g_free(g_ptr_array_free (path->introspection, FALSE));
127 g_hash_table_destroy (path->methods);
128 g_hash_table_destroy (path->properties);
132 path_get_datum (DRoutePath *path, const gchar *pathstr)
134 if (path->get_datum != NULL)
135 return (path->get_datum) (pathstr, path->user_data);
137 return path->user_data;
140 /*---------------------------------------------------------------------------*/
147 cnx = g_new0 (DRouteContext, 1);
148 cnx->registered_paths = g_ptr_array_new ();
154 droute_free (DRouteContext *cnx)
156 g_ptr_array_foreach (cnx->registered_paths, (GFunc) path_free, NULL);
160 /*---------------------------------------------------------------------------*/
162 /*---------------------------------------------------------------------------*/
164 static DBusObjectPathVTable droute_vtable =
168 NULL, NULL, NULL, NULL
172 droute_add_one (DRouteContext *cnx,
176 DRoutePath *new_path;
178 new_path = path_new (cnx, path, FALSE, (void *)data, NULL, NULL, NULL);
180 g_ptr_array_add (cnx->registered_paths, new_path);
185 droute_add_many (DRouteContext *cnx,
188 DRouteIntrospectChildrenFunction introspect_children_cb,
189 void *introspect_children_data,
190 const DRouteGetDatumFunction get_datum)
192 DRoutePath *new_path;
194 new_path = path_new (cnx, path, TRUE, (void *) data,
195 introspect_children_cb, introspect_children_data,
198 g_ptr_array_add (cnx->registered_paths, new_path);
202 /*---------------------------------------------------------------------------*/
205 droute_path_add_interface(DRoutePath *path,
207 const char *introspect,
208 const DRouteMethod *methods,
209 const DRouteProperty *properties)
213 g_return_if_fail (name != NULL);
215 itf = g_string_chunk_insert (path->chunks, name);
216 g_ptr_array_add (path->interfaces, itf);
217 g_ptr_array_add (path->introspection, (gpointer) introspect);
219 for (; methods != NULL && methods->name != NULL; methods++)
223 meth = g_string_chunk_insert (path->chunks, methods->name);
224 g_hash_table_insert (path->methods, str_pair_new (itf, meth), methods->func);
227 for (; properties != NULL && properties->name != NULL; properties++)
232 prop = g_string_chunk_insert (path->chunks, properties->name);
233 pair = g_new (PropertyPair, 1);
234 pair->get = properties->get;
235 pair->set = properties->set;
236 g_hash_table_insert (path->properties, str_pair_new (itf, prop), pair);
240 /*---------------------------------------------------------------------------*/
242 /* The data structures don't support an efficient implementation of GetAll
243 * and I don't really care.
246 impl_prop_GetAll (DBusMessage *message,
250 DBusMessageIter iter, iter_dict, iter_dict_entry;
253 GHashTableIter prop_iter;
259 void *datum = path_get_datum (path, pathstr);
261 return droute_object_does_not_exist_error (message);
263 dbus_error_init (&error);
264 if (!dbus_message_get_args
265 (message, &error, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID))
266 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
268 reply = dbus_message_new_method_return (message);
272 dbus_message_iter_init_append (reply, &iter);
273 if (!dbus_message_iter_open_container
274 (&iter, DBUS_TYPE_ARRAY, "{sv}", &iter_dict))
277 g_hash_table_iter_init (&prop_iter, path->properties);
278 while (g_hash_table_iter_next (&prop_iter, (gpointer*)&key, (gpointer*)&value))
280 if (!g_strcmp0 (key->one, iface))
284 if (!dbus_message_iter_open_container
285 (&iter_dict, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict_entry))
287 dbus_message_iter_append_basic (&iter_dict_entry, DBUS_TYPE_STRING,
289 (value->get) (&iter_dict_entry, datum);
290 if (!dbus_message_iter_close_container (&iter_dict, &iter_dict_entry))
295 if (!dbus_message_iter_close_container (&iter, &iter_dict))
301 impl_prop_GetSet (DBusMessage *message,
306 DBusMessage *reply = NULL;
310 PropertyPair *prop_funcs = NULL;
314 dbus_error_init (&error);
315 if (!dbus_message_get_args (message,
322 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
324 _DROUTE_DEBUG ("DRoute (handle prop): %s|%s on %s\n", pair.one, pair.two, pathstr);
326 prop_funcs = (PropertyPair *) g_hash_table_lookup (path->properties, &pair);
328 #ifdef DBUS_ERROR_UNKNOWN_PROPERTY
329 return dbus_message_new_error (message, DBUS_ERROR_UNKNOWN_PROPERTY, "Property unavailable");
331 return dbus_message_new_error (message, DBUS_ERROR_FAILED, "Property unavailable");
334 datum = path_get_datum (path, pathstr);
336 return droute_object_does_not_exist_error (message);
338 if (get && prop_funcs->get)
341 DBusMessageIter iter;
343 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
345 reply = dbus_message_new_method_return (message);
346 dbus_message_iter_init_append (reply, &iter);
347 if (!(prop_funcs->get) (&iter, datum))
349 dbus_message_unref (reply);
350 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Get failed");
353 else if (!get && prop_funcs->set)
355 DBusMessageIter iter;
357 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
359 dbus_message_iter_init (message, &iter);
360 /* Skip the interface and property name */
361 dbus_message_iter_next(&iter);
362 dbus_message_iter_next(&iter);
363 (prop_funcs->set) (&iter, datum);
365 reply = dbus_message_new_method_return (message);
367 #ifdef DBUS_ERROR_PROPERTY_READ_ONLY
370 reply = dbus_message_new_error (message, DBUS_ERROR_PROPERTY_READ_ONLY, "Property is read-only");
375 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Getter or setter unavailable");
381 static DBusHandlerResult
382 handle_dbus (DBusConnection *bus,
383 DBusMessage *message,
386 const gchar *pathstr)
389 char *id_str = (char *) g_malloc(40);
392 if (strcmp (iface, DBUS_INTERFACE_DBUS) != 0 ||
393 strcmp (member, "Hello") != 0)
396 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
399 /* TODO: Fix this hack (we don't handle wrap-around, for instance) */
400 sprintf (id_str, ":1.%d", id++);
401 reply = dbus_message_new_method_return (message);
402 dbus_message_append_args (reply, DBUS_TYPE_STRING, &id_str, DBUS_TYPE_INVALID);
403 dbus_connection_send (bus, reply, NULL);
404 dbus_connection_flush (bus);
405 dbus_message_unref (reply);
407 return DBUS_HANDLER_RESULT_HANDLED;
410 static DBusHandlerResult
411 handle_properties (DBusConnection *bus,
412 DBusMessage *message,
416 const gchar *pathstr)
418 DBusMessage *reply = NULL;
419 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
421 if (!g_strcmp0(member, "GetAll"))
422 reply = impl_prop_GetAll (message, path, pathstr);
423 else if (!g_strcmp0 (member, "Get"))
424 reply = impl_prop_GetSet (message, path, pathstr, TRUE);
425 else if (!g_strcmp0 (member, "Set"))
426 reply = impl_prop_GetSet (message, path, pathstr, FALSE);
428 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
432 dbus_connection_send (bus, reply, NULL);
433 dbus_message_unref (reply);
439 /*---------------------------------------------------------------------------*/
441 static const char *introspection_header =
442 "<?xml version=\"1.0\"?>\n";
444 static const char *introspection_node_element =
445 "<node name=\"%s\">\n";
447 static const char *introspection_footer =
450 static DBusHandlerResult
451 handle_introspection (DBusConnection *bus,
452 DBusMessage *message,
456 const gchar *pathstr)
464 _DROUTE_DEBUG ("DRoute (handle introspection): %s\n", pathstr);
466 if (g_strcmp0 (member, "Introspect"))
467 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
469 output = g_string_new(introspection_header);
471 g_string_append_printf(output, introspection_node_element, pathstr);
473 if (!path->get_datum || path_get_datum (path, pathstr))
475 for (i=0; i < path->introspection->len; i++)
477 gchar *introspect = (gchar *) g_ptr_array_index (path->introspection, i);
478 g_string_append (output, introspect);
482 if (path->introspect_children_cb)
484 gchar *children = (*path->introspect_children_cb) (pathstr, path->introspect_children_data);
487 g_string_append (output, children);
492 g_string_append(output, introspection_footer);
493 final = g_string_free(output, FALSE);
495 reply = dbus_message_new_method_return (message);
498 dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
500 dbus_connection_send (bus, reply, NULL);
502 dbus_message_unref (reply);
504 return DBUS_HANDLER_RESULT_HANDLED;
507 /*---------------------------------------------------------------------------*/
509 static DBusHandlerResult
510 handle_other (DBusConnection *bus,
511 DBusMessage *message,
515 const gchar *pathstr)
517 gint result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
521 DBusMessage *reply = NULL;
528 _DROUTE_DEBUG ("DRoute (handle other): %s|%s on %s\n", member, iface, pathstr);
530 func = (DRouteFunction) g_hash_table_lookup (path->methods, &pair);
533 datum = path_get_datum (path, pathstr);
535 reply = droute_object_does_not_exist_error (message);
537 reply = (func) (bus, message, datum);
539 /* All D-Bus method calls must have a reply.
540 * If one is not provided presume that the caller has already
545 dbus_connection_send (bus, reply, NULL);
546 dbus_message_unref (reply);
548 result = DBUS_HANDLER_RESULT_HANDLED;
551 _DROUTE_DEBUG ("DRoute (handle other) (reply): type %d\n",
552 dbus_message_get_type(reply));
556 /*---------------------------------------------------------------------------*/
558 static DBusHandlerResult
559 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data)
561 DRoutePath *path = (DRoutePath *) user_data;
562 const gchar *iface = dbus_message_get_interface (message);
563 const gchar *member = dbus_message_get_member (message);
564 const gint type = dbus_message_get_type (message);
565 const gchar *pathstr = dbus_message_get_path (message);
567 DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
569 _DROUTE_DEBUG ("DRoute (handle message): %s|%s of type %d on %s\n", member, iface, type, pathstr);
571 /* Check for basic reasons not to handle */
572 if (type != DBUS_MESSAGE_TYPE_METHOD_CALL ||
577 if (!strcmp (pathstr, DBUS_PATH_DBUS))
578 result = handle_dbus (bus, message, iface, member, pathstr);
579 else if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
580 result = handle_properties (bus, message, path, iface, member, pathstr);
581 else if (!strcmp (iface, "org.freedesktop.DBus.Introspectable"))
582 result = handle_introspection (bus, message, path, iface, member, pathstr);
584 result = handle_other (bus, message, path, iface, member, pathstr);
586 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
587 g_print ("DRoute | Unhandled message: %s|%s of type %d on %s\n", member, iface, type, pathstr);
593 /*---------------------------------------------------------------------------*/
596 droute_object_does_not_exist_error (DBusMessage *message)
601 errmsg= g_strdup_printf (
602 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed as object %s does not exist\n",
603 dbus_message_get_member (message),
604 dbus_message_get_signature (message),
605 dbus_message_get_interface (message),
606 dbus_message_get_path (message));
607 #ifdef DBUS_ERROR_UNKNOWN_OBJECT
608 reply = dbus_message_new_error (message,
609 DBUS_ERROR_UNKNOWN_OBJECT,
612 reply = dbus_message_new_error (message,
620 /*---------------------------------------------------------------------------*/
623 droute_not_yet_handled_error (DBusMessage *message)
628 errmsg= g_strdup_printf (
629 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
630 dbus_message_get_member (message),
631 dbus_message_get_signature (message),
632 dbus_message_get_interface (message));
633 reply = dbus_message_new_error (message,
634 DBUS_ERROR_UNKNOWN_METHOD,
641 droute_out_of_memory_error (DBusMessage *message)
646 errmsg= g_strdup_printf (
647 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed due to lack of memory\n",
648 dbus_message_get_member (message),
649 dbus_message_get_signature (message),
650 dbus_message_get_interface (message));
651 reply = dbus_message_new_error (message,
652 DBUS_ERROR_NO_MEMORY,
659 droute_invalid_arguments_error (DBusMessage *message)
664 errmsg= g_strdup_printf (
665 "Method \"%s\" with signature \"%s\" on interface \"%s\" was supplied with invalid arguments\n",
666 dbus_message_get_member (message),
667 dbus_message_get_signature (message),
668 dbus_message_get_interface (message));
669 reply = dbus_message_new_error (message,
670 DBUS_ERROR_INVALID_ARGS,
677 droute_path_register (DRoutePath *path, DBusConnection *bus)
680 dbus_connection_register_fallback (bus, path->path, &droute_vtable, path);
682 dbus_connection_register_object_path (bus, path->path,
683 &droute_vtable, path);
687 droute_path_unregister (DRoutePath *path, DBusConnection *bus)
689 dbus_connection_unregister_object_path (bus, path->path);
693 droute_context_register (DRouteContext *cnx, DBusConnection *bus)
695 g_ptr_array_foreach (cnx->registered_paths, (GFunc) droute_path_register,
700 droute_context_unregister (DRouteContext *cnx, DBusConnection *bus)
702 g_ptr_array_foreach (cnx->registered_paths, (GFunc) droute_path_unregister,
707 droute_context_deregister (DRouteContext *cnx, DBusConnection *bus)
709 g_ptr_array_foreach (cnx->registered_paths, (GFunc) droute_path_unregister,
714 droute_intercept_dbus (DBusConnection *bus)
716 dbus_connection_register_object_path (bus, DBUS_PATH_DBUS,
717 &droute_vtable, NULL);
721 droute_unintercept_dbus (DBusConnection *bus)
723 dbus_connection_unregister_object_path (bus, DBUS_PATH_DBUS);
726 /*END------------------------------------------------------------------------*/