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 "spi-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 static gchar *atspi_dbus_name;
215 static gboolean atspi_no_register;
217 static GOptionEntry atspi_option_entries[] =
219 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
220 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
227 * The following needs to be initialized.
229 * - DRoute for routing message to their accessible objects.
230 * - Event handlers for emmitting signals on specific ATK events.
231 * - Application registration with the AT-SPI registry.
235 adaptor_init (gint *argc, gchar **argv[])
242 gchar *introspection_directory;
244 DRoutePath *treepath, *accpath;
246 root = atk_get_root ();
247 g_return_val_if_fail (root, 0);
249 /* Parse command line options */
250 opt = g_option_context_new(NULL);
251 g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
252 g_option_context_set_ignore_unknown_options(opt, TRUE);
253 if (!g_option_context_parse(opt, argc, argv, &err))
254 g_warning("AT-SPI Option parsing failed: %s\n", err->message);
256 /* Allocate global data and do ATK initializations */
257 atk_adaptor_app_data = g_new0 (SpiAppData, 1);
258 atk_misc = atk_misc_get_instance ();
259 atk_adaptor_app_data->root = root;
261 /* Set up D-Bus connection and register bus name */
262 dbus_error_init (&error);
263 atk_adaptor_app_data->bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
264 if (!atk_adaptor_app_data->bus)
266 g_warning ("AT-SPI Couldn't connect to D-Bus: %s\n", error.message);
267 g_free(atk_adaptor_app_data);
268 atk_adaptor_app_data = NULL;
271 if (atspi_dbus_name != NULL &&
272 dbus_bus_request_name(atk_adaptor_app_data->bus, atspi_dbus_name, 0, &error))
274 g_print("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
277 dbus_connection_setup_with_g_main(atk_adaptor_app_data->bus, g_main_context_default());
279 /* Get D-Bus introspection directory */
280 introspection_directory = (char *) g_getenv("ATSPI_INTROSPECTION_PATH");
281 if (introspection_directory == NULL)
282 introspection_directory = ATSPI_INTROSPECTION_PATH;
284 /* Register droute for routing AT-SPI messages */
285 atk_adaptor_app_data->droute = droute_new (atk_adaptor_app_data->bus, introspection_directory);
287 treepath = droute_add_one (atk_adaptor_app_data->droute,
288 "/org/freedesktop/atspi/tree",
291 accpath = droute_add_many (atk_adaptor_app_data->droute,
292 "/org/freedesktop/atspi/accessible",
294 (DRouteGetDatumFunction) atk_dbus_path_to_object);
296 /* Register all interfaces with droute and set up application accessible db */
297 spi_initialize_tree (treepath);
299 spi_initialize_accessible (accpath);
300 spi_initialize_application (accpath);
301 spi_initialize_action(accpath);
302 spi_initialize_collection (accpath);
303 spi_initialize_component (accpath);
304 spi_initialize_document (accpath);
305 spi_initialize_editabletext (accpath);
306 spi_initialize_hyperlink (accpath);
307 spi_initialize_hypertext (accpath);
308 spi_initialize_image (accpath);
309 spi_initialize_selection (accpath);
310 spi_initialize_table (accpath);
311 spi_initialize_text (accpath);
312 spi_initialize_value (accpath);
314 /* Register methods to send D-Bus signals on certain ATK events */
315 spi_atk_register_event_listeners ();
317 /* Initialize the AtkObject registration */
318 atk_dbus_initialize (atk_adaptor_app_data->root);
320 /* Register this app by sending a signal out to AT-SPI registry daemon */
321 register_application (atk_adaptor_app_data);
323 g_atexit (exit_func);
328 /*---------------------------------------------------------------------------*/
331 gtk_module_init (gint *argc, gchar **argv[])
333 return adaptor_init (argc, argv);
336 /*END------------------------------------------------------------------------*/