2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008, 2009 Codethink Ltd.
6 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
7 * Copyright 2001, 2002, 2003 Ximian, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library 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 GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
26 #include "dbus/dbus-glib-lowlevel.h"
29 #include <X11/Xatom.h>
37 #include <droute/droute.h>
44 #include "accessible-register.h"
45 #include "accessible-leasing.h"
46 #include "accessible-cache.h"
48 #include "common/spi-dbus.h"
51 * Provides the path for the introspection directory.
53 #if !defined ATSPI_INTROSPECTION_PATH
54 #error "No introspection XML directory defined"
57 /*---------------------------------------------------------------------------*/
59 SpiBridge *spi_global_app_data = NULL;
61 static const AtkMisc *atk_misc = NULL;
63 /*static Display *bridge_display = NULL;*/
65 /*---------------------------------------------------------------------------*/
68 * Returns a 'canonicalized' value for DISPLAY,
69 * with the screen number stripped off if present.
73 spi_display_name (void)
75 static const char *canonical_display_name = NULL;
76 if (!canonical_display_name)
78 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
81 display_env = g_getenv ("DISPLAY");
82 if (!display_env || !display_env[0])
83 canonical_display_name = ":0";
86 gchar *display_p, *screen_p;
87 canonical_display_name = g_strdup (display_env);
88 display_p = strrchr (canonical_display_name, ':');
89 screen_p = strrchr (canonical_display_name, '.');
90 if (screen_p && display_p && (screen_p > display_p))
98 canonical_display_name = display_env;
101 return canonical_display_name;
104 /*---------------------------------------------------------------------------*/
107 * Gets the IOR from the XDisplay.
108 * Not currently used in D-Bus version, but something similar
109 * may be employed in the future for accessing the registry daemon
113 static DBusConnection *
114 spi_atk_bridge_get_bus (void)
118 Display *bridge_display;
120 unsigned char *data = NULL;
121 unsigned long nitems;
122 unsigned long leftover;
124 DBusConnection *bus = NULL;
127 bridge_display = XOpenDisplay (spi_display_name ());
129 g_error ("AT_SPI: Could not get the display\n");
131 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
132 XGetWindowProperty (bridge_display,
133 XDefaultRootWindow (bridge_display),
135 (long) BUFSIZ, False,
136 (Atom) 31, &actual_type, &actual_format,
137 &nitems, &leftover, &data);
139 dbus_error_init (&error);
144 ("AT-SPI: Accessibility bus not found - Using session bus.\n");
145 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
147 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
151 bus = dbus_connection_open (data, &error);
154 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
158 if (!dbus_bus_register (bus, &error))
159 g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
166 /*---------------------------------------------------------------------------*/
169 register_application (SpiBridge * app)
171 DBusMessage *message, *reply;
172 DBusMessageIter iter;
175 dbus_error_init (&error);
177 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
179 SPI_DBUS_INTERFACE_SOCKET,
182 dbus_message_iter_init_append (message, &iter);
183 spi_object_append_reference (&iter, app->root);
185 reply = dbus_connection_send_with_reply_and_block (app->bus, message, -1, &error);
188 dbus_message_unref (message);
192 DBusMessageIter iter, iter_struct;
193 gchar *app_name, *obj_path;
195 dbus_message_iter_init (reply, &iter);
196 dbus_message_iter_recurse (&iter, &iter_struct);
197 if (!(dbus_message_iter_get_arg_type (&iter_struct) == DBUS_TYPE_STRING))
198 g_error ("AT-SPI: Could not obtain desktop path or name\n");
199 dbus_message_iter_get_basic (&iter_struct, &app_name);
200 if (!dbus_message_iter_next (&iter_struct))
201 g_error ("AT-SPI: Could not obtain desktop name");
202 if (!(dbus_message_iter_get_arg_type (&iter_struct) == DBUS_TYPE_OBJECT_PATH))
203 g_error ("AT-SPI: Could not obtain desktop path");
204 dbus_message_iter_get_basic (&iter_struct, &obj_path);
206 app->desktop_name = g_strdup (app_name);
207 app->desktop_path = g_strdup (obj_path);
211 g_error ("AT-SPI: Could not embed inside desktop: %s\n", error.message);
216 /*---------------------------------------------------------------------------*/
219 deregister_application (SpiBridge * app)
221 DBusMessage *message;
222 DBusMessageIter iter;
226 dbus_error_init (&error);
228 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
229 SPI_DBUS_PATH_REGISTRY,
230 SPI_DBUS_INTERFACE_REGISTRY,
231 "DeregisterApplication");
232 dbus_message_set_no_reply (message, TRUE);
234 uname = dbus_bus_get_unique_name (app->bus);
236 dbus_message_iter_init_append (message, &iter);
237 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
238 dbus_connection_send (app->bus, message, NULL);
240 dbus_message_unref (message);
243 /*---------------------------------------------------------------------------*/
248 if (!spi_global_app_data)
253 spi_atk_tidy_windows ();
254 spi_atk_deregister_event_listeners ();
255 deregister_application (spi_global_app_data);
257 g_free (spi_global_app_data);
258 spi_global_app_data = NULL;
260 /* Not currently creating an XDisplay */
263 XCloseDisplay (bridge_display);
267 /*---------------------------------------------------------------------------*/
269 #ifdef SPI_ATK_PLUG_SOCKET
270 static AtkPlugClass *plug_class;
271 static AtkSocketClass *socket_class;
274 get_plug_id (AtkPlug * plug)
276 const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
278 GString *str = g_string_new (NULL);
280 path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
281 g_string_printf (str, "%s:%s", uname, path);
283 return g_string_free (str, FALSE);
287 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
289 AtkObject *accessible = ATK_OBJECT(socket);
290 gchar *plug_name, *plug_path;
292 /* Force registration */
293 gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
294 /* Let the plug know that it has been embedded */
295 plug_name = g_strdup (plug_id);
301 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
304 DBusMessage *message;
305 *(plug_path++) = '\0';
306 message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
307 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
308 dbus_connection_send (spi_global_app_data->bus, message, NULL);
315 install_plug_hooks ()
319 data = g_type_class_ref (ATK_TYPE_PLUG);
320 plug_class = ATK_PLUG_CLASS (data);
321 data = g_type_class_ref (ATK_TYPE_SOCKET);
322 socket_class = ATK_SOCKET_CLASS (data);
323 plug_class->get_object_id = get_plug_id;
324 socket_class->embed = socket_embed_hook;
328 gchar *atspi_dbus_name = NULL;
329 static gboolean atspi_no_register = FALSE;
331 static GOptionEntry atspi_option_entries[] = {
332 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
333 "D-Bus bus name to register as", NULL},
334 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
335 "Do not register with Registry Daemon", NULL},
342 * The following needs to be initialized.
344 * - DRoute for routing message to their accessible objects.
345 * - Event handlers for emmitting signals on specific ATK events.
346 * - Application registration with the AT-SPI registry.
350 adaptor_init (gint * argc, gchar ** argv[])
357 gchar *introspection_directory;
358 static gboolean inited = FALSE;
365 DRoutePath *treepath, *accpath;
367 root = atk_get_root ();
368 g_return_val_if_fail (root, 0);
370 /* Parse command line options */
371 opt = g_option_context_new (NULL);
372 g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
373 g_option_context_set_ignore_unknown_options (opt, TRUE);
374 if (!g_option_context_parse (opt, argc, argv, &err))
375 g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
377 /* Allocate global data and do ATK initializations */
378 spi_global_app_data = g_new0 (SpiBridge, 1);
379 atk_misc = atk_misc_get_instance ();
380 spi_global_app_data->root = g_object_ref (root);
382 /* Set up D-Bus connection and register bus name */
383 dbus_error_init (&error);
384 spi_global_app_data->bus = spi_atk_bridge_get_bus ();
385 if (!spi_global_app_data->bus)
387 g_free (spi_global_app_data);
388 spi_global_app_data = NULL;
392 if (atspi_dbus_name != NULL)
394 if (dbus_bus_request_name
395 (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
397 g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
402 ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
407 dbus_connection_setup_with_g_main (spi_global_app_data->bus,
408 g_main_context_default ());
411 * Create the leasing, register and cache objects.
412 * The order is important here, the cache depends on the
415 spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
416 spi_global_leasing = g_object_new (SPI_LEASING_TYPE, NULL);
417 spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
419 /* Get D-Bus introspection directory */
420 introspection_directory = (char *) g_getenv ("ATSPI_INTROSPECTION_PATH");
421 if (introspection_directory == NULL)
422 introspection_directory = ATSPI_INTROSPECTION_PATH;
424 /* Register droute for routing AT-SPI messages */
425 spi_global_app_data->droute =
426 droute_new (spi_global_app_data->bus, introspection_directory);
428 treepath = droute_add_one (spi_global_app_data->droute,
429 "/org/at_spi/cache", spi_global_cache);
431 accpath = droute_add_many (spi_global_app_data->droute,
432 "/org/freedesktop/atspi/accessible",
434 (DRouteGetDatumFunction)
435 spi_global_register_path_to_object);
438 /* Register all interfaces with droute and set up application accessible db */
439 spi_initialize_cache (treepath);
440 spi_initialize_accessible (accpath);
441 spi_initialize_application (accpath);
442 spi_initialize_action (accpath);
443 spi_initialize_collection (accpath);
444 spi_initialize_component (accpath);
445 spi_initialize_document (accpath);
446 spi_initialize_editabletext (accpath);
447 spi_initialize_hyperlink (accpath);
448 spi_initialize_hypertext (accpath);
449 spi_initialize_image (accpath);
450 spi_initialize_selection (accpath);
451 spi_initialize_table (accpath);
452 spi_initialize_text (accpath);
453 spi_initialize_value (accpath);
455 /* Register methods to send D-Bus signals on certain ATK events */
456 spi_atk_register_event_listeners ();
458 #ifdef SPI_ATK_PLUG_SOCKET
459 /* Hook our plug-and socket functions */
460 install_plug_hooks ();
463 /* Register this app by sending a signal out to AT-SPI registry daemon */
464 if (!atspi_no_register)
465 register_application (spi_global_app_data);
467 g_atexit (exit_func);
472 /*---------------------------------------------------------------------------*/
475 gtk_module_init (gint * argc, gchar ** argv[])
477 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
479 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
481 return adaptor_init (argc, argv);
487 gnome_accessibility_module_init (void)
489 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
491 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
493 adaptor_init (NULL, NULL);
495 if (g_getenv ("AT_SPI_DEBUG"))
497 g_print ("Atk Accessibility bridge initialized\n");
503 gnome_accessibility_module_shutdown (void)
505 spi_atk_deregister_event_listeners ();
509 /*END------------------------------------------------------------------------*/