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>
35 #include <droute/droute.h>
39 #include "accessible-register.h"
42 #include "common/spi-dbus.h"
45 * Provides the path for the introspection directory.
47 #if !defined ATSPI_INTROSPECTION_PATH
48 #error "No introspection XML directory defined"
51 /*---------------------------------------------------------------------------*/
53 SpiAppData *atk_adaptor_app_data = NULL;
55 static const AtkMisc *atk_misc = NULL;
57 /*static Display *bridge_display = NULL;*/
59 /*---------------------------------------------------------------------------*/
62 * Returns a 'canonicalized' value for DISPLAY,
63 * with the screen number stripped off if present.
65 * Not currently used in D-Bus version but may be
66 * useful in future if we make use of XAtom.
70 spi_display_name (void)
72 static const char *canonical_display_name = NULL;
73 if (!canonical_display_name)
75 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
78 display_env = g_getenv ("DISPLAY");
79 if (!display_env || !display_env[0])
80 canonical_display_name = ":0";
83 gchar *display_p, *screen_p;
84 canonical_display_name = g_strdup (display_env);
85 display_p = strrchr (canonical_display_name, ':');
86 screen_p = strrchr (canonical_display_name, '.');
87 if (screen_p && display_p && (screen_p > display_p))
95 canonical_display_name = display_env;
98 return canonical_display_name;
102 /*---------------------------------------------------------------------------*/
105 * Gets the IOR from the XDisplay.
106 * Not currently used in D-Bus version, but something similar
107 * may be employed in the future for accessing the registry daemon
112 spi_atk_bridge_get_registry_ior (void)
117 unsigned char *data = NULL;
118 unsigned long nitems;
119 unsigned long leftover;
121 bridge_display = XOpenDisplay (spi_display_name ());
123 AT_SPI_IOR = XInternAtom (bridge_display, "AT_SPI_IOR", False);
124 XGetWindowProperty(bridge_display,
125 XDefaultRootWindow (bridge_display),
128 (Atom) 31, &actual_type, &actual_format,
129 &nitems, &leftover, &data);
131 g_warning (_("AT_SPI_REGISTRY was not started at session startup."));
132 return (gchar *) data;
136 /*---------------------------------------------------------------------------*/
139 register_application (SpiAppData *app)
141 DBusMessage *message;
142 DBusMessageIter iter;
146 dbus_error_init (&error);
148 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
149 SPI_DBUS_PATH_REGISTRY,
150 SPI_DBUS_INTERFACE_REGISTRY,
151 "RegisterApplication");
152 dbus_message_set_no_reply (message, TRUE);
154 uname = dbus_bus_get_unique_name(app->bus);
156 dbus_message_iter_init_append(message, &iter);
157 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
158 dbus_connection_send (app->bus, message, NULL);
159 if (message) dbus_message_unref (message);
162 /*---------------------------------------------------------------------------*/
165 deregister_application (SpiAppData *app)
167 DBusMessage *message;
168 DBusMessageIter iter;
172 dbus_error_init (&error);
174 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
175 SPI_DBUS_PATH_REGISTRY,
176 SPI_DBUS_INTERFACE_REGISTRY,
177 "DeregisterApplication");
178 dbus_message_set_no_reply (message, TRUE);
180 uname = dbus_bus_get_unique_name(app->bus);
182 dbus_message_iter_init_append(message, &iter);
183 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
184 dbus_connection_send (app->bus, message, NULL);
185 if (message) dbus_message_unref (message);
188 /*---------------------------------------------------------------------------*/
193 if (!atk_adaptor_app_data)
198 spi_atk_tidy_windows ();
199 spi_atk_deregister_event_listeners();
200 deregister_application (atk_adaptor_app_data);
202 g_free(atk_adaptor_app_data);
203 atk_adaptor_app_data = NULL;
205 /* Not currently creating an XDisplay */
208 XCloseDisplay (bridge_display);
212 /*---------------------------------------------------------------------------*/
214 #ifdef __ATK_PLUG_H__
215 static AtkPlugClass *plug_class;
216 static AtkSocketClass *socket_class;
219 get_plug_id (AtkPlug *plug)
221 const char *uname = dbus_bus_get_unique_name(atk_adaptor_app_data->bus);
223 GString *str = g_string_new (NULL);
225 path = atk_dbus_object_to_path (ATK_OBJECT(plug), TRUE);
226 g_string_printf (str, "%s:%s", uname, path);
228 return g_string_free (str, FALSE);
232 socket_embed_hook (AtkSocket *socket, gchar *plug_id)
234 AtkObject *accessible = ATK_OBJECT(socket);
235 /* Force registration */
236 gchar *path = atk_dbus_object_to_path (accessible, TRUE);
237 spi_emit_cache_update (accessible, atk_adaptor_app_data->bus);
242 install_plug_hooks ()
246 data = g_type_class_ref (ATK_TYPE_PLUG);
247 plug_class = ATK_PLUG_CLASS (data);
248 data = g_type_class_ref (ATK_TYPE_SOCKET);
249 socket_class = ATK_SOCKET_CLASS (data);
250 plug_class->get_object_id = get_plug_id;
251 socket_class->embed = socket_embed_hook;
255 static gchar *atspi_dbus_name;
256 static gboolean atspi_no_register;
258 static GOptionEntry atspi_option_entries[] =
260 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
261 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
268 * The following needs to be initialized.
270 * - DRoute for routing message to their accessible objects.
271 * - Event handlers for emmitting signals on specific ATK events.
272 * - Application registration with the AT-SPI registry.
276 adaptor_init (gint *argc, gchar **argv[])
283 gchar *introspection_directory;
284 static gboolean inited = FALSE;
291 DRoutePath *treepath, *accpath;
293 root = atk_get_root ();
294 g_return_val_if_fail (root, 0);
296 /* Parse command line options */
297 opt = g_option_context_new(NULL);
298 g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
299 g_option_context_set_ignore_unknown_options(opt, TRUE);
300 if (!g_option_context_parse(opt, argc, argv, &err))
301 g_warning("AT-SPI Option parsing failed: %s\n", err->message);
303 /* Allocate global data and do ATK initializations */
304 atk_adaptor_app_data = g_new0 (SpiAppData, 1);
305 atk_misc = atk_misc_get_instance ();
306 atk_adaptor_app_data->root = root;
308 /* Set up D-Bus connection and register bus name */
309 dbus_error_init (&error);
310 atk_adaptor_app_data->bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
311 if (!atk_adaptor_app_data->bus)
313 g_warning ("AT-SPI Couldn't connect to D-Bus: %s\n", error.message);
314 g_free(atk_adaptor_app_data);
315 atk_adaptor_app_data = NULL;
318 if (atspi_dbus_name != NULL &&
319 dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
321 g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
324 dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
326 /* Get D-Bus introspection directory */
327 introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
328 if (introspection_directory == NULL)
329 introspection_directory = ATSPI_INTROSPECTION_PATH;
331 /* Register droute for routing AT-SPI messages */
332 atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
334 treepath = droute_add_one (atk_adaptor_app_data->droute,
335 "/org/freedesktop/atspi/tree",
338 accpath = droute_add_many (atk_adaptor_app_data->droute,
339 "/org/freedesktop/atspi/accessible",
341 (DRouteGetDatumFunction) atk_dbus_path_to_gobject);
343 /* Register all interfaces with droute and set up application accessible db */
344 spi_initialize_tree (treepath);
346 spi_initialize_accessible (accpath);
347 spi_initialize_application (accpath);
348 spi_initialize_action(accpath);
349 spi_initialize_collection (accpath);
350 spi_initialize_component (accpath);
351 spi_initialize_document (accpath);
352 spi_initialize_editabletext (accpath);
353 spi_initialize_hyperlink (accpath);
354 spi_initialize_hypertext (accpath);
355 spi_initialize_image (accpath);
356 spi_initialize_selection (accpath);
357 spi_initialize_table (accpath);
358 spi_initialize_text (accpath);
359 spi_initialize_value (accpath);
361 /* Initialize the AtkObject registration */
362 atk_dbus_initialize (atk_adaptor_app_data->root);
364 /* Register methods to send D-Bus signals on certain ATK events */
365 spi_atk_register_event_listeners ();
367 #ifdef __ATK_PLUG_H__
368 /* Hook our plug-and socket functions */
369 install_plug_hooks ();
372 /* Register this app by sending a signal out to AT-SPI registry daemon */
373 register_application (atk_adaptor_app_data);
375 g_atexit (exit_func);
380 /*---------------------------------------------------------------------------*/
383 gtk_module_init (gint *argc, gchar **argv[])
385 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
387 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
389 return adaptor_init (argc, argv);
395 gnome_accessibility_module_init (void)
397 const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
399 if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
401 adaptor_init (NULL, NULL);
403 if (g_getenv ("AT_SPI_DEBUG"))
405 g_print("Atk Accessibility bridge initialized\n");
411 gnome_accessibility_module_shutdown (void)
413 spi_atk_deregister_event_listeners ();
416 /*END------------------------------------------------------------------------*/