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