Don't leak remove_data in remove_events
[platform/core/uifw/at-spi2-atk.git] / atk-adaptor / bridge.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2008, 2009 Codethink Ltd.
6  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.,
7  * Copyright 2001, 2002, 2003 Ximian, Inc.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #include "config.h"
26 #include "dbus/dbus-glib-lowlevel.h"
27
28 #include <X11/Xlib.h>
29 #include <X11/Xatom.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include<sys/stat.h>
36 #include <atk/atk.h>
37
38 #include <droute/droute.h>
39 #include <gmodule.h>
40 #include <glib/gi18n.h>
41
42 #include "bridge.h"
43 #include "event.h"
44 #include "adaptors.h"
45 #include "object.h"
46
47 #include "accessible-register.h"
48 #include "accessible-leasing.h"
49 #include "accessible-cache.h"
50
51 #include "common/spi-dbus.h"
52
53 /*---------------------------------------------------------------------------*/
54
55 SpiBridge *spi_global_app_data = NULL;
56
57 static const AtkMisc *atk_misc = NULL;
58
59 /*static Display *bridge_display = NULL;*/
60
61 /*---------------------------------------------------------------------------*/
62
63 /*
64  * Returns a 'canonicalized' value for DISPLAY,
65  * with the screen number stripped off if present.
66  *
67  */
68 static const gchar *
69 spi_display_name (void)
70 {
71   static const char *canonical_display_name = NULL;
72   if (!canonical_display_name)
73     {
74       const gchar *display_env = g_getenv ("AT_SPI_DISPLAY");
75       if (!display_env)
76         {
77           display_env = g_getenv ("DISPLAY");
78           if (!display_env || !display_env[0])
79             canonical_display_name = ":0";
80           else
81             {
82               gchar *display_p, *screen_p;
83               canonical_display_name = g_strdup (display_env);
84               display_p = strrchr (canonical_display_name, ':');
85               screen_p = strrchr (canonical_display_name, '.');
86               if (screen_p && display_p && (screen_p > display_p))
87                 {
88                   *screen_p = '\0';
89                 }
90             }
91         }
92       else
93         {
94           canonical_display_name = display_env;
95         }
96     }
97   return canonical_display_name;
98 }
99
100 /*---------------------------------------------------------------------------*/
101
102 /*
103  * Gets the IOR from the XDisplay.
104  * Not currently used in D-Bus version, but something similar
105  * may be employed in the future for accessing the registry daemon
106  * bus name.
107  */
108
109 static DBusConnection *
110 spi_atk_bridge_get_bus (void)
111 {
112   Atom AT_SPI_BUS;
113   Atom actual_type;
114   Display *bridge_display;
115   int actual_format;
116   unsigned char *data = NULL;
117   unsigned long nitems;
118   unsigned long leftover;
119
120   DBusConnection *bus = NULL;
121   DBusError error;
122
123   bridge_display = XOpenDisplay (spi_display_name ());
124   if (!bridge_display)
125     {
126       g_warning ("AT_SPI: Could not get the display\n");
127       return NULL;
128     }
129
130   AT_SPI_BUS = XInternAtom (bridge_display, "AT_SPI_BUS", False);
131   XGetWindowProperty (bridge_display,
132                       XDefaultRootWindow (bridge_display),
133                       AT_SPI_BUS, 0L,
134                       (long) BUFSIZ, False,
135                       (Atom) 31, &actual_type, &actual_format,
136                       &nitems, &leftover, &data);
137
138   dbus_error_init (&error);
139
140   if (data == NULL)
141     {
142       g_warning
143         ("AT-SPI: Accessibility bus not found - Using session bus.\n");
144       bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
145       if (!bus)
146         {
147           g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
148           return NULL;
149         }
150     }
151   else
152     {
153       bus = dbus_connection_open (data, &error);
154       XFree (data);
155       if (!bus)
156         {
157           g_warning ("AT-SPI: Couldn't connect to bus: %s\n", error.message);
158           return NULL;
159         }
160       else
161         {
162           if (!dbus_bus_register (bus, &error))
163             {
164               g_warning ("AT-SPI: Couldn't register with bus: %s\n", error.message);
165               return NULL;
166             }
167         }
168     }
169
170   return bus;
171 }
172
173 static void
174 set_reply (DBusPendingCall *pending, void *user_data)
175 {
176     void **replyptr = (void **)user_data;
177
178     *replyptr = dbus_pending_call_steal_reply (pending);
179   dbus_pending_call_unref (pending);
180 }
181
182 /*---------------------------------------------------------------------------*/
183
184 static void
185 add_event (const char *bus_name, const char *event)
186 {
187   event_data *evdata;
188   gchar **data;
189   GList *new_list;
190
191   evdata = (event_data *) g_malloc (sizeof (*evdata));
192   if (!evdata)
193     return;
194   data = g_strsplit (event, ":", 3);
195   if (!data)
196     {
197       g_free (evdata);
198       return;
199     }
200   evdata->bus_name = g_strdup (bus_name);
201   evdata->data = data;
202   new_list = g_list_append (spi_global_app_data->events, evdata);
203   if (new_list)
204     spi_global_app_data->events = new_list;
205 }
206
207 static void
208 get_registered_event_listeners (SpiBridge *app)
209 {
210   DBusMessage *message, *reply;
211   DBusMessageIter iter, iter_array, iter_struct;
212
213   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
214                                          SPI_DBUS_PATH_REGISTRY,
215                                          SPI_DBUS_INTERFACE_REGISTRY,
216                                          "GetRegisteredEvents");
217   spi_global_app_data->events_initialized = TRUE;
218   if (!message)
219     return;
220
221   reply = dbus_connection_send_with_reply_and_block (app->bus, message, 5000, NULL);
222   dbus_message_unref (message);
223   if (!reply)
224     return;
225   if (strcmp (dbus_message_get_signature (reply), "a(ss)") != 0)
226     {
227       /* TODO: Add a warning when it's okay to add strings */
228       dbus_message_unref (reply);
229       return;
230     }
231   dbus_message_iter_init (reply, &iter);
232   dbus_message_iter_recurse (&iter, &iter_array);
233   while (dbus_message_iter_get_arg_type (&iter_array) != DBUS_TYPE_INVALID)
234     {
235       char *bus_name, *event;
236       dbus_message_iter_recurse (&iter_array, &iter_struct);
237       dbus_message_iter_get_basic (&iter_struct, &bus_name);
238       dbus_message_iter_next (&iter_struct);
239       dbus_message_iter_get_basic (&iter_struct, &event);
240       add_event (bus_name, event);
241       dbus_message_iter_next (&iter_array);
242     }
243   dbus_message_unref (reply);
244 }
245
246 static void
247 register_reply (DBusPendingCall *pending, void *user_data)
248 {
249   DBusMessage *reply;
250   SpiBridge *app = user_data;
251   DBusMessage *message;
252
253   reply = dbus_pending_call_steal_reply (pending);
254   dbus_pending_call_unref (pending);
255   if (reply)
256     {
257       gchar *app_name, *obj_path;
258
259       if (strcmp (dbus_message_get_signature (reply), "(so)") != 0)
260         {
261           g_warning ("AT-SPI: Could not obtain desktop path or name\n");
262         }
263       else
264         {
265           DBusMessageIter iter, iter_struct;
266           dbus_message_iter_init (reply, &iter);
267           dbus_message_iter_recurse (&iter, &iter_struct);
268           dbus_message_iter_get_basic (&iter_struct, &app_name);
269           dbus_message_iter_next (&iter_struct);
270           dbus_message_iter_get_basic (&iter_struct, &obj_path);
271
272           app->desktop_name = g_strdup (app_name);
273           app->desktop_path = g_strdup (obj_path);
274         }
275     }
276   else
277     {
278       g_warning ("AT-SPI: Could not embed inside desktop");
279       return;
280     }
281   dbus_message_unref (reply);
282
283   get_registered_event_listeners (spi_global_app_data);
284 }
285
286 static gboolean
287 register_application (SpiBridge * app)
288 {
289   DBusMessage *message, *reply;
290   DBusMessageIter iter;
291   DBusError error;
292   DBusPendingCall *pending;
293   const int max_addr_length = 128; /* should be long enough */
294
295   dbus_error_init (&error);
296
297   /* These will be overridden when we get a reply, but in practice these
298      defaults should always be correct */
299   app->desktop_name = SPI_DBUS_NAME_REGISTRY;
300   app->desktop_path = SPI_DBUS_PATH_ROOT;
301
302   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
303                                           SPI_DBUS_PATH_ROOT,
304                                           SPI_DBUS_INTERFACE_SOCKET,
305                                           "Embed");
306
307   dbus_message_iter_init_append (message, &iter);
308   spi_object_append_reference (&iter, app->root);
309   
310     if (!dbus_connection_send_with_reply (app->bus, message, &pending, -1)
311         || !pending)
312     {
313         return FALSE;
314     }
315
316     dbus_pending_call_set_notify (pending, register_reply, app, NULL);
317
318   if (message)
319     dbus_message_unref (message);
320
321   /* could this be better, we accept some amount of race in getting the temp name*/
322   /* make sure the directory exists */
323   mkdir ("/tmp/at-spi2/", S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX);
324   chmod ("/tmp/at-spi2/", S_IRWXU|S_IRWXG|S_IRWXO|S_ISVTX);
325   app->app_bus_addr = g_malloc(max_addr_length * sizeof(char));
326 #ifndef DISABLE_P2P
327   sprintf (app->app_bus_addr, "unix:path=/tmp/at-spi2/socket-%d-%d", getpid(),
328            rand());
329 #else
330   app->app_bus_addr [0] = '\0';
331 #endif
332
333   return TRUE;
334 }
335
336 /*---------------------------------------------------------------------------*/
337
338 static void
339 deregister_application (SpiBridge * app)
340 {
341   DBusMessage *message;
342   DBusMessageIter iter;
343   DBusError error;
344   const char *uname;
345
346   dbus_error_init (&error);
347
348   message = dbus_message_new_method_call (SPI_DBUS_NAME_REGISTRY,
349                                           SPI_DBUS_PATH_REGISTRY,
350                                           SPI_DBUS_INTERFACE_REGISTRY,
351                                           "DeregisterApplication");
352   dbus_message_set_no_reply (message, TRUE);
353
354   uname = dbus_bus_get_unique_name (app->bus);
355
356   dbus_message_iter_init_append (message, &iter);
357   dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &uname);
358   dbus_connection_send (app->bus, message, NULL);
359   if (message)
360     dbus_message_unref (message);
361 }
362
363 /*---------------------------------------------------------------------------*/
364
365 static void
366 exit_func (void)
367 {
368   if (!spi_global_app_data)
369     {
370       return;
371     }
372
373   spi_atk_tidy_windows ();
374   spi_atk_deregister_event_listeners ();
375   deregister_application (spi_global_app_data);
376
377   g_free (spi_global_app_data);
378   spi_global_app_data = NULL;
379
380   /* Not currently creating an XDisplay */
381 #if 0
382   if (bridge_display)
383     XCloseDisplay (bridge_display);
384 #endif
385 }
386
387 /*---------------------------------------------------------------------------*/
388
389 static AtkPlugClass *plug_class;
390 static AtkSocketClass *socket_class;
391
392 static gchar *
393 get_plug_id (AtkPlug * plug)
394 {
395   const char *uname = dbus_bus_get_unique_name (spi_global_app_data->bus);
396   gchar *path;
397   GString *str = g_string_new (NULL);
398
399   path = spi_register_object_to_path (spi_global_register, G_OBJECT (plug));
400   g_string_printf (str, "%s:%s", uname, path);
401   g_free (path);
402   return g_string_free (str, FALSE);
403 }
404
405 AtkStateSet *
406 socket_ref_state_set (AtkObject *accessible)
407 {
408   char *child_name, *child_path;
409   AtkSocket *socket = ATK_SOCKET (accessible);
410   int count = 0;
411   int j;
412   int v;
413   DBusMessage *message, *reply;
414   DBusMessageIter iter, iter_array;
415   AtkStateSet *set;
416
417   if (!socket->embedded_plug_id)
418     return NULL;
419
420   child_name = g_strdup (socket->embedded_plug_id);
421   if (!child_name)
422     return NULL;
423   child_path = g_utf8_strchr (child_name + 1, -1, ':');
424   if (!child_path)
425     {
426       g_free (child_name);
427       return NULL;
428     }
429   *(child_path++) = '\0';
430   message = dbus_message_new_method_call (child_name, child_path, SPI_DBUS_INTERFACE_ACCESSIBLE, "GetState");
431   g_free (child_name);
432   reply = dbus_connection_send_with_reply_and_block (spi_global_app_data->bus, message, 1, NULL);
433   dbus_message_unref (message);
434   if (reply == NULL)
435     return NULL;
436   if (strcmp (dbus_message_get_signature (reply), "au") != 0)
437     {
438       dbus_message_unref (reply);
439       return NULL;
440     }
441   set = atk_state_set_new ();
442   if (!set)
443     return  NULL;
444   dbus_message_iter_init (reply, &iter);
445   dbus_message_iter_recurse (&iter, &iter_array);
446   do
447     {
448       dbus_message_iter_get_basic (&iter_array, &v);
449       for (j = 0; j < 32; j++)
450         {
451           if (v & (1 << j))
452             {
453               AtkState state = spi_atk_state_from_spi_state ((count << 5) + j);
454               atk_state_set_add_state (set, state);
455             }
456         }
457       count++;
458     }
459   while (dbus_message_iter_next (&iter_array));
460   dbus_message_unref (reply);
461   return set;
462 }
463
464 static void
465 socket_embed_hook (AtkSocket * socket, gchar * plug_id)
466 {
467   AtkObject *accessible = ATK_OBJECT(socket);
468   gchar *plug_name, *plug_path;
469   AtkObjectClass *klass;
470
471   /* Force registration */
472   gchar *path = spi_register_object_to_path (spi_global_register, G_OBJECT (accessible));
473   /* Let the plug know that it has been embedded */
474   plug_name = g_strdup (plug_id);
475   if (!plug_name)
476     {
477       g_free (path);
478       return;
479     }
480   plug_path = g_utf8_strchr (plug_name + 1, -1, ':');
481   if (plug_path)
482     {
483       DBusMessage *message;
484       *(plug_path++) = '\0';
485       message = dbus_message_new_method_call (plug_name, plug_path, SPI_DBUS_INTERFACE_SOCKET, "Embedded");
486       dbus_message_append_args (message, DBUS_TYPE_STRING, &path, DBUS_TYPE_INVALID);
487       dbus_connection_send (spi_global_app_data->bus, message, NULL);
488     }
489   g_free (plug_name);
490   g_free (path);
491
492   klass = ATK_OBJECT_GET_CLASS (accessible);
493   klass->ref_state_set = socket_ref_state_set;
494 }
495
496 static void
497 install_plug_hooks ()
498 {
499   gpointer data;
500
501   data = g_type_class_ref (ATK_TYPE_PLUG);
502   plug_class = ATK_PLUG_CLASS (data);
503   data = g_type_class_ref (ATK_TYPE_SOCKET);
504   socket_class = ATK_SOCKET_CLASS (data);
505   plug_class->get_object_id = get_plug_id;
506   socket_class->embed = socket_embed_hook;
507 }
508
509 static void
510 new_connection_cb (DBusServer *server, DBusConnection *con, void *data)
511 {
512   GList *new_list;
513
514   dbus_connection_ref(con);
515   dbus_connection_setup_with_g_main(con, NULL);
516   droute_intercept_dbus (con);
517   droute_context_register (spi_global_app_data->droute, con);
518
519   new_list = g_list_append (spi_global_app_data->direct_connections, con);
520   if (new_list)
521     spi_global_app_data->direct_connections = new_list;
522 }
523
524 static int
525 setup_bus (void)
526 {
527 #ifndef DISABLE_P2P
528   DBusServer *server;
529   DBusError err;
530
531   dbus_error_init(&err);
532   server = dbus_server_listen(spi_global_app_data->app_bus_addr, &err);
533   if (server == NULL)
534   {
535     g_warning (_("atk-bridge: Couldn't listen on dbus server: %s"), err.message);
536     dbus_error_init (&err);
537     spi_global_app_data->app_bus_addr [0] = '\0';
538     g_main_context_unref (spi_global_app_data->main_context);
539     spi_global_app_data->main_context = NULL;
540     return -1;
541   }
542
543   dbus_server_setup_with_g_main(server, NULL);
544   dbus_server_set_new_connection_function(server, new_connection_cb, NULL, NULL);
545
546   spi_global_app_data->server = server;
547 #endif
548
549   return 0;
550 }
551
552
553 gchar *atspi_dbus_name = NULL;
554 static gboolean atspi_no_register = FALSE;
555
556 static GOptionEntry atspi_option_entries[] = {
557   {"atspi-dbus-name", 0, 0, G_OPTION_ARG_STRING, &atspi_dbus_name,
558    "D-Bus bus name to register as", NULL},
559   {"atspi-no-register", 0, 0, G_OPTION_ARG_NONE, &atspi_no_register,
560    "Do not register with Registry Daemon", NULL},
561   {NULL}
562 };
563
564 static gchar *
565 introspect_children_cb (const char *path, void *data)
566 {
567   if (!strcmp (path, "/org/a11y/atspi/accessible"))
568     {
569       return g_strdup ("<node name=\"root\"/>\n");
570       /* TODO: Should we place the whole hierarchy here? */
571     }
572   return NULL;
573 }
574
575 static void
576 handle_event_listener_registered (DBusConnection *bus, DBusMessage *message,
577                                   void *user_data)
578 {
579   const char *name;
580   char *sender;
581
582   if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
583     DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
584     return;
585
586   add_event (sender, name);
587 }
588
589 static void
590 remove_events (const char *bus_name, const char *event)
591 {
592   event_data *evdata;
593   gchar **remove_data;
594   GList *list;
595
596   remove_data = g_strsplit (event, ":", 3);
597   if (!remove_data)
598     {
599       return;
600     }
601
602   for (list = spi_global_app_data->events; list;)
603     {
604       event_data *evdata = list->data;
605       if (!g_strcmp0 (evdata->bus_name, bus_name) &&
606           spi_event_is_subtype (evdata->data, remove_data))
607         {
608           GList *events = spi_global_app_data->events;
609           list = list->next;
610           g_strfreev (evdata->data);
611           g_free (evdata->bus_name);
612           g_free (evdata);
613           spi_global_app_data->events = g_list_remove (events, evdata);
614         }
615       else
616         {
617           list = list->next;
618         }
619     }
620
621   g_strfreev (remove_data);
622 }
623
624 static void
625 handle_event_listener_deregistered (DBusConnection *bus, DBusMessage *message,
626                                     void *user_data)
627 {
628   const char *orig_name;
629   gchar *name;
630   char *sender;
631
632   if (!dbus_message_get_args (message, NULL, DBUS_TYPE_STRING, &sender,
633                               DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID))
634     return;
635
636   remove_events (sender, name);
637 }
638
639 static DBusHandlerResult
640 signal_filter (DBusConnection *bus, DBusMessage *message, void *user_data)
641 {
642   const char *interface = dbus_message_get_interface (message);
643   const char *member = dbus_message_get_member (message);
644   DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
645
646   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
647     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
648
649   if (!strcmp (interface, SPI_DBUS_INTERFACE_REGISTRY))
650     {
651       result = DBUS_HANDLER_RESULT_HANDLED;
652       if (!strcmp (member, "EventListenerRegistered"))
653         handle_event_listener_registered (bus, message, user_data);
654       else if (!strcmp (member, "EventListenerDeregistered"))
655         handle_event_listener_deregistered (bus, message, user_data);
656       else
657         result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
658     }
659   return result;
660 }
661
662 /*
663  * spi_app_init
664  *
665  * The following needs to be initialized.
666  *
667  * - DRoute for routing message to their accessible objects.
668  * - Event handlers for emmitting signals on specific ATK events.
669  * - setup the bus for p2p communication
670  * - Application registration with the AT-SPI registry.
671  *
672  */
673 static int
674 adaptor_init (gint * argc, gchar ** argv[])
675 {
676   GOptionContext *opt;
677   GError *err = NULL;
678   DBusError error;
679   AtkObject *root;
680   gchar *introspection_directory;
681   static gboolean inited = FALSE;
682
683   if (inited)
684     return 0;
685
686   inited = TRUE;
687
688   DRoutePath *treepath, *accpath;
689
690   root = atk_get_root ();
691   g_warn_if_fail (root);
692   if (!root)
693     {
694       inited = FALSE;
695       return -1;
696     }
697
698   /* Parse command line options */
699   opt = g_option_context_new (NULL);
700   g_option_context_add_main_entries (opt, atspi_option_entries, NULL);
701   g_option_context_set_ignore_unknown_options (opt, TRUE);
702   if (!g_option_context_parse (opt, argc, argv, &err))
703     g_warning ("AT-SPI Option parsing failed: %s\n", err->message);
704   g_option_context_free (opt);
705
706   /* Allocate global data and do ATK initializations */
707   spi_global_app_data = g_new0 (SpiBridge, 1);
708   atk_misc = atk_misc_get_instance ();
709   spi_global_app_data->root = g_object_ref (root);
710
711   /* Set up D-Bus connection and register bus name */
712   dbus_error_init (&error);
713   spi_global_app_data->bus = spi_atk_bridge_get_bus ();
714   if (!spi_global_app_data->bus)
715     {
716       g_free (spi_global_app_data);
717       spi_global_app_data = NULL;
718       inited = FALSE;
719       return -1;
720     }
721
722   if (atspi_dbus_name != NULL)
723     {
724       if (dbus_bus_request_name
725           (spi_global_app_data->bus, atspi_dbus_name, 0, &error))
726         {
727           g_print ("AT-SPI Recieved D-Bus name - %s\n", atspi_dbus_name);
728         }
729       else
730         {
731           g_print
732             ("AT-SPI D-Bus name requested but could not be allocated - %s\n",
733              atspi_dbus_name);
734         }
735     }
736
737 #ifndef DISABLE_P2P
738   spi_global_app_data->main_context = g_main_context_new ();
739 #else
740   spi_global_app_data->main_context = NULL;
741 #endif
742
743   dbus_connection_setup_with_g_main (spi_global_app_data->bus, NULL);
744
745   /* Hook our plug-and socket functions */
746   install_plug_hooks ();
747
748   /* 
749    * Create the leasing, register and cache objects.
750    * The order is important here, the cache depends on the
751    * register object.
752    */
753   spi_global_register = g_object_new (SPI_REGISTER_TYPE, NULL);
754   spi_global_leasing  = g_object_new (SPI_LEASING_TYPE, NULL);
755   spi_global_cache    = g_object_new (SPI_CACHE_TYPE, NULL);
756
757   /* Register droute for routing AT-SPI messages */
758   spi_global_app_data->droute =
759     droute_new ();
760
761   treepath = droute_add_one (spi_global_app_data->droute,
762                              "/org/a11y/atspi/cache", spi_global_cache);
763
764   if (!treepath)
765     {
766       g_warning ("atk-bridge: Error in droute_add_one().  Already running?");
767       return -1;
768     }
769
770   accpath = droute_add_many (spi_global_app_data->droute,
771                              "/org/a11y/atspi/accessible",
772                              NULL,
773                              introspect_children_cb,
774                              NULL,
775                              (DRouteGetDatumFunction)
776                              spi_global_register_path_to_object);
777
778
779   /* Register all interfaces with droute and set up application accessible db */
780   spi_initialize_cache (treepath);
781   spi_initialize_accessible (accpath);
782   spi_initialize_application (accpath);
783   spi_initialize_action (accpath);
784   spi_initialize_collection (accpath);
785   spi_initialize_component (accpath);
786   spi_initialize_document (accpath);
787   spi_initialize_editabletext (accpath);
788   spi_initialize_hyperlink (accpath);
789   spi_initialize_hypertext (accpath);
790   spi_initialize_image (accpath);
791   spi_initialize_selection (accpath);
792   spi_initialize_socket (accpath);
793   spi_initialize_table (accpath);
794   spi_initialize_text (accpath);
795   spi_initialize_value (accpath);
796
797   droute_context_register (spi_global_app_data->droute,
798                            spi_global_app_data->bus);
799
800   /* Register methods to send D-Bus signals on certain ATK events */
801   spi_atk_register_event_listeners ();
802
803   /* Set up filter and match rules to catch signals */
804   dbus_bus_add_match (spi_global_app_data->bus, "type='signal', interface='org.a11y.atspi.Registry', sender='org.a11y.atspi.Registry'", NULL);
805   dbus_connection_add_filter (spi_global_app_data->bus, signal_filter, NULL,
806                               NULL);
807
808   /* Register this app by sending a signal out to AT-SPI registry daemon */
809   if (!atspi_no_register && (!root || !ATK_IS_PLUG (root)))
810     register_application (spi_global_app_data);
811   else
812     get_registered_event_listeners (spi_global_app_data);
813
814   setup_bus();
815
816   return 0;
817 }
818
819 /*---------------------------------------------------------------------------*/
820
821 int
822 gtk_module_init (gint * argc, gchar ** argv[])
823 {
824   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
825
826   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
827     {
828       return adaptor_init (argc, argv);
829     }
830   return 0;
831 }
832
833 gchar*
834 g_module_check_init (GModule *module)
835 {
836   g_module_make_resident (module);
837
838   return NULL;
839 }
840
841 void
842 gnome_accessibility_module_init (void)
843 {
844   const gchar *load_bridge = g_getenv ("NO_AT_BRIDGE");
845
846   if (!load_bridge || g_ascii_strtod (load_bridge, NULL) == 0)
847     {
848       adaptor_init (NULL, NULL);
849
850       if (g_getenv ("AT_SPI_DEBUG"))
851         {
852           g_print ("Atk Accessibility bridge initialized\n");
853         }
854     }
855 }
856
857 void
858 gnome_accessibility_module_shutdown (void)
859 {
860   spi_atk_deregister_event_listeners ();
861   exit_func ();
862 }
863
864 /*END------------------------------------------------------------------------*/