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 /* Force registration */
264 gchar *path = atk_dbus_object_to_path (accessible, TRUE);
265 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
270 install_plug_hooks ()
274 data = g_type_class_ref (ATK_TYPE_PLUG);
275 plug_class = ATK_PLUG_CLASS (data);
276 data = g_type_class_ref (ATK_TYPE_SOCKET);
277 socket_class = ATK_SOCKET_CLASS (data);
278 plug_class->get_object_id = get_plug_id;
279 socket_class->embed = socket_embed_hook;
283 gchar *atspi_dbus_name = NULL;
284 static gboolean atspi_no_register = FALSE;
286 static GOptionEntry atspi_option_entries[] =
288 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
289 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
296 * The following needs to be initialized.
298 * - DRoute for routing message to their accessible objects.
299 * - Event handlers for emmitting signals on specific ATK events.
300 * - Application registration with the AT-SPI registry.
304 adaptor_init (gint *argc, gchar **argv[])
311 gchar *introspection_directory;
312 static gboolean inited = FALSE;
319 DRoutePath *treepath, *accpath;
321 root = atk_get_root ();
322 g_return_val_if_fail (root, 0);
324 /* Parse command line options */
325 opt = g_option_context_new(NULL);
326 g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
327 g_option_context_set_ignore_unknown_options(opt, TRUE);
328 if (!g_option_context_parse(opt, argc, argv, &err))
329 g_warning("AT-SPI Option parsing failed: %s\n", err->message);
331 /* Allocate global data and do ATK initializations */
332 atk_adaptor_app_data = g_new0 (SpiAppData, 1);
333 atk_misc = atk_misc_get_instance ();
334 atk_adaptor_app_data->root = root;
336 /* Set up D-Bus connection and register bus name */
337 dbus_error_init (&error);
338 atk_adaptor_app_data->bus = spi_atk_bridge_get_bus ();
339 if (!atk_adaptor_app_data->bus)
341 g_free(atk_adaptor_app_data);
342 atk_adaptor_app_data = NULL;
346 if (atspi_dbus_name != NULL)
348 if (dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
350 g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
354 g_print("AT-SPI D-Bus name requested but could not be allocated - %s\n", atspi_dbus_name);
358 dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
360 /* Get D-Bus introspection directory */
361 introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
362 if (introspection_directory == NULL)
363 introspection_directory = ATSPI_INTROSPECTION_PATH;
365 /* Register droute for routing AT-SPI messages */
366 atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
368 treepath = droute_add_one (atk_adaptor_app_data->droute,
369 "/org/freedesktop/atspi/tree",
372 accpath = droute_add_many (atk_adaptor_app_data->droute,
373 "/org/freedesktop/atspi/accessible",
375 (DRouteGetDatumFunction) atk_dbus_path_to_gobject);
377 /* Register all interfaces with droute and set up application accessible db */
378 spi_initialize_tree (treepath);
380 spi_initialize_accessible (accpath);
381 spi_initialize_application (accpath);
382 spi_initialize_action(accpath);
383 spi_initialize_collection (accpath);
384 spi_initialize_component (accpath);
385 spi_initialize_document (accpath);
386 spi_initialize_editabletext (accpath);
387 spi_initialize_hyperlink (accpath);
388 spi_initialize_hypertext (accpath);
389 spi_initialize_image (accpath);
390 spi_initialize_selection (accpath);
391 spi_initialize_table (accpath);
392 spi_initialize_text (accpath);
393 spi_initialize_value (accpath);
395 /* Initialize the AtkObject registration */
396 atk_dbus_initialize (atk_adaptor_app_data->root);
398 /* Register methods to send D-Bus signals on certain ATK events */
399 spi_atk_register_event_listeners ();
401 #ifdef __ATK_PLUG_H__
402 /* Hook our plug-and socket functions */
403 install_plug_hooks ();
406 /* Register this app by sending a signal out to AT-SPI registry daemon */
407 if (!atspi_no_register)
408 register_application (atk_adaptor_app_data);
410 g_atexit (exit_func);
415 /*---------------------------------------------------------------------------*/
418 gtk_module_init (gint *argc, gchar **argv[])
420 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
422 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
424 return adaptor_init (argc, argv);
430 gnome_accessibility_module_init (void)
432 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
434 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
436 adaptor_init (NULL, NULL);
438 if (g_getenv ("AT_SPI_DEBUG"))
440 g_print("Atk Accessibility bridge initialized\n");
446 gnome_accessibility_module_shutdown (void)
448 spi_atk_deregister_event_listeners ();
451 /*END------------------------------------------------------------------------*/