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),
131 (Atom) 31, &actual_type, &actual_format,
132 &nitems, &leftover, &data);
134 dbus_error_init (&error);
138 g_warning ("AT-SPI: Accessibility bus not found - Using session bus.\n");
139 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
141 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
145 bus = dbus_connection_open (data, &error);
148 g_error ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
152 if (!dbus_bus_register (bus, &error))
153 g_error ("AT-SPI: Couldn't register with bus: %s\n");
160 /*---------------------------------------------------------------------------*/
163 register_application (SpiAppData *app)
165 DBusMessage *message;
166 DBusMessageIter iter;
168 const char *uname = NULL;
170 dbus_error_init (&error);
172 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
173 SPI_DBUS_PATH_REGISTRY,
174 SPI_DBUS_INTERFACE_REGISTRY,
175 "RegisterApplication");
176 dbus_message_set_no_reply (message, TRUE);
178 uname = dbus_bus_get_unique_name(app->bus);
181 g_error ("AT-SPI: Couldn't get unique name for this connection");
184 dbus_message_iter_init_append(message, &iter);
185 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
186 dbus_connection_send (app->bus, message, NULL);
187 if (message) dbus_message_unref (message);
190 /*---------------------------------------------------------------------------*/
193 deregister_application (SpiAppData *app)
195 DBusMessage *message;
196 DBusMessageIter iter;
200 dbus_error_init (&error);
202 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
203 SPI_DBUS_PATH_REGISTRY,
204 SPI_DBUS_INTERFACE_REGISTRY,
205 "DeregisterApplication");
206 dbus_message_set_no_reply (message, TRUE);
208 uname = dbus_bus_get_unique_name(app->bus);
210 dbus_message_iter_init_append(message, &iter);
211 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
212 dbus_connection_send (app->bus, message, NULL);
213 if (message) dbus_message_unref (message);
216 /*---------------------------------------------------------------------------*/
221 if (!atk_adaptor_app_data)
226 spi_atk_tidy_windows ();
227 spi_atk_deregister_event_listeners();
228 deregister_application (atk_adaptor_app_data);
230 g_free(atk_adaptor_app_data);
231 atk_adaptor_app_data = NULL;
233 /* Not currently creating an XDisplay */
236 XCloseDisplay (bridge_display);
240 /*---------------------------------------------------------------------------*/
242 #ifdef __ATK_PLUG_H__
243 static AtkPlugClass *plug_class;
244 static AtkSocketClass *socket_class;
247 get_plug_id (AtkPlug *plug)
249 const char *uname = dbus_bus_get_unique_name(atk_adaptor_app_data->bus);
251 GString *str = g_string_new (NULL);
253 path = atk_dbus_object_to_path (ATK_OBJECT(plug), TRUE);
254 g_string_printf (str, "%s:%s", uname, path);
256 return g_string_free (str, FALSE);
260 socket_embed_hook (AtkSocket *socket, gchar *plug_id)
262 AtkObject *accessible = ATK_OBJECT(socket);
263 gchar *plug_name, *plug_path;
265 /* Force registration */
266 gchar *path = atk_dbus_object_to_path (accessible, TRUE);
267 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
268 /* Let the plug know that it has been embedded */
269 plug_name = g_strdup (plug_id);
275 plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
278 DBusMessage *message;
279 *(plug_path++) = '\0';
280 message = dbus_message_new_method_call (plug_name, plug_path, "org.freedesktop.atspi.Accessible", "Embedded");
281 dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
282 dbus_connection_send (atk_adaptor_app_data->bus, message, NULL);
289 install_plug_hooks ()
293 data = g_type_class_ref (ATK_TYPE_PLUG);
294 plug_class = ATK_PLUG_CLASS (data);
295 data = g_type_class_ref (ATK_TYPE_SOCKET);
296 socket_class = ATK_SOCKET_CLASS (data);
297 plug_class->get_object_id = get_plug_id;
298 socket_class->embed = socket_embed_hook;
302 gchar *atspi_dbus_name = NULL;
303 static gboolean atspi_no_register = FALSE;
305 static GOptionEntry atspi_option_entries[] =
307 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
308 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
315 * The following needs to be initialized.
317 * - DRoute for routing message to their accessible objects.
318 * - Event handlers for emmitting signals on specific ATK events.
319 * - Application registration with the AT-SPI registry.
323 adaptor_init (gint *argc, gchar **argv[])
330 gchar *introspection_directory;
331 static gboolean inited = FALSE;
338 DRoutePath *treepath, *accpath;
340 root = atk_get_root ();
341 g_return_val_if_fail (root, 0);
343 /* Parse command line options */
344 opt = g_option_context_new(NULL);
345 g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
346 g_option_context_set_ignore_unknown_options(opt, TRUE);
347 if (!g_option_context_parse(opt, argc, argv, &err))
348 g_warning("AT-SPI Option parsing failed: %s\n", err->message);
350 /* Allocate global data and do ATK initializations */
351 atk_adaptor_app_data = g_new0 (SpiAppData, 1);
352 atk_misc = atk_misc_get_instance ();
353 atk_adaptor_app_data->root = root;
355 /* Set up D-Bus connection and register bus name */
356 dbus_error_init (&error);
357 atk_adaptor_app_data->bus = spi_atk_bridge_get_bus ();
358 if (!atk_adaptor_app_data->bus)
360 g_free(atk_adaptor_app_data);
361 atk_adaptor_app_data = NULL;
365 if (atspi_dbus_name != NULL)
367 if (dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
369 g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
373 g_print("AT-SPI D-Bus name requested but could not be allocated - %s\n", atspi_dbus_name);
377 dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
379 /* Get D-Bus introspection directory */
380 introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
381 if (introspection_directory == NULL)
382 introspection_directory = ATSPI_INTROSPECTION_PATH;
384 /* Register droute for routing AT-SPI messages */
385 atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
387 treepath = droute_add_one (atk_adaptor_app_data->droute,
388 "/org/freedesktop/atspi/tree",
391 accpath = droute_add_many (atk_adaptor_app_data->droute,
392 "/org/freedesktop/atspi/accessible",
394 (DRouteGetDatumFunction) atk_dbus_path_to_gobject);
396 /* Register all interfaces with droute and set up application accessible db */
397 spi_initialize_tree (treepath);
399 spi_initialize_accessible (accpath);
400 spi_initialize_application (accpath);
401 spi_initialize_action(accpath);
402 spi_initialize_collection (accpath);
403 spi_initialize_component (accpath);
404 spi_initialize_document (accpath);
405 spi_initialize_editabletext (accpath);
406 spi_initialize_hyperlink (accpath);
407 spi_initialize_hypertext (accpath);
408 spi_initialize_image (accpath);
409 spi_initialize_selection (accpath);
410 spi_initialize_table (accpath);
411 spi_initialize_text (accpath);
412 spi_initialize_value (accpath);
414 /* Initialize the AtkObject registration */
415 atk_dbus_initialize (atk_adaptor_app_data->root);
417 /* Register methods to send D-Bus signals on certain ATK events */
418 spi_atk_register_event_listeners ();
420 #ifdef __ATK_PLUG_H__
421 /* Hook our plug-and socket functions */
422 install_plug_hooks ();
425 /* Register this app by sending a signal out to AT-SPI registry daemon */
426 if (!atspi_no_register)
427 register_application (atk_adaptor_app_data);
429 g_atexit (exit_func);
434 /*---------------------------------------------------------------------------*/
437 gtk_module_init (gint *argc, gchar **argv[])
439 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
441 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
443 return adaptor_init (argc, argv);
449 gnome_accessibility_module_init (void)
451 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
453 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
455 adaptor_init (NULL, NULL);
457 if (g_getenv ("AT_SPI_DEBUG"))
459 g_print("Atk Accessibility bridge initialized\n");
465 gnome_accessibility_module_shutdown (void)
467 spi_atk_deregister_event_listeners ();
470 /*END------------------------------------------------------------------------*/