2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2008 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 <atk/atkobject.h>
37 #include <atk/atknoopobject.h>
39 #include <droute/droute.h>
45 #include "spi-common/spi-dbus.h"
48 * Provides the path for the introspection directory.
50 #if !defined ATSPI_INTROSPECTION_PATH
51 #error "No introspection XML directory defined"
54 /*---------------------------------------------------------------------------*/
56 SpiAppData *atk_adaptor_app_data = NULL;
58 static const AtkMisc *atk_misc = NULL;
60 /*static Display *bridge_display = NULL;*/
62 /*---------------------------------------------------------------------------*/
65 * Returns a 'canonicalized' value for DISPLAY,
66 * with the screen number stripped off if present.
68 * Not currently used in D-Bus version but may be
69 * useful in future if we make use of XAtom.
73 spi_display_name (void)
75 static const char *canonical_display_name = NULL;
76 if (!canonical_display_name)
78 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
81 display_env = g_getenv ("DISPLAY");
82 if (!display_env || !display_env[0])
83 canonical_display_name = ":0";
86 gchar *display_p, *screen_p;
87 canonical_display_name = g_strdup (display_env);
88 display_p = strrchr (canonical_display_name, ':');
89 screen_p = strrchr (canonical_display_name, '.');
90 if (screen_p && display_p && (screen_p > display_p))
98 canonical_display_name = display_env;
101 return canonical_display_name;
105 /*---------------------------------------------------------------------------*/
108 * Gets the IOR from the XDisplay.
109 * Not currently used in D-Bus version, but something similar
110 * may be employed in the future for accessing the registry daemon
115 spi_atk_bridge_get_registry_ior (void)
120 unsigned char *data = NULL;
121 unsigned long nitems;
122 unsigned long leftover;
124 bridge_display = XOpenDisplay (spi_display_name ());
126 AT_SPI_IOR = XInternAtom (bridge_display, "AT_SPI_IOR", False);
127 XGetWindowProperty(bridge_display,
128 XDefaultRootWindow (bridge_display),
131 (Atom) 31, &actual_type, &actual_format,
132 &nitems, &leftover, &data);
134 g_warning (_("AT_SPI_REGISTRY was not started at session startup."));
135 return (gchar *) data;
139 /*---------------------------------------------------------------------------*/
142 register_application (SpiAppData *app)
144 DBusMessage *message;
145 DBusMessageIter iter;
149 dbus_error_init (&error);
151 message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
152 SPI_DBUS_PATH_REGISTRY,
153 SPI_DBUS_INTERFACE_REGISTRY,
154 "registerApplication");
155 dbus_message_set_no_reply (message, TRUE);
157 uname = dbus_bus_get_unique_name(app->bus);
159 dbus_message_iter_init_append(message, &iter);
160 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &uname);
161 dbus_connection_send (app->bus, message, NULL);
162 if (message) dbus_message_unref (message);
165 /*---------------------------------------------------------------------------*/
168 deregister_application (SpiAppData *app)
170 DBusMessage *message;
171 DBusMessageIter iter;
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 "deregisterApplication");
181 dbus_message_set_no_reply (message, TRUE);
183 uname = dbus_bus_get_unique_name(app->bus);
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);
188 if (message) dbus_message_unref (message);
191 /*---------------------------------------------------------------------------*/
196 if (!atk_adaptor_app_data)
201 spi_atk_tidy_windows ();
202 spi_atk_deregister_event_listeners();
203 deregister_application (atk_adaptor_app_data);
205 g_free(atk_adaptor_app_data);
206 atk_adaptor_app_data = NULL;
208 /* Not currently creating an XDisplay */
211 XCloseDisplay (bridge_display);
215 /*---------------------------------------------------------------------------*/
217 static gchar *atspi_dbus_name;
218 static gboolean atspi_no_register;
220 static GOptionEntry atspi_option_entries[] =
222 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
223 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
230 * The following needs to be initialized.
232 * - DRoute for routing message to their accessible objects.
233 * - Event handlers for emmitting signals on specific ATK events.
234 * - Application registration with the AT-SPI registry.
238 adaptor_init (gint *argc, gchar **argv[])
244 gchar *introspection_directory;
246 DRoutePath *treepath, *accpath;
248 /* Parse command line options */
249 opt = g_option_context_new(NULL);
250 g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
251 g_option_context_set_ignore_unknown_options(opt, TRUE);
252 if (!g_option_context_parse(opt, argc, argv, &err))
253 g_warning("AT-SPI Option parsing failed: %s\n", err->message);
255 /* Allocate global data and do ATK initializations */
256 atk_adaptor_app_data = g_new0 (SpiAppData, 1);
257 atk_misc = atk_misc_get_instance ();
258 atk_adaptor_app_data->root = atk_get_root();
260 /* Set up D-Bus connection and register bus name */
261 dbus_error_init (&error);
262 atk_adaptor_app_data->bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
263 if (!atk_adaptor_app_data->bus)
265 g_warning ("AT-SPI Couldn't connect to D-Bus: %s\n", error.message);
266 g_free(atk_adaptor_app_data);
267 atk_adaptor_app_data = NULL;
270 if (atspi_dbus_name != NULL &&
271 dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
273 g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
276 dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
278 /* Get D-Bus introspection directory */
279 introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
280 if (introspection_directory == NULL)
281 introspection_directory = ATSPI_INTROSPECTION_PATH;
283 /* Register droute for routing AT-SPI messages */
284 atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
286 treepath = droute_add_one (atk_adaptor_app_data->droute,
287 "/org/freedesktop/atspi/tree",
290 accpath = droute_add_many (atk_adaptor_app_data->droute,
291 "/org/freedesktop/atspi/accessible",
293 (DRouteGetDatumFunction) atk_dbus_get_object);
295 /* Register all interfaces with droute and set up application accessible db */
296 spi_initialize_tree (treepath);
298 spi_initialize_accessible (accpath);
299 spi_initialize_action(accpath);
300 spi_initialize_collection (accpath);
301 spi_initialize_component (accpath);
302 spi_initialize_document (accpath);
303 spi_initialize_editabletext (accpath);
304 spi_initialize_hyperlink (accpath);
305 spi_initialize_hypertext (accpath);
306 spi_initialize_image (accpath);
307 spi_initialize_selection (accpath);
308 spi_initialize_table (accpath);
309 spi_initialize_text (accpath);
310 spi_initialize_value (accpath);
312 /* Register methods to send D-Bus signals on certain ATK events */
313 spi_atk_register_event_listeners ();
315 /* Initialize the AtkObject registration */
316 atk_dbus_initialize (atk_adaptor_app_data->root);
318 /* Register this app by sending a signal out to AT-SPI registry daemon */
319 register_application (atk_adaptor_app_data);
321 g_atexit (exit_func);
326 /*---------------------------------------------------------------------------*/
329 gtk_module_init (gint *argc, gchar **argv[])
331 return adaptor_init (argc, argv);
334 /*END------------------------------------------------------------------------*/