2 * AT-SPI - Assistive Technology Service Provider Interface
3 * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
5 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
6 * Copyright 2001, 2002, 2003 Ximian, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 #include "dbus/dbus-glib-lowlevel.h"
28 #include <X11/Xatom.h>
35 #include <atk/atkobject.h>
36 #include <atk/atknoopobject.h>
37 #include "accessible.h"
41 #undef SPI_BRIDGE_DEBUG
43 #define DBG(a,b) if(_dbg>=(a))b
46 static const char *registry = NULL;
47 SpiAppData *this_app = NULL;
48 static gboolean registry_died = FALSE;
49 static gint toplevels = 0;
50 static gboolean exiting = FALSE;
51 static AtkMisc *misc = NULL;
52 static gboolean during_init_shutdown = TRUE;
54 static const char *spi_atk_bridge_get_registry (void);
55 static char *device_event_controller = NULL;
56 static void spi_atk_bridge_register_application (const char *registry);
57 static gchar *spi_atk_bridge_get_registry_ior (void);
58 static gboolean spi_atk_bridge_do_registration (void);
59 static void spi_atk_bridge_toplevel_added (AtkObject *object,
62 static void spi_atk_bridge_toplevel_removed (AtkObject *object,
66 static void spi_atk_bridge_exit_func (void);
67 static void spi_atk_tidy_windows (void);
68 static void deregister_application (SpiAppData *app);
69 static void reinit_register_vars (void);
71 /* For automatic libgnome init */
72 extern void gnome_accessibility_module_init (void);
73 extern void gnome_accessibility_module_shutdown (void);
75 static int atk_bridge_initialized = FALSE;
76 static pid_t atk_bridge_pid = 0;
79 * These exported symbols are hooked by gnome-program
80 * to provide automatic module initialization and shutdown.
82 extern void gnome_accessibility_module_init (void);
83 extern void gnome_accessibility_module_shutdown (void);
86 spi_atk_register_event_listeners(void);
89 spi_atk_deregister_event_listeners (void);
92 post_init (gpointer data)
94 during_init_shutdown = FALSE;
98 static DBusObjectPathVTable droute_vtable =
102 NULL, NULL, NULL, NULL
105 static gchar* atspi_dbus_name;
106 static gboolean atspi_no_register;
108 static GOptionEntry atspi_option_entries[] =
110 {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name, "D-Bus bus name to register as", NULL},
111 {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register, "Do not register with Registry Daemon", NULL},
116 spi_app_init (AtkObject *root, gint *argc, gchar **argv[])
119 SpiAppData *ad = g_new0(SpiAppData, 1);
124 opt = g_option_context_new(NULL);
125 g_option_context_add_main_entries(opt, atspi_option_entries, NULL);
126 g_option_context_set_ignore_unknown_options(opt, TRUE);
127 if (!g_option_context_parse(opt, argc, argv, &err))
128 g_warning("Option parsing failed: %s\n", err->message);
130 dbus_error_init(&error);
132 ad->droute.bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
133 g_print("D-Bus unique name is : %s\n", dbus_bus_get_unique_name(ad->droute.bus));
137 g_warning("Couldn't connect to dbus: %s\n", error.message);
141 if (atspi_dbus_name != NULL && dbus_bus_request_name(ad->droute.bus,
146 g_print("\nRecieved D-Bus name - %s\n", atspi_dbus_name);
148 spi_register_tree_object(ad->droute.bus, &ad->droute, "/org/freedesktop/atspi/tree");
149 if (!dbus_connection_try_register_fallback (ad->droute.bus,
150 "/org/freedesktop/atspi/accessible",
155 g_warning("Couldn't register droute.\n");
160 dbus_connection_setup_with_g_main(ad->droute.bus, g_main_context_default());
162 atk_dbus_initialize (&ad->droute);
167 atk_bridge_init (gint *argc, gchar **argv[])
169 const char *debug_env_string = g_getenv ("AT_SPI_DEBUG");
171 gboolean success = FALSE;
173 if (atk_bridge_initialized)
177 atk_bridge_initialized = TRUE;
178 atk_bridge_pid = getpid ();
180 misc = atk_misc_get_instance();
182 if (g_getenv ("ATK_BRIDGE_REDIRECT_LOG"))
184 fname = g_strconcat ("/tmp/", g_get_prgname (), ".at-spi-log", NULL);
185 /* make sure we're not being redirected - security issue */
186 if (!g_file_test (fname, G_FILE_TEST_IS_SYMLINK))
187 freopen (fname, "w", stderr);
191 if (debug_env_string)
192 _dbg = (int) g_ascii_strtod (debug_env_string, NULL);
194 /* Connect to dbus */
195 this_app = spi_app_init (atk_get_root (), argc, argv);
198 * We only want to enable the bridge for top level
199 * applications, we detect bonobo components by seeing
200 * if they were activated with the intention of extracting
201 * an impl. by IID - very solid.
204 // TODO: Figure out if this is still needed
205 if (bonobo_activation_iid_get ())
210 DBG (1, g_message ("Found Bonobo component\n"));
211 g_signal_connect (atk_get_root (),
212 "children-changed::add",
213 (GCallback) spi_atk_bridge_toplevel_added,
215 g_signal_connect (atk_get_root (),
216 "children-changed::remove",
217 (GCallback) spi_atk_bridge_toplevel_removed,
219 /* in this case we redefine 'success' to mean 'registry is present' */
220 success = (spi_atk_bridge_get_registry () != NULL);
224 success = spi_atk_bridge_do_registration ();
227 * we must emit events even if we are not registered as a
228 * full-fledged app; See bugzilla #400709.
232 spi_atk_register_event_listeners ();
236 atk_bridge_initialized = FALSE;
238 g_idle_add (post_init, NULL);
244 spi_atk_bridge_do_registration (void)
246 if (spi_atk_bridge_get_registry () == NULL)
248 g_warning ("Could not locate registry");
252 /* Create the accessible application server object */
253 if (this_app == NULL)
254 this_app = spi_app_init (atk_get_root (), 0, NULL);
256 DBG (1, g_message ("About to register application\n"));
258 spi_atk_bridge_register_application (spi_atk_bridge_get_registry ());
260 g_atexit (spi_atk_bridge_exit_func);
262 DBG (1, g_message ("Application registered & listening\n"));
267 spi_atk_bridge_toplevel_added (AtkObject *object,
273 spi_atk_bridge_do_registration ();
279 spi_atk_bridge_toplevel_removed (AtkObject *object,
286 deregister_application (this_app);
287 reinit_register_vars ();
291 g_warning ("More toplevels removed than added\n");
297 spi_atk_bridge_register_application (const char *registry)
299 DBusMessage *message, *reply;
302 message = dbus_message_new_signal (SPI_DBUS_PATH_REGISTRY, SPI_DBUS_INTERFACE_TREE, "registerApplication");
303 dbus_error_init (&error);
304 dbus_connection_send (this_app->droute.bus, message, NULL);
305 if (error.message) g_print (error.message);
306 if (message) dbus_message_unref (message);
310 * Returns a 'canonicalized' value for DISPLAY,
311 * with the screen number stripped off if present.
314 spi_display_name (void)
316 static const char *canonical_display_name = NULL;
317 if (!canonical_display_name)
319 const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
322 display_env = g_getenv ("DISPLAY");
323 if (!display_env || !display_env[0])
324 canonical_display_name = ":0";
327 gchar *display_p, *screen_p;
328 canonical_display_name = g_strdup (display_env);
329 display_p = strrchr (canonical_display_name, ':');
330 screen_p = strrchr (canonical_display_name, '.');
331 if (screen_p && display_p && (screen_p > display_p))
339 canonical_display_name = display_env;
342 return canonical_display_name;
345 static Display *bridge_display = NULL;
348 spi_atk_bridge_get_registry_ior (void)
354 unsigned char *data = NULL;
355 unsigned long nitems;
356 unsigned long leftover;
358 bridge_display = XOpenDisplay (spi_display_name ());
360 AT_SPI_IOR = XInternAtom (bridge_display, "AT_SPI_IOR", False);
361 XGetWindowProperty(bridge_display,
362 XDefaultRootWindow (bridge_display),
365 (Atom) 31, &actual_type, &actual_format,
366 &nitems, &leftover, &data);
368 g_warning (_("AT_SPI_REGISTRY was not started at session startup."));
370 return (gchar *) data;
375 spi_atk_bridge_get_registry (void)
377 // TODO: check for registry dying, as the old code attempted to do
378 return "org.freedesktop.atspi.registry";
382 spi_atk_bridge_get_dec (void)
388 gtk_module_init (gint *argc, gchar **argv[])
390 //printf("Pointer to argc %x %x\n", (short) ((argc && 0xffff0000) >> 16), (short) (argc && 0xffff));
391 return atk_bridge_init (argc, argv);
395 deregister_application (SpiAppData *app)
397 const char *registry = spi_atk_bridge_get_registry ();
402 spi_atk_bridge_exit_func (void)
404 SpiAppData *app = (SpiAppData *) this_app;
406 DBG (1, g_message ("exiting bridge\n"));
412 if (atk_bridge_pid != getpid ())
417 during_init_shutdown = TRUE;
420 * Check whether we still have windows which have not been deleted.
422 spi_atk_tidy_windows ();
424 * FIXME: this may be incorrect for apps that do their own bonobo
425 * shutdown, until we can explicitly shutdown to get the ordering
429 deregister_application (this_app);
431 DBG (1, g_message ("bridge exit func complete.\n"));
433 if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
437 XCloseDisplay (bridge_display);
441 gnome_accessibility_module_init (void)
443 atk_bridge_init (0, NULL);
445 if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
447 g_print("Atk Accessibility bridge initialized\n");
452 gnome_accessibility_module_shutdown (void)
456 if (!atk_bridge_initialized)
460 during_init_shutdown = TRUE;
461 atk_bridge_initialized = FALSE;
463 if (g_getenv ("AT_BRIDGE_SHUTDOWN"))
465 g_print("Atk Accessibility bridge shutdown\n");
468 spi_atk_deregister_event_listeners();
470 deregister_application (this_app);
477 reinit_register_vars (void)
480 device_event_controller = NULL;