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>
43 #include "accessible-register.h"
44 #include "accessible-leasing.h"
45 #include "accessible-cache.h"
47 #include "common/spi-dbus.h"
50 * Provides the path for the introspection directory.
52 #if !defined ATSPI_INTROSPECTION_PATH
53 #error "No introspection XML directory defined"
56 /*---------------------------------------------------------------------------*/
58 SpiBridge *spi_global_app_data = NULL;
60 static const AtkMisc *atk_misc = NULL;
62 /*static Display *bridge_display = NULL;*/
64 /*---------------------------------------------------------------------------*/
67 * Returns a 'canonicalized' value for DISPLAY,
68 * with the screen number stripped off if present.
72 spi_display_name (void)
74 static const char *canonical_display_name = NULL;
75 if (!canonical_display_name)
77 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
80 display_env = g_getenv ("DISPLAY");
81 if (!display_env || !display_env[0])
82 canonical_display_name = ":0";
85 gchar *display_p, *screen_p;
86 canonical_display_name = g_strdup (display_env);
87 display_p = strrchr (canonical_display_name, ':');
88 screen_p = strrchr (canonical_display_name, '.');
89 if (screen_p && display_p && (screen_p > display_p))
97 canonical_display_name = display_env;
100 return canonical_display_name;
103 /*---------------------------------------------------------------------------*/
106 * Gets the IOR from the XDisplay.
107 * Not currently used in D-Bus version, but something similar
108 * may be employed in the future for accessing the registry daemon
112 static DBusConnection *
113 spi_atk_bridge_get_bus (void)
117 Display *bridge_display;
119 unsigned char *data = NULL;
120 unsigned long nitems;
121 unsigned long leftover;
123 DBusConnection *bus = NULL;
126 bridge_display = XOpenDisplay (spi_display_name ());
128 g_error ("AT_SPI: Could not get the display\n");
130 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
131 XGetWindowProperty (bridge_display,
132 XDefaultRootWindow (bridge_display),
134 (long) BUFSIZ, False,
135 (Atom) 31, &actual_type, &actual_format,
136 &nitems, &leftover, &data);
138 dbus_error_init (&error);
143 ("AT-SPI: Accessibility bus not found - Using session bus.\n");
144 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
146 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
150 bus = dbus_connection_open (data, &error);
153 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
157 if (!dbus_bus_register (bus, &error))
158 g_error ("AT-SPI: Couldn't register with bus: %s\n", error.message);
165 /*---------------------------------------------------------------------------*/
168 register_application (SpiBridge * app)
170 DBusMessage *message;
171 DBusMessageIter iter;
173 const char *uname = NULL;
175 dbus_error_init (&error);
177 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
178 SPI_DBUS_PATH_REGISTRY,
179 SPI_DBUS_INTERFACE_REGISTRY,
180 "RegisterApplication");
181 dbus_message_set_no_reply (message, TRUE);
183 uname = dbus_bus_get_unique_name (app->bus);
186 g_error ("AT-SPI: Couldn't get unique name for this connection");
189 dbus_message_iter_init_append (message, &iter);
190 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
191 dbus_connection_send (app->bus, message, NULL);
193 dbus_message_unref (message);
196 /*---------------------------------------------------------------------------*/
199 deregister_application (SpiBridge * app)
201 DBusMessage *message;
202 DBusMessageIter iter;
206 dbus_error_init (&error);
208 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
209 SPI_DBUS_PATH_REGISTRY,
210 SPI_DBUS_INTERFACE_REGISTRY,
211 "DeregisterApplication");
212 dbus_message_set_no_reply (message, TRUE);
214 uname = dbus_bus_get_unique_name (app->bus);
216 dbus_message_iter_init_append (message, &iter);
217 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
218 dbus_connection_send (app->bus, message, NULL);
220 dbus_message_unref (message);
223 /*---------------------------------------------------------------------------*/
228 if (!spi_global_app_data)
233 spi_atk_tidy_windows ();
234 spi_atk_deregister_event_listeners ();
235 deregister_application (spi_global_app_data);
237 g_free (spi_global_app_data);
238 spi_global_app_data = NULL;
240 /* Not currently creating an XDisplay */
243 XCloseDisplay (bridge_display);
247 /*---------------------------------------------------------------------------*/
249 #ifdef __ATK_PLUG_H__
250 static AtkPlugClass *plug_class;
251 static AtkSocketClass *socket_class;
254 get_plug_id (AtkPlug * plug)
256 const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
258 GString *str = g_string_new (NULL);
260 path = atk_dbus_object_to_path (ATK_OBJECT (plug), TRUE);
261 g_string_printf (str, "%s:%s", uname, path);
263 return g_string_free (str, FALSE);
267 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
269 AtkObject *accessible = ATK_OBJECT(socket);
270 gchar *plug_name, *plug_path;
272 /* Force registration */
273 gchar *path = atk_dbus_object_to_path (accessible, TRUE);
274 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
275 /* Let the plug know that it has been embedded */
276 plug_name = g_strdup (plug_id);
282 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
285 DBusMessage *message;
286 *(plug_path++) = '\0';
287 message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
288 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
289 dbus_connection_send (atk_adaptor_app_data->bus, message, NULL);
296 install_plug_hooks ()
300 data = g_type_class_ref (ATK_TYPE_PLUG);
301 plug_class = ATK_PLUG_CLASS (data);
302 data = g_type_class_ref (ATK_TYPE_SOCKET);
303 socket_class = ATK_SOCKET_CLASS (data);
304 plug_class->get_object_id = get_plug_id;
305 socket_class->embed = socket_embed_hook;
309 gchar *atspi_dbus_name = NULL;
310 static gboolean atspi_no_register = FALSE;
312 static GOptionEntry atspi_option_entries[] = {
313 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
314 "D-Bus bus name to register as", NULL},
315 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
316 "Do not register with Registry Daemon", NULL},
323 * The following needs to be initialized.
325 * - DRoute for routing message to their accessible objects.
326 * - Event handlers for emmitting signals on specific ATK events.
327 * - Application registration with the AT-SPI registry.
331 adaptor_init (gint * argc, gchar ** argv[])
338 gchar *introspection_directory;
339 static gboolean inited = FALSE;
346 DRoutePath *treepath, *accpath;
348 root = atk_get_root ();
349 g_return_val_if_fail (root, 0);
351 /* Parse command line options */
352 opt = g_option_context_new (NULL);
353 g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
354 g_option_context_set_ignore_unknown_options (opt, TRUE);
355 if (!g_option_context_parse (opt, argc, argv, &err))
356 g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
358 /* Allocate global data and do ATK initializations */
359 spi_global_app_data = g_new0 (SpiBridge, 1);
360 atk_misc = atk_misc_get_instance ();
361 spi_global_app_data->root = root;
363 /* Set up D-Bus connection and register bus name */
364 dbus_error_init (&error);
365 spi_global_app_data->bus = spi_atk_bridge_get_bus ();
366 if (!spi_global_app_data->bus)
368 g_free (spi_global_app_data);
369 spi_global_app_data = NULL;
373 if (atspi_dbus_name != NULL)
375 if (dbus_bus_request_name
376 (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
378 g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
383 ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
388 dbus_connection_setup_with_g_main (spi_global_app_data->bus,
389 g_main_context_default ());
391 /* Get D-Bus introspection directory */
392 introspection_directory = (char *) g_getenv ("ATSPI_INTROSPECTION_PATH");
393 if (introspection_directory == NULL)
394 introspection_directory = ATSPI_INTROSPECTION_PATH;
396 /* Register droute for routing AT-SPI messages */
397 spi_global_app_data->droute =
398 droute_new (spi_global_app_data->bus, introspection_directory);
400 treepath = droute_add_one (spi_global_app_data->droute,
401 "/org/at_spi/cache", NULL);
403 accpath = droute_add_many (spi_global_app_data->droute,
404 "/org/at_spi/accessible",
406 (DRouteGetDatumFunction)
407 spi_global_register_path_to_object);
410 * Create the leasing, register and cache objects.
411 * The order is important here, the cache depends on the
414 spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
415 spi_global_leasing = g_object_new (SPI_LEASING_TYPE, NULL);
416 spi_global_cache = g_object_new (SPI_CACHE_TYPE, NULL);
418 /* Register all interfaces with droute and set up application accessible db */
419 spi_initialize_cache (treepath);
420 spi_initialize_accessible (accpath);
421 spi_initialize_application (accpath);
422 spi_initialize_action (accpath);
423 spi_initialize_collection (accpath);
424 spi_initialize_component (accpath);
425 spi_initialize_document (accpath);
426 spi_initialize_editabletext (accpath);
427 spi_initialize_hyperlink (accpath);
428 spi_initialize_hypertext (accpath);
429 spi_initialize_image (accpath);
430 spi_initialize_selection (accpath);
431 spi_initialize_table (accpath);
432 spi_initialize_text (accpath);
433 spi_initialize_value (accpath);
435 /* Register methods to send D-Bus signals on certain ATK events */
436 spi_atk_register_event_listeners ();
438 #ifdef __ATK_PLUG_H__
439 /* Hook our plug-and socket functions */
440 install_plug_hooks ();
443 /* Register this app by sending a signal out to AT-SPI registry daemon */
444 if (!atspi_no_register)
445 register_application (spi_global_app_data);
447 g_atexit (exit_func);
452 /*---------------------------------------------------------------------------*/
455 gtk_module_init (gint * argc, gchar ** argv[])
457 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
459 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
461 return adaptor_init (argc, argv);
467 gnome_accessibility_module_init (void)
469 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
471 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
473 adaptor_init (NULL, NULL);
475 if (g_getenv ("AT_SPI_DEBUG"))
477 g_print ("Atk Accessibility bridge initialized\n");
483 gnome_accessibility_module_shutdown (void)
485 spi_atk_deregister_event_listeners ();
489 /*END------------------------------------------------------------------------*/