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