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.
28 #include "droute-pairhash.h"
30 #define CHUNKS_DEFAULT (512)
32 #define oom() g_error ("D-Bus out of memory, this message will fail anyway")
34 #if defined DROUTE_DEBUG
35 #define _DROUTE_DEBUG(format, args...) g_print (format , ## args)
37 #define _DROUTE_DEBUG(format, args...)
43 GPtrArray *registered_paths;
45 gchar *introspect_dir;
52 GPtrArray *interfaces;
54 GHashTable *properties;
57 DRouteGetDatumFunction get_datum;
60 /*---------------------------------------------------------------------------*/
62 typedef struct PropertyPair
64 DRoutePropertyFunction get;
65 DRoutePropertyFunction set;
68 /*---------------------------------------------------------------------------*/
70 static DBusHandlerResult
71 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data);
73 /*---------------------------------------------------------------------------*/
76 path_new (DRouteContext *cnx,
78 DRouteGetDatumFunction get_datum)
82 new_path = g_new0 (DRoutePath, 1);
84 new_path->chunks = g_string_chunk_new (CHUNKS_DEFAULT);
85 new_path->interfaces = g_ptr_array_new ();
87 new_path->methods = g_hash_table_new_full ((GHashFunc)str_pair_hash,
92 new_path->properties = g_hash_table_new_full ((GHashFunc)str_pair_hash,
97 new_path->user_data = user_data;
98 new_path->get_datum = get_datum;
104 path_free (DRoutePath *path, gpointer user_data)
106 g_string_chunk_free (path->chunks);
107 g_ptr_array_free (path->interfaces, TRUE);
108 g_hash_table_destroy (path->methods);
109 g_hash_table_destroy (path->properties);
113 path_get_datum (DRoutePath *path, const gchar *pathstr)
115 if (path->get_datum != NULL)
116 return (path->get_datum) (pathstr, path->user_data);
118 return path->user_data;
121 /*---------------------------------------------------------------------------*/
124 droute_new (DBusConnection *bus, const char *introspect_dir)
128 cnx = g_new0 (DRouteContext, 1);
130 cnx->registered_paths = g_ptr_array_new ();
131 cnx->introspect_dir = g_strdup(introspect_dir);
137 droute_free (DRouteContext *cnx)
139 g_ptr_array_foreach (cnx->registered_paths, (GFunc) path_free, NULL);
140 g_free (cnx->introspect_dir);
144 /*---------------------------------------------------------------------------*/
147 droute_get_bus (DRouteContext *cnx)
152 /*---------------------------------------------------------------------------*/
154 static DBusObjectPathVTable droute_vtable =
158 NULL, NULL, NULL, NULL
162 droute_add_one (DRouteContext *cnx,
166 DRoutePath *new_path;
169 new_path = path_new (cnx, (void *) data, NULL);
171 registered = dbus_connection_register_object_path (cnx->bus, path, &droute_vtable, new_path);
175 g_ptr_array_add (cnx->registered_paths, new_path);
180 droute_add_many (DRouteContext *cnx,
183 const DRouteGetDatumFunction get_datum)
185 DRoutePath *new_path;
187 new_path = path_new (cnx, (void *) data, get_datum);
189 if (!dbus_connection_register_fallback (cnx->bus, path, &droute_vtable, new_path))
192 g_ptr_array_add (cnx->registered_paths, new_path);
196 /*---------------------------------------------------------------------------*/
199 droute_path_add_interface(DRoutePath *path,
201 const DRouteMethod *methods,
202 const DRouteProperty *properties)
206 g_return_if_fail (name != NULL);
208 itf = g_string_chunk_insert (path->chunks, name);
209 g_ptr_array_add (path->interfaces, itf);
211 for (; methods != NULL && methods->name != NULL; methods++)
215 meth = g_string_chunk_insert (path->chunks, methods->name);
216 g_hash_table_insert (path->methods, str_pair_new (itf, meth), methods->func);
219 for (; properties != NULL && properties->name != NULL; properties++)
224 prop = g_string_chunk_insert (path->chunks, properties->name);
225 pair = g_new (PropertyPair, 1);
226 pair->get = properties->get;
227 pair->set = properties->set;
228 g_hash_table_insert (path->properties, str_pair_new (itf, prop), pair);
232 /*---------------------------------------------------------------------------*/
234 /* The data structures don't support an efficient implementation of GetAll
235 * and I don't really care.
238 impl_prop_GetAll (DBusMessage *message,
242 DBusMessageIter iter, iter_dict, iter_dict_entry;
245 GHashTableIter prop_iter;
251 void *datum = path_get_datum (path, pathstr);
253 dbus_error_init (&error);
254 if (!dbus_message_get_args
255 (message, &error, DBUS_TYPE_STRING, &iface, DBUS_TYPE_INVALID))
256 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
258 reply = dbus_message_new_method_return (message);
262 dbus_message_iter_init_append (reply, &iter);
263 if (!dbus_message_iter_open_container
264 (&iter, DBUS_TYPE_ARRAY, "{sv}", &iter_dict))
267 g_hash_table_iter_init (&prop_iter, path->properties);
268 while (g_hash_table_iter_next (&prop_iter, (gpointer*)&key, (gpointer*)&value))
270 if (!g_strcmp0 (key->one, iface))
274 if (!dbus_message_iter_open_container
275 (&iter_dict, DBUS_TYPE_DICT_ENTRY, NULL, &iter_dict_entry))
277 dbus_message_iter_append_basic (&iter_dict_entry, DBUS_TYPE_STRING,
279 (value->get) (&iter_dict_entry, datum);
280 if (!dbus_message_iter_close_container (&iter_dict, &iter_dict_entry))
285 if (!dbus_message_iter_close_container (&iter, &iter_dict))
291 impl_prop_GetSet (DBusMessage *message,
296 DBusMessage *reply = NULL;
300 PropertyPair *prop_funcs = NULL;
303 dbus_error_init (&error);
304 if (!dbus_message_get_args (message,
311 return dbus_message_new_error (message, DBUS_ERROR_FAILED, error.message);
313 _DROUTE_DEBUG ("DRoute (handle prop): %s|%s on %s\n", pair.one, pair.two, pathstr);
315 prop_funcs = (PropertyPair *) g_hash_table_lookup (path->properties, &pair);
317 return dbus_message_new_error (message, DBUS_ERROR_FAILED, "Property unavailable");
319 if (get && prop_funcs->get)
321 void *datum = path_get_datum (path, pathstr);
322 DBusMessageIter iter;
324 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
326 reply = dbus_message_new_method_return (message);
327 dbus_message_iter_init_append (reply, &iter);
328 if (!(prop_funcs->get) (&iter, datum))
330 dbus_message_unref (reply);
331 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Get failed");
334 else if (!get && prop_funcs->set)
336 void *datum = path_get_datum (path, pathstr);
337 DBusMessageIter iter;
339 _DROUTE_DEBUG ("DRoute (handle prop Get): %s|%s on %s\n", pair.one, pair.two, pathstr);
341 dbus_message_iter_init (message, &iter);
342 /* Skip the interface and property name */
343 dbus_message_iter_next(&iter);
344 dbus_message_iter_next(&iter);
345 (prop_funcs->set) (&iter, datum);
347 reply = dbus_message_new_method_return (message);
351 reply = dbus_message_new_error (message, DBUS_ERROR_FAILED, "Getter or setter unavailable");
357 static DBusHandlerResult
358 handle_properties (DBusConnection *bus,
359 DBusMessage *message,
363 const gchar *pathstr)
366 DBusHandlerResult result = DBUS_HANDLER_RESULT_HANDLED;
368 if (!g_strcmp0(member, "GetAll"))
369 reply = impl_prop_GetAll (message, path, pathstr);
370 else if (!g_strcmp0 (member, "Get"))
371 reply = impl_prop_GetSet (message, path, pathstr, TRUE);
372 else if (!g_strcmp0 (member, "Set"))
373 reply = impl_prop_GetSet (message, path, pathstr, FALSE);
375 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
379 dbus_connection_send (bus, reply, NULL);
380 dbus_message_unref (reply);
386 /*---------------------------------------------------------------------------*/
388 static const char *introspection_header =
389 "<?xml version=\"1.0\"?>\n";
391 static const char *introspection_node_element =
392 "<node name=\"%s\">\n";
394 static const char *introspection_footer =
398 append_interface (GString *str,
399 const gchar *interface,
400 const gchar *directory)
408 filename = g_build_filename (directory, interface, NULL);
410 if (g_file_get_contents (filename, &contents, &len, &err))
412 g_string_append_len (str, contents, len);
416 g_warning ("AT-SPI: Cannot find introspection XML file %s - %s",
417 filename, err->message);
421 g_string_append (str, "\n");
426 static DBusHandlerResult
427 handle_introspection (DBusConnection *bus,
428 DBusMessage *message,
432 const gchar *pathstr)
440 _DROUTE_DEBUG ("DRoute (handle introspection): %s\n", pathstr);
442 if (g_strcmp0 (member, "Introspect"))
443 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
445 output = g_string_new(introspection_header);
447 g_string_append_printf(output, introspection_node_element, pathstr);
449 for (i=0; i < path->interfaces->len; i++)
451 gchar *interface = (gchar *) g_ptr_array_index (path->interfaces, i);
452 append_interface(output, interface, path->cnx->introspect_dir);
455 g_string_append(output, introspection_footer);
456 final = g_string_free(output, FALSE);
458 reply = dbus_message_new_method_return (message);
461 dbus_message_append_args(reply, DBUS_TYPE_STRING, &final,
463 dbus_connection_send (bus, reply, NULL);
465 dbus_message_unref (reply);
467 return DBUS_HANDLER_RESULT_HANDLED;
470 /*---------------------------------------------------------------------------*/
472 static DBusHandlerResult
473 handle_other (DBusConnection *bus,
474 DBusMessage *message,
478 const gchar *pathstr)
480 gint result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
484 DBusMessage *reply = NULL;
489 _DROUTE_DEBUG ("DRoute (handle other): %s|%s on %s\n", member, iface, pathstr);
491 func = (DRouteFunction) g_hash_table_lookup (path->methods, &pair);
494 void *datum = path_get_datum (path, pathstr);
496 reply = (func) (bus, message, datum);
500 /* All D-Bus method calls must have a reply.
501 * If one is not provided presume that the call has a void
502 * return and no error has occured.
504 reply = dbus_message_new_method_return (message);
506 dbus_connection_send (bus, reply, NULL);
507 dbus_message_unref (reply);
508 result = DBUS_HANDLER_RESULT_HANDLED;
511 _DROUTE_DEBUG ("DRoute (handle other) (reply): type %d\n",
512 dbus_message_get_type(reply));
516 /*---------------------------------------------------------------------------*/
518 static DBusHandlerResult
519 handle_message (DBusConnection *bus, DBusMessage *message, void *user_data)
521 DRoutePath *path = (DRoutePath *) user_data;
522 const gchar *iface = dbus_message_get_interface (message);
523 const gchar *member = dbus_message_get_member (message);
524 const gint type = dbus_message_get_type (message);
525 const gchar *pathstr = dbus_message_get_path (message);
527 /* Check for basic reasons not to handle */
528 if (type != DBUS_MESSAGE_TYPE_METHOD_CALL ||
531 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
533 if (!strcmp (iface, "org.freedesktop.DBus.Properties"))
534 return handle_properties (bus, message, path, iface, member, pathstr);
536 if (!strcmp (iface, "org.freedesktop.DBus.Introspectable"))
537 return handle_introspection (bus, message, path, iface, member, pathstr);
539 return handle_other (bus, message, path, iface, member, pathstr);
542 /*---------------------------------------------------------------------------*/
545 droute_not_yet_handled_error (DBusMessage *message)
550 errmsg= g_strdup_printf (
551 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
552 dbus_message_get_member (message),
553 dbus_message_get_signature (message),
554 dbus_message_get_interface (message));
555 reply = dbus_message_new_error (message,
556 DBUS_ERROR_UNKNOWN_METHOD,
563 droute_out_of_memory_error (DBusMessage *message)
568 errmsg= g_strdup_printf (
569 "Method \"%s\" with signature \"%s\" on interface \"%s\" could not be processed due to lack of memory\n",
570 dbus_message_get_member (message),
571 dbus_message_get_signature (message),
572 dbus_message_get_interface (message));
573 reply = dbus_message_new_error (message,
574 DBUS_ERROR_NO_MEMORY,
581 droute_invalid_arguments_error (DBusMessage *message)
586 errmsg= g_strdup_printf (
587 "Method \"%s\" with signature \"%s\" on interface \"%s\" was supplied with invalid arguments\n",
588 dbus_message_get_member (message),
589 dbus_message_get_signature (message),
590 dbus_message_get_interface (message));
591 reply = dbus_message_new_error (message,
592 DBUS_ERROR_INVALID_ARGS,
598 /*END------------------------------------------------------------------------*/