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>
36 #include <droute/droute.h>
40 #include "accessible-register.h"
43 #include "common/spi-dbus.h"
46 * Provides the path for the introspection directory.
48 #if !defined ATSPI_INTROSPECTION_PATH
49 #error "No introspection XML directory defined"
52 /*---------------------------------------------------------------------------*/
54 SpiAppData *atk_adaptor_app_data = NULL;
56 static const AtkMisc *atk_misc = NULL;
58 /*static Display *bridge_display = NULL;*/
60 /*---------------------------------------------------------------------------*/
63 * Returns a 'canonicalized' value for DISPLAY,
64 * with the screen number stripped off if present.
68 spi_display_name (void)
70 static const char *canonical_display_name = NULL;
71 if (!canonical_display_name)
73 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
76 display_env = g_getenv ("DISPLAY");
77 if (!display_env || !display_env[0])
78 canonical_display_name = ":0";
81 gchar *display_p, *screen_p;
82 canonical_display_name = g_strdup (display_env);
83 display_p = strrchr (canonical_display_name, ':');
84 screen_p = strrchr (canonical_display_name, '.');
85 if (screen_p && display_p && (screen_p > display_p))
93 canonical_display_name = display_env;
96 return canonical_display_name;
99 /*---------------------------------------------------------------------------*/
102 * Gets the IOR from the XDisplay.
103 * Not currently used in D-Bus version, but something similar
104 * may be employed in the future for accessing the registry daemon
108 static DBusConnection *
109 spi_atk_bridge_get_bus (void)
113 Display *bridge_display;
115 unsigned char *data = NULL;
116 unsigned long nitems;
117 unsigned long leftover;
119 DBusConnection *bus = NULL;
122 bridge_display = XOpenDisplay (spi_display_name ());
124 g_error ("AT_SPI: Could not get the display\n");
126 AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
127 XGetWindowProperty (bridge_display,
128 XDefaultRootWindow (bridge_display),
130 (long) BUFSIZ, False,
131 (Atom) 31, &actual_type, &actual_format,
132 &nitems, &leftover, &data);
134 dbus_error_init (&error);
139 ("AT-SPI: Accessibility bus not found - Using session bus.\n");
140 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
142 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
146 bus = dbus_connection_open (data, &error);
149 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
153 if (!dbus_bus_register (bus, &error))
154 g_error ("AT-SPI: Couldn't register with bus: %s\n");
161 /*---------------------------------------------------------------------------*/
164 register_application (SpiAppData * app)
166 DBusMessage *message;
167 DBusMessageIter iter;
169 const char *uname = NULL;
171 dbus_error_init (&error);
173 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
174 SPI_DBUS_PATH_REGISTRY,
175 SPI_DBUS_INTERFACE_REGISTRY,
176 "RegisterApplication");
177 dbus_message_set_no_reply (message, TRUE);
179 uname = dbus_bus_get_unique_name (app->bus);
182 g_error ("AT-SPI: Couldn't get unique name for this connection");
185 dbus_message_iter_init_append (message, &iter);
186 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
187 dbus_connection_send (app->bus, message, NULL);
189 dbus_message_unref (message);
192 /*---------------------------------------------------------------------------*/
195 deregister_application (SpiAppData * app)
197 DBusMessage *message;
198 DBusMessageIter iter;
202 dbus_error_init (&error);
204 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
205 SPI_DBUS_PATH_REGISTRY,
206 SPI_DBUS_INTERFACE_REGISTRY,
207 "DeregisterApplication");
208 dbus_message_set_no_reply (message, TRUE);
210 uname = dbus_bus_get_unique_name (app->bus);
212 dbus_message_iter_init_append (message, &iter);
213 dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
214 dbus_connection_send (app->bus, message, NULL);
216 dbus_message_unref (message);
219 /*---------------------------------------------------------------------------*/
224 if (!atk_adaptor_app_data)
229 spi_atk_tidy_windows ();
230 spi_atk_deregister_event_listeners ();
231 deregister_application (atk_adaptor_app_data);
233 g_free (atk_adaptor_app_data);
234 atk_adaptor_app_data = NULL;
236 /* Not currently creating an XDisplay */
239 XCloseDisplay (bridge_display);
243 /*---------------------------------------------------------------------------*/
245 #ifdef __ATK_PLUG_H__
246 static AtkPlugClass *plug_class;
247 static AtkSocketClass *socket_class;
250 get_plug_id (AtkPlug * plug)
252 const char *uname = dbus_bus_get_unique_name (atk_adaptor_app_data->bus);
254 GString *str = g_string_new (NULL);
256 path = atk_dbus_object_to_path (ATK_OBJECT (plug), TRUE);
257 g_string_printf (str, "%s:%s", uname, path);
259 return g_string_free (str, FALSE);
263 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
265 AtkObject *accessible = ATK_OBJECT(socket);
266 gchar *plug_name, *plug_path;
268 /* Force registration */
269 gchar *path = atk_dbus_object_to_path (accessible, TRUE);
270 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
271 /* Let the plug know that it has been embedded */
272 plug_name = g_strdup (plug_id);
278 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
281 DBusMessage *message;
282 *(plug_path++) = '\0';
283 message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
284 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
285 dbus_connection_send (atk_adaptor_app_data->bus, message, NULL);
292 install_plug_hooks ()
296 data = g_type_class_ref (ATK_TYPE_PLUG);
297 plug_class = ATK_PLUG_CLASS (data);
298 data = g_type_class_ref (ATK_TYPE_SOCKET);
299 socket_class = ATK_SOCKET_CLASS (data);
300 plug_class->get_object_id = get_plug_id;
301 socket_class->embed = socket_embed_hook;
305 gchar *atspi_dbus_name = NULL;
306 static gboolean atspi_no_register = FALSE;
308 static GOptionEntry atspi_option_entries[] = {
309 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
310 "D-Bus bus name to register as", NULL},
311 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
312 "Do not register with Registry Daemon", NULL},
319 * The following needs to be initialized.
321 * - DRoute for routing message to their accessible objects.
322 * - Event handlers for emmitting signals on specific ATK events.
323 * - Application registration with the AT-SPI registry.
327 adaptor_init (gint * argc, gchar ** argv[])
334 gchar *introspection_directory;
335 static gboolean inited = FALSE;
342 DRoutePath *treepath, *accpath;
344 root = atk_get_root ();
345 g_return_val_if_fail (root, 0);
347 /* Parse command line options */
348 opt = g_option_context_new (NULL);
349 g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
350 g_option_context_set_ignore_unknown_options (opt, TRUE);
351 if (!g_option_context_parse (opt, argc, argv, &err))
352 g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
354 /* Allocate global data and do ATK initializations */
355 atk_adaptor_app_data = g_new0 (SpiAppData, 1);
356 atk_misc = atk_misc_get_instance ();
357 atk_adaptor_app_data->root = root;
359 /* Set up D-Bus connection and register bus name */
360 dbus_error_init (&error);
361 atk_adaptor_app_data->bus = spi_atk_bridge_get_bus ();
362 if (!atk_adaptor_app_data->bus)
364 g_free (atk_adaptor_app_data);
365 atk_adaptor_app_data = NULL;
369 if (atspi_dbus_name != NULL)
371 if (dbus_bus_request_name
372 (atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
374 g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
379 ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
384 dbus_connection_setup_with_g_main (atk_adaptor_app_data->bus,
385 g_main_context_default ());
387 /* Get D-Bus introspection directory */
388 introspection_directory = (char *) g_getenv ("ATSPI_INTROSPECTION_PATH");
389 if (introspection_directory == NULL)
390 introspection_directory = ATSPI_INTROSPECTION_PATH;
392 /* Register droute for routing AT-SPI messages */
393 atk_adaptor_app_data->droute =
394 droute_new (atk_adaptor_app_data->bus, introspection_directory);
396 treepath = droute_add_one (atk_adaptor_app_data->droute,
397 "/org/freedesktop/atspi/tree", NULL);
399 accpath = droute_add_many (atk_adaptor_app_data->droute,
400 "/org/freedesktop/atspi/accessible",
402 (DRouteGetDatumFunction)
403 atk_dbus_path_to_gobject);
405 /* Register all interfaces with droute and set up application accessible db */
406 spi_initialize_tree (treepath);
408 spi_initialize_accessible (accpath);
409 spi_initialize_application (accpath);
410 spi_initialize_action (accpath);
411 spi_initialize_collection (accpath);
412 spi_initialize_component (accpath);
413 spi_initialize_document (accpath);
414 spi_initialize_editabletext (accpath);
415 spi_initialize_hyperlink (accpath);
416 spi_initialize_hypertext (accpath);
417 spi_initialize_image (accpath);
418 spi_initialize_selection (accpath);
419 spi_initialize_table (accpath);
420 spi_initialize_text (accpath);
421 spi_initialize_value (accpath);
423 /* Initialize the AtkObject registration */
424 atk_dbus_initialize (atk_adaptor_app_data->root);
426 /* Register methods to send D-Bus signals on certain ATK events */
427 spi_atk_register_event_listeners ();
429 #ifdef __ATK_PLUG_H__
430 /* Hook our plug-and socket functions */
431 install_plug_hooks ();
434 /* Register this app by sending a signal out to AT-SPI registry daemon */
435 if (!atspi_no_register)
436 register_application (atk_adaptor_app_data);
438 g_atexit (exit_func);
443 /*---------------------------------------------------------------------------*/
446 gtk_module_init (gint * argc, gchar ** argv[])
448 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
450 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
452 return adaptor_init (argc, argv);
458 gnome_accessibility_module_init (void)
460 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
462 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
464 adaptor_init (NULL, NULL);
466 if (g_getenv ("AT_SPI_DEBUG"))
468 g_print ("Atk Accessibility bridge initialized\n");
474 gnome_accessibility_module_shutdown (void)
476 spi_atk_deregister_event_listeners ();
480 /*END------------------------------------------------------------------------*/