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